fix: file upload and OAuth2 (#10)
This commit is contained in:
		
							
								
								
									
										53
									
								
								src/services/users/oauth2/github/add-strategy/get.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/services/users/oauth2/github/add-strategy/get.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
import { Static, Type } from '@sinclair/typebox'
 | 
			
		||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '../../../../../tools/configurations/index.js'
 | 
			
		||||
import { fastifyErrors } from '../../../../../models/utils.js'
 | 
			
		||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
 | 
			
		||||
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - add-strategy',
 | 
			
		||||
  tags: ['users'] as string[],
 | 
			
		||||
  security: [
 | 
			
		||||
    {
 | 
			
		||||
      bearerAuth: []
 | 
			
		||||
    }
 | 
			
		||||
  ] as Array<{ [key: string]: [] }>,
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getAddStrategyGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
  fastify
 | 
			
		||||
) => {
 | 
			
		||||
  await fastify.register(authenticateUser)
 | 
			
		||||
 | 
			
		||||
  await fastify.route<{
 | 
			
		||||
    Querystring: QuerySchemaType
 | 
			
		||||
  }>({
 | 
			
		||||
    method: 'GET',
 | 
			
		||||
    url: '/users/oauth2/github/add-strategy',
 | 
			
		||||
    schema: getServiceSchema,
 | 
			
		||||
    handler: async (request, reply) => {
 | 
			
		||||
      if (request.user == null) {
 | 
			
		||||
        throw fastify.httpErrors.forbidden()
 | 
			
		||||
      }
 | 
			
		||||
      const { redirectURI } = request.query
 | 
			
		||||
      const redirectCallback = `${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`
 | 
			
		||||
      const url = `${GITHUB_BASE_URL}/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&state=${request.user.accessToken}&redirect_uri=${redirectCallback}`
 | 
			
		||||
      reply.statusCode = 200
 | 
			
		||||
      return url
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,56 @@
 | 
			
		||||
import { Static, Type } from '@sinclair/typebox'
 | 
			
		||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '../../../../../tools/configurations/index.js'
 | 
			
		||||
import { fastifyErrors } from '../../../../../models/utils.js'
 | 
			
		||||
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
 | 
			
		||||
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
 | 
			
		||||
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  code: Type.String(),
 | 
			
		||||
  state: Type.String(),
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - callback-add-strategy',
 | 
			
		||||
  tags: ['users'] as string[],
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getCallbackAddStrategyGitHubOAuth2Service: FastifyPluginAsync =
 | 
			
		||||
  async (fastify) => {
 | 
			
		||||
    await fastify.route<{
 | 
			
		||||
      Querystring: QuerySchemaType
 | 
			
		||||
    }>({
 | 
			
		||||
      method: 'GET',
 | 
			
		||||
      url: '/users/oauth2/github/callback-add-strategy',
 | 
			
		||||
      schema: getServiceSchema,
 | 
			
		||||
      handler: async (request, reply) => {
 | 
			
		||||
        const { redirectURI, code, state: accessToken } = request.query
 | 
			
		||||
        const userRequest = await getUserWithBearerToken(
 | 
			
		||||
          `Bearer ${accessToken}`
 | 
			
		||||
        )
 | 
			
		||||
        const githubUser = await getGitHubUserData(
 | 
			
		||||
          code,
 | 
			
		||||
          `${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`
 | 
			
		||||
        )
 | 
			
		||||
        const message = await githubStrategy.callbackAddStrategy(
 | 
			
		||||
          {
 | 
			
		||||
            name: githubUser.name,
 | 
			
		||||
            id: githubUser.id
 | 
			
		||||
          },
 | 
			
		||||
          userRequest
 | 
			
		||||
        )
 | 
			
		||||
        return await reply.redirect(buildQueryURL(redirectURI, { message }))
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
		Reference in New Issue
	
	Block a user