✨ 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:
@ -4,7 +4,7 @@ validateEnv();
|
||||
const { Elysia } = await import("elysia");
|
||||
const { routes } = await import("./routes");
|
||||
|
||||
const app = new Elysia().use(routes).listen(process.env.PORT || 3000);
|
||||
const app = new Elysia().use(routes).listen(process.env.APP_PORT || 3000);
|
||||
|
||||
console.log(
|
||||
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
|
||||
|
||||
20
src/modules/auth/controllers/githubCallback.controller.ts
Normal file
20
src/modules/auth/controllers/githubCallback.controller.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Context } from "elysia";
|
||||
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { githubCallbackService } from "../services/githubCallback.service";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
|
||||
export const githubCallbackController = async (
|
||||
ctx: Context & { query: { code: string } }
|
||||
) => {
|
||||
try {
|
||||
const userData = await githubCallbackService(ctx.query.code);
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
200,
|
||||
"Authenticated successfully!",
|
||||
userData
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
13
src/modules/auth/controllers/githubRequest.controller.ts
Normal file
13
src/modules/auth/controllers/githubRequest.controller.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Context } from "elysia";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { githubRequestService } from "../services/githubRequest.service";
|
||||
|
||||
export const githubRequestController = async (ctx: Context) => {
|
||||
const loginUrl = await githubRequestService();
|
||||
return returnReadResponse(
|
||||
ctx.set,
|
||||
200,
|
||||
"Login URL generated successfully",
|
||||
String(loginUrl)
|
||||
);
|
||||
};
|
||||
7
src/modules/auth/index.ts
Normal file
7
src/modules/auth/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import Elysia from "elysia";
|
||||
import { githubRequestController } from "./controllers/githubRequest.controller";
|
||||
import { githubCallbackController } from "./controllers/githubCallback.controller";
|
||||
|
||||
export const authModule = new Elysia({ prefix: "/auth" })
|
||||
.get("/github", githubRequestController)
|
||||
.get("/github/callback", githubCallbackController);
|
||||
10
src/modules/auth/providers/github.provider.ts
Normal file
10
src/modules/auth/providers/github.provider.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { GitHub } from "arctic";
|
||||
|
||||
export const githubProvider = () => {
|
||||
const redirectURI = `${process.env.APP_PROTOCOL}://${process.env.APP_DOMAIN}${process.env.GITHUB_CLIENT_CALLBACK}`;
|
||||
return new GitHub(
|
||||
process.env.GITHUB_CLIENT_ID!,
|
||||
process.env.GITHUB_CLIENT_SECRET!,
|
||||
redirectURI
|
||||
);
|
||||
};
|
||||
27
src/modules/auth/services/githubCallback.service.ts
Normal file
27
src/modules/auth/services/githubCallback.service.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
16
src/modules/auth/services/githubRequest.service.ts
Normal file
16
src/modules/auth/services/githubRequest.service.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user