create oauth login

Create authentication with oAuth using a third-party vendor. Currently, only GitHub is available, but more will be added
in the future.
This commit is contained in:
Rafi Arrafif
2025-08-05 17:11:36 +07:00
parent 88257d0eee
commit 419b5b0ae4
10 changed files with 132 additions and 4 deletions

View File

@ -0,0 +1,27 @@
import { AppError } from "../../../helpers/error/instances/app";
import { githubProvider } from "../providers/github.provider";
export const githubCallbackService = async (code: string) => {
try {
const github = githubProvider();
const tokens = await github.validateAuthorizationCode(code);
const accessToken = tokens.accessToken();
const userdata = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const useremail = await fetch("https://api.github.com/user/emails", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return {
userdata: await userdata.json(),
useremail: await useremail.json(),
};
} catch (error) {
return new AppError(500, "Authentication service error", error);
}
};

View File

@ -0,0 +1,16 @@
import * as arctic from "arctic";
import { githubProvider } from "../providers/github.provider";
import { AppError } from "../../../helpers/error/instances/app";
export const githubRequestService = async () => {
try {
const github = githubProvider();
const state = arctic.generateState();
const scopes = ["user:email"];
const url = github.createAuthorizationURL(state, scopes);
return url;
} catch (error) {
throw new AppError(500, "Oops! something happening", error);
}
};