Pass extra data to authenticate using AsyncLocalStorage
mainIf your authenticate method needs access to data beyond the standard Request object, you can use the Node.js AsyncLocalStorage API.
- Create an
AsyncLocalStorageinstance. - Wrap the
authenticator.authenticatecall withinasyncLocalStorage.run(...)inside your action. - Inside the strategy's
authenticatemethod, retrieve the data usingasyncLocalStorage.getStore().
import { AsyncLocalStorage } from "async_hooks";
export const asyncLocalStorage = new AsyncLocalStorage<{ someValue: string }>();
// In your Strategy
async authenticate(request: Request): Promise<User> {
let store = asyncLocalStorage.getStore();
if (!store) throw new Error("Failed to get AsyncLocalStorage store");
let { someValue } = store;
// ...
}
// In your Action
export async function action({ request }: Route.ActionArgs) {
let user = await asyncLocalStorage.run({ someValue: "some value" }, () =>
authenticator.authenticate("user-pass", request),
);
// ...
}