mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: make social login optional
This commit is contained in:
1 parent
a79b66ed8d
commit
72b2711367
5 files changed
+113
-17
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { login, register } from '../utils/auth'
|
import { login, register } from '../utils/auth'
|
||||||
import api from '../utils/api'
|
import api from '../utils/api'
|
||||||
@@ -13,6 +13,12 @@ const emailInput = ref(null)
|
|||||||
const password = ref('')
|
const password = ref('')
|
||||||
const errorMessage = ref('')
|
const errorMessage = ref('')
|
||||||
|
|
||||||
|
const availableMethods = ref({})
|
||||||
|
onMounted(async () => {
|
||||||
|
const res = await api.get('/auth/available-methods')
|
||||||
|
availableMethods.value = res.data
|
||||||
|
})
|
||||||
|
|
||||||
const success = () => {
|
const success = () => {
|
||||||
router.push({ name: 'game' })
|
router.push({ name: 'game' })
|
||||||
}
|
}
|
||||||
@@ -172,6 +178,7 @@ const goBack = () => {
|
|||||||
Log in with social account
|
Log in with social account
|
||||||
</p>
|
</p>
|
||||||
<a
|
<a
|
||||||
|
v-if="availableMethods.google"
|
||||||
class="inline-flex justify-center rounded-lg text-sm font-black py-2 mt-3 bg-icon-google text-text w-full"
|
class="inline-flex justify-center rounded-lg text-sm font-black py-2 mt-3 bg-icon-google text-text w-full"
|
||||||
href="/api/v1/auth/google"
|
href="/api/v1/auth/google"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import passport from 'passport'
|
import passport from 'passport'
|
||||||
|
|
||||||
|
import enabledMethods from '../../auth/passport.js'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
register,
|
register,
|
||||||
checkUsername,
|
checkUsername,
|
||||||
@@ -26,14 +28,19 @@ router.get('/check-username', checkUsername)
|
|||||||
|
|
||||||
router.get('/check-session', checkSession)
|
router.get('/check-session', checkSession)
|
||||||
|
|
||||||
router.get('/google', passport.authenticate('google'))
|
router.get('/available-methods', (req, res) => {
|
||||||
|
res.json(enabledMethods)
|
||||||
|
})
|
||||||
|
|
||||||
router.get(
|
if (enabledMethods.google) {
|
||||||
|
router.get('/google', passport.authenticate('google'))
|
||||||
|
router.get(
|
||||||
'/google/callback',
|
'/google/callback',
|
||||||
passport.authenticate('google', {
|
passport.authenticate('google', {
|
||||||
successRedirect: '/',
|
successRedirect: '/',
|
||||||
failureRedirect: '/login',
|
failureRedirect: '/login',
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -3,16 +3,7 @@ import GoogleStrategy from 'passport-google-oidc'
|
|||||||
import config from '../config.js'
|
import config from '../config.js'
|
||||||
import User from '../models/user.js'
|
import User from '../models/user.js'
|
||||||
|
|
||||||
console.log('Google config:', config.google)
|
function verify(issuer, profile, cb) {
|
||||||
|
|
||||||
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)
|
console.log('Get a google login:', profile)
|
||||||
|
|
||||||
process.nextTick(async () => {
|
process.nextTick(async () => {
|
||||||
@@ -46,5 +37,18 @@ export default new GoogleStrategy(
|
|||||||
|
|
||||||
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(passwordStrategy)
|
||||||
passport.use(jwtStrategy)
|
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) => {
|
passport.serializeUser((user, done) => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
@@ -22,3 +32,5 @@ passport.deserializeUser((user, done) => {
|
|||||||
done(null, user)
|
done(null, user)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export default enabledMethods
|
||||||
@@ -319,6 +319,30 @@ paths:
|
|||||||
type: string
|
type: string
|
||||||
example: Username is already taken
|
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:
|
/auth/check-session:
|
||||||
get:
|
get:
|
||||||
summary: Check if the session is valid
|
summary: Check if the session is valid
|
||||||
@@ -334,6 +358,36 @@ paths:
|
|||||||
type: boolean
|
type: boolean
|
||||||
example: true
|
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:
|
/quests:
|
||||||
get:
|
get:
|
||||||
summary: Get quests list
|
summary: Get quests list
|
||||||
@@ -571,3 +625,15 @@ components:
|
|||||||
type: apiKey
|
type: apiKey
|
||||||
in: cookie
|
in: cookie
|
||||||
name: connect.sid
|
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