BREAKING: change user social login schema

This commit is contained in:
wancat committed 2023-11-18 16:29:37 +08:00
1 parent 9e935b320a
commit 81581c4635
8 files changed
+59 -32

No files matched your search

+20 -7
View File
@@ -1,5 +1,20 @@
import { model, Schema } from 'mongoose' import { model, Schema } from 'mongoose'
const socialLoginSchema = new Schema({
provider: {
type: String,
required: true,
},
id: {
type: String,
required: true,
},
displayName: {
type: String,
required: false,
},
})
const User = model( const User = model(
'User', 'User',
new Schema( new Schema(
@@ -15,13 +30,11 @@ const User = model(
required: true, required: true,
}, },
hashedPassword: String, hashedPassword: String,
google: { socialLogins: {
id: String, type: Map,
displayName: String, of: socialLoginSchema,
}, default: {},
github: { required: true,
id: String,
displayName: String,
}, },
}, },
{ {
+7 -5
View File
@@ -1,5 +1,4 @@
import { matchedData } from 'express-validator' import { matchedData } from 'express-validator'
import { User } from '@linux-odyssey/models'
import { genJWT } from '../../utils/auth.js' import { genJWT } from '../../utils/auth.js'
import { createUser } from '../../models/userManager.js' import { createUser } from '../../models/userManager.js'
import { asyncHandler } from '../../middleware/error.js' import { asyncHandler } from '../../middleware/error.js'
@@ -50,7 +49,7 @@ export function checkSession(req, res) {
}) })
} }
export function socialLogin(req, res) { export function socialLoginHandler(req, res) {
const { newUser } = req.user const { newUser } = req.user
if (newUser) { if (newUser) {
req.session.newUser = newUser req.session.newUser = newUser
@@ -71,10 +70,13 @@ export const registerFromSession = asyncHandler(async (req, res, next) => {
return return
} }
newUser.username = username const { email, socialLogin } = newUser
const user = new User(newUser)
await user.save() if (!socialLogin) {
throw new Error('Socail Login not found')
}
const user = await createUser(username, email, { socialLogin })
req.login(user, (err) => { req.login(user, (err) => {
if (err) { if (err) {
+3 -3
View File
@@ -7,8 +7,8 @@ import {
register, register,
checkSession, checkSession,
logout, logout,
socialLogin,
registerFromSession, registerFromSession,
socialLoginHandler,
} from '../controllers/authController.js' } from '../controllers/authController.js'
import { import {
checkUsernameValidators, checkUsernameValidators,
@@ -49,7 +49,7 @@ if (enabledMethods.google) {
'/google/callback', '/google/callback',
authenticateRateLimit, authenticateRateLimit,
passport.authenticate('google'), passport.authenticate('google'),
socialLogin socialLoginHandler
) )
} }
@@ -59,7 +59,7 @@ if (enabledMethods.github) {
'/github/callback', '/github/callback',
authenticateRateLimit, authenticateRateLimit,
passport.authenticate('github'), passport.authenticate('github'),
socialLogin socialLoginHandler
) )
} }
+1 -1
View File
@@ -4,7 +4,7 @@ import config from '../config.js'
import oauthVerify from './oauthVerify.js' import oauthVerify from './oauthVerify.js'
function verify(accessToken, refreshToken, profile, cb) { function verify(accessToken, refreshToken, profile, cb) {
oauthVerify('github', profile, { 'github.id': profile.id }, cb) oauthVerify('github', profile, { 'socialLogins.github.id': profile.id }, cb)
} }
const enabled = config.github.clientID && config.github.clientSecret const enabled = config.github.clientID && config.github.clientSecret
+1 -1
View File
@@ -4,7 +4,7 @@ import config from '../config.js'
import oauthVerify from './oauthVerify.js' import oauthVerify from './oauthVerify.js'
function verify(issuer, profile, cb) { function verify(issuer, profile, cb) {
oauthVerify('google', profile, { 'google.id': profile.id }, cb) oauthVerify('google', profile, { 'socialLogins.google.id': profile.id }, cb)
} }
const enabled = config.google.clientID && config.google.clientSecret const enabled = config.google.clientID && config.google.clientSecret
+7 -6
View File
@@ -7,18 +7,19 @@ export default async function oauthVerify(provider, profile, query, cb) {
return cb(new Error('No email found')) return cb(new Error('No email found'))
} }
const socialLogin = {
provider,
id: profile.id,
displayName: profile.displayName,
}
let user = await User.findOne(query) let user = await User.findOne(query)
if (!user) { if (!user) {
user = await User.findOne({ email }) user = await User.findOne({ email })
if (!user) { if (!user) {
const newUser = { email } const newUser = { email, socialLogin }
newUser[provider] = profile
return cb(null, { newUser }) return cb(null, { newUser })
} }
user[provider] = { user.socialLogins.set(provider, socialLogin)
id: profile.id,
displayName: profile.displayName,
}
await user.save() await user.save()
} }
+11 -7
View File
@@ -26,14 +26,18 @@ if (enabledMethods.github) {
passport.serializeUser((user, done) => { passport.serializeUser((user, done) => {
process.nextTick(async () => { process.nextTick(async () => {
if (!(await UserProfile.exists({ user: user._id }))) { try {
UserProfile.create({ user: user._id }) if (user._id && !(await UserProfile.exists({ user: user._id }))) {
await UserProfile.create({ user: user._id })
}
done(null, {
_id: user._id,
username: user.username,
email: user.email,
})
} catch (err) {
done(err)
} }
done(null, {
_id: user._id,
username: user.username,
email: user.email,
})
}) })
}) })
+9 -2
View File
@@ -1,16 +1,23 @@
import { User, UserProfile } from '@linux-odyssey/models' import { User, UserProfile } from '@linux-odyssey/models'
import { hashPassword } from '../utils/auth.js' import { hashPassword } from '../utils/auth.js'
export async function createUser(username, email, password) { export async function createUser(username, email, { password, socialLogin }) {
const user = new User({ const user = new User({
username, username,
email, email,
}) })
user.hashedPassword = await hashPassword(password) if (password) {
user.hashedPassword = await hashPassword(password)
}
if (socialLogin) {
user.socialLogins.set(socialLogin.provider, socialLogin)
}
await user.save() await user.save()
const userProfile = new UserProfile({ const userProfile = new UserProfile({
user, user,
}) })
await userProfile.save() await userProfile.save()
return user
} }