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

@ -0,0 +1,42 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { HOST, PORT } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'Google OAuth2 - signin',
tags: ['users'] as string[],
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getSigninGoogleOAuth2Service: FastifyPluginAsync = async (
fastify
) => {
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/google/signin',
schema: getServiceSchema,
handler: async (request, reply) => {
const { redirectURI } = request.query
const redirectCallback = `${request.protocol}://${HOST}:${PORT}/users/oauth2/google/callback?redirectURI=${redirectURI}`
const url = `${GOOGLE_BASE_URL}?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${redirectCallback}&response_type=code&scope=profile&access_type=online`
reply.statusCode = 200
return url
}
})
}