mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: github auth
This commit is contained in:
1 parent
8719591faa
commit
ad5344c98b
14 files changed
+127
-2
No files matched your search
@@ -22,6 +22,7 @@
|
||||
"mongoose": "^7.4.0",
|
||||
"node-schedule": "^2.1.1",
|
||||
"passport": "^0.6.0",
|
||||
"passport-github2": "^0.1.12",
|
||||
"passport-google-oidc": "^0.1.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
|
||||
@@ -43,4 +43,15 @@ if (enabledMethods.google) {
|
||||
)
|
||||
}
|
||||
|
||||
if (enabledMethods.github) {
|
||||
router.get('/github', passport.authenticate('github'))
|
||||
router.get(
|
||||
'/github/callback',
|
||||
passport.authenticate('github', {
|
||||
successRedirect: '/',
|
||||
failureRedirect: '/login',
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,53 @@
|
||||
import GitHubStrategy from 'passport-github2'
|
||||
|
||||
import config from '../config.js'
|
||||
import User from '../models/user.js'
|
||||
|
||||
function verify(accessToken, refreshToken, profile, cb) {
|
||||
process.nextTick(async () => {
|
||||
console.log(accessToken, refreshToken, profile)
|
||||
// cb('todo')
|
||||
const email = profile.emails[0]?.value
|
||||
if (!email) {
|
||||
cb(new Error('No email found'))
|
||||
return
|
||||
}
|
||||
|
||||
let user = await User.findOne({ 'github.id': profile.id })
|
||||
if (!user) {
|
||||
user = await User.findOne({ email })
|
||||
const github = {
|
||||
id: profile.id,
|
||||
displayName: profile.displayName,
|
||||
}
|
||||
if (!user) {
|
||||
user = new User({
|
||||
email,
|
||||
github,
|
||||
})
|
||||
} else if (user.github?.id) {
|
||||
cb(new Error('Email already used and binded to another account'))
|
||||
return
|
||||
} else {
|
||||
user.github = github
|
||||
}
|
||||
await user.save()
|
||||
}
|
||||
|
||||
cb(null, user)
|
||||
})
|
||||
}
|
||||
|
||||
const enabled = config.github.clientID && config.github.clientSecret
|
||||
|
||||
export default enabled
|
||||
? new GitHubStrategy(
|
||||
{
|
||||
clientID: config.github.clientID,
|
||||
clientSecret: config.github.clientSecret,
|
||||
callbackURL: `${config.baseUrl}/api/v1/auth/github/callback`,
|
||||
scope: ['user:email'],
|
||||
},
|
||||
verify
|
||||
)
|
||||
: null
|
||||
@@ -2,6 +2,7 @@ import passport from 'passport'
|
||||
import passwordStrategy from './passwordStrategy.js'
|
||||
import jwtStrategy from './jwtStrategy.js'
|
||||
import googleStrategy from './googleStrategy.js'
|
||||
import githubStrategy from './githubStrategy.js'
|
||||
|
||||
passport.use(passwordStrategy)
|
||||
passport.use(jwtStrategy)
|
||||
@@ -9,6 +10,7 @@ passport.use(jwtStrategy)
|
||||
const enabledMethods = {
|
||||
local: true,
|
||||
google: googleStrategy !== null,
|
||||
github: githubStrategy !== null,
|
||||
}
|
||||
|
||||
console.log('Enabled login methods', enabledMethods)
|
||||
@@ -17,6 +19,10 @@ if (enabledMethods.google) {
|
||||
passport.use(googleStrategy)
|
||||
}
|
||||
|
||||
if (enabledMethods.github) {
|
||||
passport.use(githubStrategy)
|
||||
}
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
process.nextTick(() => {
|
||||
done(null, {
|
||||
|
||||
@@ -14,6 +14,10 @@ const config = {
|
||||
clientID: get('GOOGLE_CLIENT_ID', ''),
|
||||
clientSecret: get('GOOGLE_CLIENT_SECRET', ''),
|
||||
},
|
||||
github: {
|
||||
clientID: get('GITHUB_CLIENT_ID', ''),
|
||||
clientSecret: get('GITHUB_CLIENT_SECRET', ''),
|
||||
},
|
||||
}
|
||||
|
||||
config.baseUrl = get('BASE_URL', `http://${config.host}:${config.port}`)
|
||||
|
||||
@@ -17,6 +17,10 @@ const User = model('User', {
|
||||
id: String,
|
||||
displayName: String,
|
||||
},
|
||||
github: {
|
||||
id: String,
|
||||
displayName: String,
|
||||
},
|
||||
})
|
||||
|
||||
export default User
|
||||
Reference in new issue
Block a user