feat: add OAuth2 authentication (Google/GitHub/Discord)

This commit is contained in:
Divlo
2022-03-06 15:41:30 +00:00
parent 9e6bf25c83
commit 91a0e2a76f
16 changed files with 556 additions and 17 deletions

View File

@ -33,24 +33,25 @@ export const getCurrentUser: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const settings = await prisma.userSetting.findFirst({
where: { userId: request.user.current.id }
where: { userId: user.current.id }
})
const OAuths = await prisma.oAuth.findMany({
where: { userId: request.user.current.id }
where: { userId: user.current.id }
})
const strategies = OAuths.map((oauth) => {
return oauth.provider
})
if (request.user.current.password != null) {
if (user.current.password != null) {
strategies.push('local')
}
reply.statusCode = 200
return {
user: {
...request.user.current,
...user.current,
settings,
currentStrategy: request.user.currentStrategy,
currentStrategy: user.currentStrategy,
strategies
}
}

View File

@ -65,7 +65,10 @@ export const putCurrentUser: FastifyPluginAsync = async (fastify) => {
const { redirectURI } = request.params
const userValidation = await prisma.user.findFirst({
where: {
OR: [{ email }, { name }],
OR: [
...(email != null ? [{ email }] : [{}]),
...(name != null ? [{ name }] : [{}])
],
AND: [{ id: { not: request.user.current.id } }]
}
})