remix-hono
Version:
Hono middlewares for Remix
39 lines • 1.53 kB
JavaScript
import { createMiddleware } from "hono/factory";
const sessionStorageKey = Symbol();
const sessionKey = Symbol();
export function session(options) {
return createMiddleware(async (c, next) => {
let sessionStorage = options.createSessionStorage(c);
c.set(sessionStorageKey, sessionStorage);
// If autoCommit is disabled, we just create the SessionStorage and make it
// available with c.get(sessionStorageSymbol), then call next() and
// return.
if (!options.autoCommit)
return await next();
// If autoCommit is enabled, we get the Session from the request.
let session = await sessionStorage.getSession(c.req.raw.headers.get("cookie"));
// And make it available with c.get(sessionSymbol).
c.set(sessionKey, session);
// Then we call next() to let the rest of the middlewares run.
await next();
// Finally, we commit the session before the response is sent.
c.header("set-cookie", await sessionStorage.commitSession(session), {
append: true,
});
});
}
export function getSessionStorage(c) {
let sessionStorage = c.get(sessionStorageKey);
if (!sessionStorage) {
throw new Error("A session middleware was not set.");
}
return sessionStorage;
}
export function getSession(c) {
let session = c.get(sessionKey);
if (!session) {
throw new Error("A session middleware was not set.");
}
return session;
}
//# sourceMappingURL=session.js.map