From a2a46ec93377fd788ca68ba348cd35a01beeb01b Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Sun, 7 Sep 2025 23:17:00 +0700 Subject: [PATCH] :triangular_flag_on_post: (flags) complete github signup process --- src/modules/auth/auth.types.ts | 47 +++++++++++++++++++ .../controllers/githubCallback.controller.ts | 7 ++- .../services/http/githubCallback.service.ts | 37 ++++++++++++--- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/modules/auth/auth.types.ts b/src/modules/auth/auth.types.ts index 4951ded..98060e7 100644 --- a/src/modules/auth/auth.types.ts +++ b/src/modules/auth/auth.types.ts @@ -7,3 +7,50 @@ export interface GoogleCallbackUserData { email: string; email_verified: boolean; } + +export interface GithubCallbackUserData { + user_data: GithubUserData; + user_email: GithubUserEmail[]; +} +interface GithubUserData { + login: string; + id: number; + node_id: string; + avatar_url: string; + gravatar_id: string; + url: string; + html_url: string; + followers_url: string; + following_url: string; + gists_url: string; + starred_url: string; + subscriptions_url: string; + organizations_url: string; + repos_url: string; + events_url: string; + received_events_url: string; + type: string; + user_view_type: string; + site_admin: boolean; + name: string; + company: null; + blog: string; + location: string; + email: null; + hireable: null; + bio: string; + twitter_username: null; + notification_email: null; + public_repos: number; + public_gists: number; + followers: number; + following: number; + created_at: Date; + updated_at: Date; +} +interface GithubUserEmail { + email: string; + primary: boolean; + verified: boolean; + visibility: null | string; +} diff --git a/src/modules/auth/controllers/githubCallback.controller.ts b/src/modules/auth/controllers/githubCallback.controller.ts index 6a577da..245a3b7 100644 --- a/src/modules/auth/controllers/githubCallback.controller.ts +++ b/src/modules/auth/controllers/githubCallback.controller.ts @@ -2,12 +2,15 @@ import { Context } from "elysia"; import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; import { githubCallbackService } from "../services/http/githubCallback.service"; import { mainErrorHandler } from "../../../helpers/error/handler"; +import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation"; export const githubCallbackController = async ( - ctx: Context & { query: { code: string } } + ctx: Context & { query: { code: string; callbackURI: string } } ) => { try { - const userData = await githubCallbackService(ctx.query.code); + const userHeaderInfo = getUserHeaderInformation(ctx); + + const userData = await githubCallbackService(ctx.query, userHeaderInfo); return returnWriteResponse( ctx.set, 200, diff --git a/src/modules/auth/services/http/githubCallback.service.ts b/src/modules/auth/services/http/githubCallback.service.ts index 63b4f90..2b4a41f 100644 --- a/src/modules/auth/services/http/githubCallback.service.ts +++ b/src/modules/auth/services/http/githubCallback.service.ts @@ -1,10 +1,16 @@ import { AppError } from "../../../../helpers/error/instances/app"; +import { UserHeaderInformation } from "../../../../helpers/http/userHeader/getUserHeaderInformation/types"; +import { GithubCallbackUserData } from "../../auth.types"; import { githubProvider } from "../../providers/github.provider"; +import { OAuthUserProvisionService } from "../internal/OAuthUserProvision.service"; -export const githubCallbackService = async (code: string) => { +export const githubCallbackService = async ( + query: { code: string; callbackURI: string }, + userHeaderInfo: UserHeaderInformation +) => { try { - const github = githubProvider(); - const tokens = await github.validateAuthorizationCode(code); + const github = githubProvider(query.callbackURI); + const tokens = await github.validateAuthorizationCode(query.code); const accessToken = tokens.accessToken(); const userdata = await fetch("https://api.github.com/user", { headers: { @@ -17,10 +23,29 @@ export const githubCallbackService = async (code: string) => { }, }); - return { - userdata: await userdata.json(), - useremail: await useremail.json(), + const userPayload: GithubCallbackUserData = { + user_data: await userdata.json(), + user_email: await useremail.json(), }; + + return await OAuthUserProvisionService( + { + provider: "github", + providerId: userPayload.user_data.id.toString(), + providerToken: accessToken, + providerPayload: userPayload, + email: + userPayload.user_email.find((email) => email.primary === true) + ?.email || userPayload.user_email[0].email, + username: `git_${userPayload.user_data.id}`, + name: userPayload.user_data.name, + avatar: userPayload.user_data.avatar_url, + password: Math.random() + .toString(36) + .slice(2, 16), + }, + userHeaderInfo + ); } catch (error) { return new AppError(500, "Authentication service error", error); }