mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-09 07:25:19 +02:00
Cookie fallback
This commit is contained in:
parent
94f1acdd07
commit
ed4a8d558c
2 changed files with 15 additions and 3 deletions
|
@ -85,7 +85,6 @@ export const authOptions = {
|
|||
strategy: 'jwt' as SessionStrategy,
|
||||
maxAge: 1 * 24 * 60 * 60, // 1 Day
|
||||
},
|
||||
useSecureCookies: true,
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: 'Credentials',
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
import cookieParser from 'cookie-parser'
|
||||
import { decode } from 'next-auth/jwt'
|
||||
import type { Request } from 'express'
|
||||
|
||||
const SECRET = process.env.NEXTAUTH_SECRET ?? 'REPLACE_THIS'
|
||||
|
||||
const getNextAuthCookie = (req: Request) => {
|
||||
if (req.cookies) {
|
||||
if ('__Secure-next-auth.session-token' in req.cookies) {
|
||||
return req.cookies['__Secure-next-auth.session-token']
|
||||
} else if ('next-auth.session-token' in req.cookies) {
|
||||
return req.cookies['next-auth.session-token']
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const validateAuthJwt = async (req, res, next) => {
|
||||
cookieParser(SECRET)(req, res, async (err) => {
|
||||
if (err) {
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
|
||||
if (req.cookies && '__Secure-next-auth.session-token' in req.cookies) {
|
||||
if (req.cookies && getNextAuthCookie(req)) {
|
||||
try {
|
||||
const token = await decode({
|
||||
token: req.cookies['__Secure-next-auth.session-token'],
|
||||
token: getNextAuthCookie(req),
|
||||
secret: SECRET,
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue