mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: make social login optional
This commit is contained in:
1 parent
a79b66ed8d
commit
72b2711367
5 files changed
+145
-49
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { Router } from 'express'
|
||||
import passport from 'passport'
|
||||
|
||||
import enabledMethods from '../../auth/passport.js'
|
||||
|
||||
import {
|
||||
register,
|
||||
checkUsername,
|
||||
@@ -26,14 +28,19 @@ router.get('/check-username', checkUsername)
|
||||
|
||||
router.get('/check-session', checkSession)
|
||||
|
||||
router.get('/google', passport.authenticate('google'))
|
||||
router.get('/available-methods', (req, res) => {
|
||||
res.json(enabledMethods)
|
||||
})
|
||||
|
||||
router.get(
|
||||
'/google/callback',
|
||||
passport.authenticate('google', {
|
||||
successRedirect: '/',
|
||||
failureRedirect: '/login',
|
||||
})
|
||||
)
|
||||
if (enabledMethods.google) {
|
||||
router.get('/google', passport.authenticate('google'))
|
||||
router.get(
|
||||
'/google/callback',
|
||||
passport.authenticate('google', {
|
||||
successRedirect: '/',
|
||||
failureRedirect: '/login',
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export default router
|
||||
@@ -3,48 +3,52 @@ import GoogleStrategy from 'passport-google-oidc'
|
||||
import config from '../config.js'
|
||||
import User from '../models/user.js'
|
||||
|
||||
console.log('Google config:', config.google)
|
||||
function verify(issuer, profile, cb) {
|
||||
console.log('Get a google login:', profile)
|
||||
|
||||
export default new GoogleStrategy(
|
||||
{
|
||||
clientID: config.google.clientID,
|
||||
clientSecret: config.google.clientSecret,
|
||||
callbackURL: '/api/v1/auth/google/callback',
|
||||
scope: ['profile', 'email'],
|
||||
},
|
||||
function verify(issuer, profile, cb) {
|
||||
console.log('Get a google login:', profile)
|
||||
process.nextTick(async () => {
|
||||
const email = profile.emails[0]?.value
|
||||
if (!email) {
|
||||
cb(new Error('No email found'))
|
||||
return
|
||||
}
|
||||
|
||||
process.nextTick(async () => {
|
||||
const email = profile.emails[0]?.value
|
||||
if (!email) {
|
||||
cb(new Error('No email found'))
|
||||
return
|
||||
let user = await User.findOne({ 'google.id': profile.id })
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
const google = {
|
||||
id: profile.id,
|
||||
displayName: profile.displayName,
|
||||
}
|
||||
|
||||
let user = await User.findOne({ 'google.id': profile.id })
|
||||
console.log('Existing user:', user)
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
const google = {
|
||||
id: profile.id,
|
||||
displayName: profile.displayName,
|
||||
}
|
||||
console.log('Existing user:', user)
|
||||
if (!user) {
|
||||
user = new User({
|
||||
email,
|
||||
google,
|
||||
})
|
||||
} else if (user.google?.id) {
|
||||
cb(new Error('Email already used and binded to another account'))
|
||||
return
|
||||
} else {
|
||||
user.google = google
|
||||
}
|
||||
await user.save()
|
||||
user = new User({
|
||||
email,
|
||||
google,
|
||||
})
|
||||
} else if (user.google?.id) {
|
||||
cb(new Error('Email already used and binded to another account'))
|
||||
return
|
||||
} else {
|
||||
user.google = google
|
||||
}
|
||||
await user.save()
|
||||
}
|
||||
|
||||
cb(null, user)
|
||||
})
|
||||
}
|
||||
)
|
||||
cb(null, user)
|
||||
})
|
||||
}
|
||||
|
||||
const enabled = config.google.clientID && config.google.clientSecret
|
||||
|
||||
export default enabled
|
||||
? new GoogleStrategy(
|
||||
{
|
||||
clientID: config.google.clientID,
|
||||
clientSecret: config.google.clientSecret,
|
||||
callbackURL: '/api/v1/auth/google/callback',
|
||||
scope: ['profile', 'email'],
|
||||
},
|
||||
verify
|
||||
)
|
||||
: null
|
||||
@@ -5,7 +5,17 @@ import googleStrategy from './googleStrategy.js'
|
||||
|
||||
passport.use(passwordStrategy)
|
||||
passport.use(jwtStrategy)
|
||||
passport.use(googleStrategy)
|
||||
|
||||
const enabledMethods = {
|
||||
local: true,
|
||||
google: googleStrategy !== null,
|
||||
}
|
||||
|
||||
console.log('Enabled login methods', enabledMethods)
|
||||
|
||||
if (enabledMethods.google) {
|
||||
passport.use(googleStrategy)
|
||||
}
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
process.nextTick(() => {
|
||||
@@ -22,3 +32,5 @@ passport.deserializeUser((user, done) => {
|
||||
done(null, user)
|
||||
})
|
||||
})
|
||||
|
||||
export default enabledMethods
|
||||
@@ -319,6 +319,30 @@ paths:
|
||||
type: string
|
||||
example: Username is already taken
|
||||
|
||||
/auth/available-methods:
|
||||
get:
|
||||
summary: Get available login methods
|
||||
responses:
|
||||
'200':
|
||||
description: Available login methods
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
local:
|
||||
type: boolean
|
||||
example: true
|
||||
github:
|
||||
type: boolean
|
||||
example: true
|
||||
google:
|
||||
type: boolean
|
||||
example: true
|
||||
facebook:
|
||||
type: boolean
|
||||
example: true
|
||||
|
||||
/auth/check-session:
|
||||
get:
|
||||
summary: Check if the session is valid
|
||||
@@ -334,6 +358,36 @@ paths:
|
||||
type: boolean
|
||||
example: true
|
||||
|
||||
/auth/google:
|
||||
get:
|
||||
summary: Initiate OAuth2 login with Google
|
||||
operationId: initiateOAuth
|
||||
security:
|
||||
- GoogleOAuth: []
|
||||
responses:
|
||||
302:
|
||||
description: Redirect to Google's OAuth 2.0 authorization endpoint
|
||||
500:
|
||||
description: Internal Server Error
|
||||
/auth/google/callback:
|
||||
get:
|
||||
summary: OAuth2 callback endpoint
|
||||
operationId: oauthCallback
|
||||
parameters:
|
||||
- name: code
|
||||
in: query
|
||||
description: Authorization code from Google
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
200:
|
||||
description: Successful Operation
|
||||
400:
|
||||
description: Bad Request
|
||||
500:
|
||||
description: Internal Server Error
|
||||
|
||||
/quests:
|
||||
get:
|
||||
summary: Get quests list
|
||||
@@ -571,3 +625,15 @@ components:
|
||||
type: apiKey
|
||||
in: cookie
|
||||
name: connect.sid
|
||||
|
||||
GoogleOAuth:
|
||||
type: oauth2
|
||||
flows:
|
||||
authorizationCode:
|
||||
authorizationUrl: https://accounts.google.com/o/oauth2/auth
|
||||
tokenUrl: https://accounts.google.com/o/oauth2/token
|
||||
scopes:
|
||||
# Define your scopes here
|
||||
openid: OpenID
|
||||
profile: User profile
|
||||
email: User email
|
||||
Reference in new issue
Block a user