mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { User } from '@linux-odyssey/models'
|
|
import type { Profile } from 'passport'
|
|
import type { VerifyCallback } from 'passport-oauth2'
|
|
|
|
export default async function oauthVerify(
|
|
provider: string,
|
|
profile: Profile,
|
|
query: Record<string, any>,
|
|
cb: VerifyCallback
|
|
) {
|
|
process.nextTick(async () => {
|
|
try {
|
|
const email = profile.emails ? profile.emails[0]?.value : undefined
|
|
if (!email) {
|
|
return cb(new Error('No email found'))
|
|
}
|
|
|
|
const socialLogin = {
|
|
provider,
|
|
id: profile.id,
|
|
displayName: profile.displayName,
|
|
}
|
|
let user = await User.findOne(query)
|
|
if (!user) {
|
|
user = await User.findOne({ email })
|
|
if (!user) {
|
|
const newUser = { email, socialLogin }
|
|
return cb(null, { newUser })
|
|
}
|
|
user.socialLogins.set(provider, socialLogin)
|
|
await user.save()
|
|
}
|
|
|
|
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')
|
|
)
|
|
}
|
|
})
|
|
}
|