Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow insecure cookie handling in development mode #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ async function getSessionAgent(
res: ServerResponse<IncomingMessage>,
ctx: AppContext
) {
const session = await getIronSession<Session>(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)
Expand Down Expand Up @@ -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<Session>(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()
Expand Down Expand Up @@ -133,10 +127,7 @@ export const createRouter = (ctx: AppContext) => {
router.post(
'/logout',
handler(async (req, res) => {
const session = await getIronSession<Session>(req, res, {
cookieName: 'sid',
password: env.COOKIE_SECRET,
})
const session = await getSession(req, res)
await session.destroy()
return res.redirect('/')
})
Expand Down Expand Up @@ -275,3 +266,13 @@ export const createRouter = (ctx: AppContext) => {

return router
}

async function getSession(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
return await getIronSession<Session>(req, res, {
cookieName: 'sid',
password: env.COOKIE_SECRET,
cookieOptions: {
secure: env.NODE_ENV === 'production',
},
})
}