chore: better Prettier config for easier reviews
This commit is contained in:
		@@ -1,16 +1,16 @@
 | 
			
		||||
import querystring from 'node:querystring'
 | 
			
		||||
import querystring from "node:querystring"
 | 
			
		||||
 | 
			
		||||
import axios from 'axios'
 | 
			
		||||
import axios from "axios"
 | 
			
		||||
 | 
			
		||||
import { OAuthStrategy } from '#src/tools/utils/OAuthStrategy.js'
 | 
			
		||||
import { OAuthStrategy } from "#src/tools/utils/OAuthStrategy.js"
 | 
			
		||||
 | 
			
		||||
export const GITHUB_PROVIDER = 'GitHub'
 | 
			
		||||
export const GITHUB_BASE_URL = 'https://github.com'
 | 
			
		||||
export const GITHUB_API_BASE_URL = 'https://api.github.com'
 | 
			
		||||
export const GITHUB_PROVIDER = "GitHub"
 | 
			
		||||
export const GITHUB_BASE_URL = "https://github.com"
 | 
			
		||||
export const GITHUB_API_BASE_URL = "https://api.github.com"
 | 
			
		||||
export const GITHUB_CLIENT_ID =
 | 
			
		||||
  process.env['GITHUB_CLIENT_ID'] ?? 'GITHUB_CLIENT_ID'
 | 
			
		||||
  process.env["GITHUB_CLIENT_ID"] ?? "GITHUB_CLIENT_ID"
 | 
			
		||||
export const GITHUB_CLIENT_SECRET =
 | 
			
		||||
  process.env['GITHUB_CLIENT_SECRET'] ?? 'GITHUB_CLIENT_SECRET'
 | 
			
		||||
  process.env["GITHUB_CLIENT_SECRET"] ?? "GITHUB_CLIENT_SECRET"
 | 
			
		||||
export const githubStrategy = new OAuthStrategy(GITHUB_PROVIDER)
 | 
			
		||||
 | 
			
		||||
export interface GitHubUser {
 | 
			
		||||
@@ -28,7 +28,7 @@ export interface GitHubTokens {
 | 
			
		||||
 | 
			
		||||
export const getGitHubUserData = async (
 | 
			
		||||
  code: string,
 | 
			
		||||
  redirectURI: string
 | 
			
		||||
  redirectURI: string,
 | 
			
		||||
): Promise<GitHubUser> => {
 | 
			
		||||
  const { data: token } = await axios.post<GitHubTokens>(
 | 
			
		||||
    `${GITHUB_BASE_URL}/login/oauth/access_token`,
 | 
			
		||||
@@ -36,22 +36,22 @@ export const getGitHubUserData = async (
 | 
			
		||||
      client_id: GITHUB_CLIENT_ID,
 | 
			
		||||
      client_secret: GITHUB_CLIENT_SECRET,
 | 
			
		||||
      code,
 | 
			
		||||
      redirect_uri: redirectURI
 | 
			
		||||
      redirect_uri: redirectURI,
 | 
			
		||||
    }),
 | 
			
		||||
    {
 | 
			
		||||
      headers: {
 | 
			
		||||
        'Content-Type': 'application/x-www-form-urlencoded',
 | 
			
		||||
        Accept: 'application/json'
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
        "Content-Type": "application/x-www-form-urlencoded",
 | 
			
		||||
        Accept: "application/json",
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
  )
 | 
			
		||||
  const { data: githubUser } = await axios.get<GitHubUser>(
 | 
			
		||||
    `${GITHUB_API_BASE_URL}/user`,
 | 
			
		||||
    {
 | 
			
		||||
      headers: {
 | 
			
		||||
        Authorization: `token ${token.access_token}`
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
        Authorization: `token ${token.access_token}`,
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
  )
 | 
			
		||||
  return githubUser
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,44 +1,44 @@
 | 
			
		||||
import type { Static } from '@sinclair/typebox'
 | 
			
		||||
import { Type } from '@sinclair/typebox'
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
import type { Static } from "@sinclair/typebox"
 | 
			
		||||
import { Type } from "@sinclair/typebox"
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from "fastify"
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '#src/tools/configurations.js'
 | 
			
		||||
import { fastifyErrors } from '#src/models/utils.js'
 | 
			
		||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
 | 
			
		||||
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
 | 
			
		||||
import { API_URL } from "#src/tools/configurations.js"
 | 
			
		||||
import { fastifyErrors } from "#src/models/utils.js"
 | 
			
		||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "../__utils__/utils.js"
 | 
			
		||||
import authenticateUser from "#src/tools/plugins/authenticateUser.js"
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
  redirectURI: Type.String({ format: "uri-reference" }),
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - add-strategy',
 | 
			
		||||
  tags: ['oauth2'] as string[],
 | 
			
		||||
  description: "GitHub OAuth2 - add-strategy",
 | 
			
		||||
  tags: ["oauth2"] as string[],
 | 
			
		||||
  security: [
 | 
			
		||||
    {
 | 
			
		||||
      bearerAuth: []
 | 
			
		||||
    }
 | 
			
		||||
      bearerAuth: [],
 | 
			
		||||
    },
 | 
			
		||||
  ] as Array<{ [key: string]: [] }>,
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
    500: fastifyErrors[500],
 | 
			
		||||
  },
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getAddStrategyGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
  fastify
 | 
			
		||||
  fastify,
 | 
			
		||||
) => {
 | 
			
		||||
  await fastify.register(authenticateUser)
 | 
			
		||||
 | 
			
		||||
  await fastify.route<{
 | 
			
		||||
    Querystring: QuerySchemaType
 | 
			
		||||
  }>({
 | 
			
		||||
    method: 'GET',
 | 
			
		||||
    url: '/users/oauth2/github/add-strategy',
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/users/oauth2/github/add-strategy",
 | 
			
		||||
    schema: getServiceSchema,
 | 
			
		||||
    handler: async (request, reply) => {
 | 
			
		||||
      if (request.user == null) {
 | 
			
		||||
@@ -49,6 +49,6 @@ export const getAddStrategyGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
      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
 | 
			
		||||
    }
 | 
			
		||||
    },
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,30 +1,30 @@
 | 
			
		||||
import type { Static } from '@sinclair/typebox'
 | 
			
		||||
import { Type } from '@sinclair/typebox'
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
import type { Static } from "@sinclair/typebox"
 | 
			
		||||
import { Type } from "@sinclair/typebox"
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from "fastify"
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '#src/tools/configurations.js'
 | 
			
		||||
import { fastifyErrors } from '#src/models/utils.js'
 | 
			
		||||
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
 | 
			
		||||
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
 | 
			
		||||
import { getUserWithBearerToken } from '#src/tools/plugins/authenticateUser.js'
 | 
			
		||||
import { API_URL } from "#src/tools/configurations.js"
 | 
			
		||||
import { fastifyErrors } from "#src/models/utils.js"
 | 
			
		||||
import { githubStrategy, getGitHubUserData } from "../__utils__/utils.js"
 | 
			
		||||
import { buildQueryURL } from "#src/tools/utils/buildQueryURL.js"
 | 
			
		||||
import { getUserWithBearerToken } from "#src/tools/plugins/authenticateUser.js"
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  code: Type.String(),
 | 
			
		||||
  state: Type.String(),
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
  redirectURI: Type.String({ format: "uri-reference" }),
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - callback-add-strategy',
 | 
			
		||||
  tags: ['oauth2'] as string[],
 | 
			
		||||
  description: "GitHub OAuth2 - callback-add-strategy",
 | 
			
		||||
  tags: ["oauth2"] as string[],
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
    500: fastifyErrors[500],
 | 
			
		||||
  },
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getCallbackAddStrategyGitHubOAuth2Service: FastifyPluginAsync =
 | 
			
		||||
@@ -32,26 +32,26 @@ export const getCallbackAddStrategyGitHubOAuth2Service: FastifyPluginAsync =
 | 
			
		||||
    await fastify.route<{
 | 
			
		||||
      Querystring: QuerySchemaType
 | 
			
		||||
    }>({
 | 
			
		||||
      method: 'GET',
 | 
			
		||||
      url: '/users/oauth2/github/callback-add-strategy',
 | 
			
		||||
      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}`
 | 
			
		||||
          `Bearer ${accessToken}`,
 | 
			
		||||
        )
 | 
			
		||||
        const githubUser = await getGitHubUserData(
 | 
			
		||||
          code,
 | 
			
		||||
          `${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`
 | 
			
		||||
          `${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`,
 | 
			
		||||
        )
 | 
			
		||||
        const message = await githubStrategy.callbackAddStrategy(
 | 
			
		||||
          {
 | 
			
		||||
            name: githubUser.name,
 | 
			
		||||
            id: githubUser.id
 | 
			
		||||
            id: githubUser.id,
 | 
			
		||||
          },
 | 
			
		||||
          userRequest
 | 
			
		||||
          userRequest,
 | 
			
		||||
        )
 | 
			
		||||
        return await reply.redirect(buildQueryURL(redirectURI, { message }))
 | 
			
		||||
      }
 | 
			
		||||
      },
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,50 +1,50 @@
 | 
			
		||||
import type { Static } from '@sinclair/typebox'
 | 
			
		||||
import { Type } from '@sinclair/typebox'
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
import type { Static } from "@sinclair/typebox"
 | 
			
		||||
import { Type } from "@sinclair/typebox"
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from "fastify"
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '#src/tools/configurations.js'
 | 
			
		||||
import { fastifyErrors } from '#src/models/utils.js'
 | 
			
		||||
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
 | 
			
		||||
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
 | 
			
		||||
import { API_URL } from "#src/tools/configurations.js"
 | 
			
		||||
import { fastifyErrors } from "#src/models/utils.js"
 | 
			
		||||
import { githubStrategy, getGitHubUserData } from "../__utils__/utils.js"
 | 
			
		||||
import { buildQueryURL } from "#src/tools/utils/buildQueryURL.js"
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  code: Type.String(),
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
  redirectURI: Type.String({ format: "uri-reference" }),
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - callback',
 | 
			
		||||
  tags: ['oauth2'] as string[],
 | 
			
		||||
  description: "GitHub OAuth2 - callback",
 | 
			
		||||
  tags: ["oauth2"] as string[],
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
    500: fastifyErrors[500],
 | 
			
		||||
  },
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getCallbackGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
  fastify
 | 
			
		||||
  fastify,
 | 
			
		||||
) => {
 | 
			
		||||
  await fastify.route<{
 | 
			
		||||
    Querystring: QuerySchemaType
 | 
			
		||||
  }>({
 | 
			
		||||
    method: 'GET',
 | 
			
		||||
    url: '/users/oauth2/github/callback',
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/users/oauth2/github/callback",
 | 
			
		||||
    schema: getServiceSchema,
 | 
			
		||||
    handler: async (request, reply) => {
 | 
			
		||||
      const { redirectURI, code } = request.query
 | 
			
		||||
      const githubUser = await getGitHubUserData(
 | 
			
		||||
        code,
 | 
			
		||||
        `${API_URL}/users/oauth2/github/callback?redirectURI=${redirectURI}`
 | 
			
		||||
        `${API_URL}/users/oauth2/github/callback?redirectURI=${redirectURI}`,
 | 
			
		||||
      )
 | 
			
		||||
      const responseJWT = await githubStrategy.callbackSignin({
 | 
			
		||||
        name: githubUser.name,
 | 
			
		||||
        id: githubUser.id
 | 
			
		||||
        id: githubUser.id,
 | 
			
		||||
      })
 | 
			
		||||
      return await reply.redirect(buildQueryURL(redirectURI, responseJWT))
 | 
			
		||||
    }
 | 
			
		||||
    },
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,36 +1,36 @@
 | 
			
		||||
import type { Static } from '@sinclair/typebox'
 | 
			
		||||
import { Type } from '@sinclair/typebox'
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
 | 
			
		||||
import type { Static } from "@sinclair/typebox"
 | 
			
		||||
import { Type } from "@sinclair/typebox"
 | 
			
		||||
import type { FastifyPluginAsync, FastifySchema } from "fastify"
 | 
			
		||||
 | 
			
		||||
import { API_URL } from '#src/tools/configurations.js'
 | 
			
		||||
import { fastifyErrors } from '#src/models/utils.js'
 | 
			
		||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
 | 
			
		||||
import { API_URL } from "#src/tools/configurations.js"
 | 
			
		||||
import { fastifyErrors } from "#src/models/utils.js"
 | 
			
		||||
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "../__utils__/utils.js"
 | 
			
		||||
 | 
			
		||||
const querySchema = Type.Object({
 | 
			
		||||
  redirectURI: Type.String({ format: 'uri-reference' })
 | 
			
		||||
  redirectURI: Type.String({ format: "uri-reference" }),
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
type QuerySchemaType = Static<typeof querySchema>
 | 
			
		||||
 | 
			
		||||
const getServiceSchema: FastifySchema = {
 | 
			
		||||
  description: 'GitHub OAuth2 - signin',
 | 
			
		||||
  tags: ['oauth2'] as string[],
 | 
			
		||||
  description: "GitHub OAuth2 - signin",
 | 
			
		||||
  tags: ["oauth2"] as string[],
 | 
			
		||||
  querystring: querySchema,
 | 
			
		||||
  response: {
 | 
			
		||||
    200: Type.String(),
 | 
			
		||||
    400: fastifyErrors[400],
 | 
			
		||||
    500: fastifyErrors[500]
 | 
			
		||||
  }
 | 
			
		||||
    500: fastifyErrors[500],
 | 
			
		||||
  },
 | 
			
		||||
} as const
 | 
			
		||||
 | 
			
		||||
export const getSigninGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
  fastify
 | 
			
		||||
  fastify,
 | 
			
		||||
) => {
 | 
			
		||||
  await fastify.route<{
 | 
			
		||||
    Querystring: QuerySchemaType
 | 
			
		||||
  }>({
 | 
			
		||||
    method: 'GET',
 | 
			
		||||
    url: '/users/oauth2/github/signin',
 | 
			
		||||
    method: "GET",
 | 
			
		||||
    url: "/users/oauth2/github/signin",
 | 
			
		||||
    schema: getServiceSchema,
 | 
			
		||||
    handler: async (request, reply) => {
 | 
			
		||||
      const { redirectURI } = request.query
 | 
			
		||||
@@ -38,6 +38,6 @@ export const getSigninGitHubOAuth2Service: FastifyPluginAsync = async (
 | 
			
		||||
      const url = `${GITHUB_BASE_URL}/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&redirect_uri=${redirectCallback}`
 | 
			
		||||
      reply.statusCode = 200
 | 
			
		||||
      return url
 | 
			
		||||
    }
 | 
			
		||||
    },
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user