From e34000c9c7c1558acfcb21c599f17a0a1a487543 Mon Sep 17 00:00:00 2001 From: drewmcarthur Date: Sun, 8 Dec 2024 22:45:14 -0700 Subject: [PATCH] extract to helper --- src/routes.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/routes.ts b/src/routes.ts index c42cc78..2e28014 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -38,10 +38,7 @@ async function getSessionAgent( res: ServerResponse, ctx: AppContext ) { - const session = await getIronSession(req, res, { - cookieName: 'sid', - password: env.COOKIE_SECRET, - }) + const session = await getSession(req, res) if (!session.did) return null try { const oauthSession = await ctx.oauthClient.restore(session.did) @@ -74,10 +71,7 @@ export const createRouter = (ctx: AppContext) => { const params = new URLSearchParams(req.originalUrl.split('?')[1]) try { const { session } = await ctx.oauthClient.callback(params) - const clientSession = await getIronSession(req, res, { - cookieName: 'sid', - password: env.COOKIE_SECRET, - }) + const clientSession = await getSession(req, res) assert(!clientSession.did, 'session already exists') clientSession.did = session.did await clientSession.save() @@ -133,10 +127,7 @@ export const createRouter = (ctx: AppContext) => { router.post( '/logout', handler(async (req, res) => { - const session = await getIronSession(req, res, { - cookieName: 'sid', - password: env.COOKIE_SECRET, - }) + const session = await getSession(req, res) await session.destroy() return res.redirect('/') }) @@ -275,3 +266,13 @@ export const createRouter = (ctx: AppContext) => { return router } + +async function getSession(req: IncomingMessage, res: ServerResponse) { + return await getIronSession(req, res, { + cookieName: 'sid', + password: env.COOKIE_SECRET, + cookieOptions: { + secure: env.NODE_ENV === 'production', + }, + }) +} \ No newline at end of file