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

fix: session verification #1571

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
65 changes: 36 additions & 29 deletions packages/nuxt/src/runtime/auth/api.session-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,46 @@ export default defineEventHandler(async (event) => {
const adminAuth = getAdminAuth(adminApp)

logger.debug(token ? 'Verifying the token' : 'Deleting the session cookie')
const verifiedIdToken = token ? await adminAuth.verifyIdToken(token) : null
try {
const verifiedIdToken = token ? await adminAuth.verifyIdToken(token) : null

if (verifiedIdToken) {
if (new Date().getTime() / 1_000 - verifiedIdToken.iat > ID_TOKEN_MAX_AGE) {
setResponseStatus(event, 301)
} else {
const cookie = await adminAuth
.createSessionCookie(token!, { expiresIn: AUTH_COOKIE_MAX_AGE })
.catch((e: any) => {
logger.error('Error minting the cookie', e)
})
if (cookie) {
// logger.debug(`minted a session cookie for user ${verifiedIdToken.uid}`)
setCookie(event, AUTH_COOKIE_NAME, cookie, {
maxAge: AUTH_COOKIE_MAX_AGE,
secure: true,
httpOnly: true,
path: '/',
sameSite: 'lax',
// add user overrides
...(typeof runtimeConfig.vuefire?.auth?.sessionCookie === 'object'
? runtimeConfig.vuefire?.auth?.sessionCookie
: {}),
})
setResponseStatus(event, 201)
return ''
if (verifiedIdToken) {
if (
new Date().getTime() / 1_000 - verifiedIdToken.iat >
ID_TOKEN_MAX_AGE
) {
setResponseStatus(event, 301)
} else {
setResponseStatus(event, 401)
return ''
const cookie = await adminAuth
.createSessionCookie(token!, { expiresIn: AUTH_COOKIE_MAX_AGE })
.catch((e: any) => {
logger.error('Error minting the cookie', e)
})
if (cookie) {
// logger.debug(`minted a session cookie for user ${verifiedIdToken.uid}`)
setCookie(event, AUTH_COOKIE_NAME, cookie, {
maxAge: AUTH_COOKIE_MAX_AGE,
secure: true,
httpOnly: true,
path: '/',
sameSite: 'lax',
// add user overrides
...(typeof runtimeConfig.vuefire?.auth?.sessionCookie === 'object'
? runtimeConfig.vuefire?.auth?.sessionCookie
: {}),
})
setResponseStatus(event, 201)
return ''
} else {
setResponseStatus(event, 401)
return ''
}
}
} else {
throw new Error('Cannot verify the token')
}
} else {
// logger.debug('deleting the session cookie')
} catch (error: any) {
logger.debug(`[${error.code}] ${error.message}`)
deleteCookie(event, AUTH_COOKIE_NAME)
setResponseStatus(event, 204)
}
Expand Down