mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 11:10:11 +08:00
refactor: userController and add req user type
This commit is contained in:
1 parent
4c421b4cb3
commit
c912319967
8 files changed
+49
-23
No files matched your search
@@ -2,12 +2,6 @@ import { User } from '@linux-odyssey/models'
|
||||
import type { Profile } from 'passport'
|
||||
import type { VerifyCallback } from 'passport-oauth2'
|
||||
|
||||
export type SocialLogin = {
|
||||
provider: string
|
||||
id: string
|
||||
displayName: string
|
||||
}
|
||||
|
||||
export default async function oauthVerify(
|
||||
provider: string,
|
||||
profile: Profile,
|
||||
@@ -37,7 +31,11 @@ export default async function oauthVerify(
|
||||
await user.save()
|
||||
}
|
||||
|
||||
return cb(null, user)
|
||||
return cb(null, {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
})
|
||||
} catch (err) {
|
||||
return cb(
|
||||
err instanceof Error ? err : new Error('An unknown error occurred')
|
||||
|
||||
@@ -29,11 +29,11 @@ if (githubStrategy !== null) {
|
||||
passport.serializeUser((user: Express.User, done) => {
|
||||
process.nextTick(async () => {
|
||||
try {
|
||||
const id = (user as any)._id
|
||||
if (!id) {
|
||||
if (!('id' in user)) {
|
||||
done(new Error('User has no _id'))
|
||||
return
|
||||
}
|
||||
const { id } = user
|
||||
if (!(await UserProfile.exists({ user: id }))) {
|
||||
await UserProfile.create({ user: id })
|
||||
}
|
||||
|
||||
@@ -29,7 +29,11 @@ async function verifyPassword(
|
||||
if (!isPasswordValid) {
|
||||
return done(null, false, { message: 'Incorrect username or password.' })
|
||||
}
|
||||
return done(null, user)
|
||||
return done(null, {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
})
|
||||
} catch (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user