Skip to content

Commit

Permalink
Move multi auth init
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Mar 3, 2025
1 parent 090203b commit ea83664
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ function getCallbacks (req, res) {
}
}
}

// add multi_auth cookie for user that just logged in
if (res) {
const secret = process.env.NEXTAUTH_SECRET
const jwt = await encodeJWT({ token, secret })
const me = await prisma.user.findUnique({ where: { id: token.id } })
setMultiAuthCookies(new NodeNextRequest(req), new NodeNextResponse(res), { ...me, jwt })
}
}

if (token?.id) {
Expand All @@ -125,17 +133,6 @@ function getCallbacks (req, res) {
token.sub = Number(token.id)
}

// this only runs during a signup/login because response is only defined during signup/login
// and will add the multi_auth cookies for the user we just logged in as
if (req && res) {
req = new NodeNextRequest(req)
res = new NodeNextResponse(res)
const secret = process.env.NEXTAUTH_SECRET
const jwt = await encodeJWT({ token, secret })
const me = await prisma.user.findUnique({ where: { id: token.id } })
setMultiAuthCookies(req, res, { ...me, jwt })
}

return token
},
async session ({ session, token }) {
Expand Down

0 comments on commit ea83664

Please sign in to comment.