fix: OAuth flow by setting scope and cookie to 'lax'

This commit is contained in:
wancat committed 2024-04-05 17:56:09 +08:00
1 parent df140c64f8
commit 455fde41ab
5 files changed
+12 -5

No files matched your search

+4 -1
View File
@@ -46,7 +46,10 @@ router.get('/available-methods', (req, res) => {
})
if (enabledMethods.google) {
router.get('/google', passport.authenticate('google'))
router.get(
'/google',
passport.authenticate('google', { scope: ['email', 'profile'] })
)
router.get(
'/google/callback',
authenticateRateLimit,
+2
View File
@@ -1,6 +1,7 @@
import { User } from '@linux-odyssey/models'
import type { Profile } from 'passport'
import type { VerifyCallback } from 'passport-oauth2'
import logger from '../utils/logger.js'
export default async function oauthVerify(
provider: string,
@@ -37,6 +38,7 @@ export default async function oauthVerify(
email: user.email,
})
} catch (err) {
logger.error(err)
return cb(
err instanceof Error ? err : new Error('An unknown error occurred')
)
+4 -2
View File
@@ -1,6 +1,6 @@
import passport from 'passport'
import { UserProfile } from '@linux-odyssey/models'
import type { Express } from 'express'
import { UserProfile } from '@linux-odyssey/models'
import passwordStrategy from './passwordStrategy.js'
import jwtStrategy from './jwtStrategy.js'
import googleStrategy from './googleStrategy.js'
@@ -30,7 +30,8 @@ passport.serializeUser((user: Express.User, done) => {
process.nextTick(async () => {
try {
if (!('id' in user)) {
done(new Error('User has no _id'))
// OAuth new user
done(null, user)
return
}
const { id } = user
@@ -39,6 +40,7 @@ passport.serializeUser((user: Express.User, done) => {
}
done(null, user)
} catch (err) {
logger.error(err)
done(err)
}
})
+1 -1
View File
@@ -13,7 +13,7 @@ export default function errorHandler(
req: Request,
res: Response,
// eslint-disable-next-line no-unused-vars
next: NextFunction
_next: NextFunction
) {
logger.error(err) // Log error stack trace to the console
res.status(500).send({ message: 'Something broke!' }) // Send error response
+1 -1
View File
@@ -12,7 +12,7 @@ export default session({
domain: config.domain,
httpOnly: true,
secure: config.protocol === 'https:',
sameSite: 'strict',
sameSite: 'lax',
maxAge: config.sessionMaxAge,
},
store: MongoStore.create({