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