🚩 (flags) complete github signup process

This commit is contained in:
Rafi Arrafif
2025-09-07 23:17:00 +07:00
parent 8861b0997d
commit a2a46ec933
3 changed files with 83 additions and 8 deletions

View File

@ -7,3 +7,50 @@ export interface GoogleCallbackUserData {
email: string; email: string;
email_verified: boolean; 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;
}

View File

@ -2,12 +2,15 @@ import { Context } from "elysia";
import { returnWriteResponse } from "../../../helpers/callback/httpResponse"; import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
import { githubCallbackService } from "../services/http/githubCallback.service"; import { githubCallbackService } from "../services/http/githubCallback.service";
import { mainErrorHandler } from "../../../helpers/error/handler"; import { mainErrorHandler } from "../../../helpers/error/handler";
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
export const githubCallbackController = async ( export const githubCallbackController = async (
ctx: Context & { query: { code: string } } ctx: Context & { query: { code: string; callbackURI: string } }
) => { ) => {
try { try {
const userData = await githubCallbackService(ctx.query.code); const userHeaderInfo = getUserHeaderInformation(ctx);
const userData = await githubCallbackService(ctx.query, userHeaderInfo);
return returnWriteResponse( return returnWriteResponse(
ctx.set, ctx.set,
200, 200,

View File

@ -1,10 +1,16 @@
import { AppError } from "../../../../helpers/error/instances/app"; 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 { 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 { try {
const github = githubProvider(); const github = githubProvider(query.callbackURI);
const tokens = await github.validateAuthorizationCode(code); const tokens = await github.validateAuthorizationCode(query.code);
const accessToken = tokens.accessToken(); const accessToken = tokens.accessToken();
const userdata = await fetch("https://api.github.com/user", { const userdata = await fetch("https://api.github.com/user", {
headers: { headers: {
@ -17,10 +23,29 @@ export const githubCallbackService = async (code: string) => {
}, },
}); });
return { const userPayload: GithubCallbackUserData = {
userdata: await userdata.json(), user_data: await userdata.json(),
useremail: await useremail.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) { } catch (error) {
return new AppError(500, "Authentication service error", error); return new AppError(500, "Authentication service error", error);
} }