• Creates a context storage middleware that stores the context, and request, in an AsyncLocalStorage.

    This is useful for accessing the context in any part of the application inside loaders or actions, but without needing to pass the context around from loaders or actions to those functions.

    After the middleware is added, any function that needs the context can call the getContext function to retrieve the context from the storage.

    Returns unstable_createContextStorageMiddleware.ReturnType

    The context storage middleware and a function to get the context from the storage

    // app/middlewares/context-storage.ts
    import { unstable_createContextStorageMiddleware } from "remix-utils";

    export const [contextStorageMiddleware, getContext, getRequest] = unstable_createContextStorageMiddleware();

    // app/root.tsx
    import { contextStorageMiddleware } from "~/middlewares/context-storage";
    export const unstable_middleware = [contextStorageMiddleware];

    // app/helpers/authenticate.ts
    import { getContext } from "~/middlewares/context-storage";
    import { getSession } from "~/middlewares/session";

    export function authenticate() {
    let context = getContext();
    let session = getSession(context);
    // code to authenticate the user
    }