✨ feat: add logout module
This commit is contained in:
@ -1,15 +1,14 @@
|
|||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { JWTSessionPayload } from "../../../../modules/auth/auth.types";
|
import { JWTAuthToken } from "./types";
|
||||||
import { AppError } from "../../../error/instances/app";
|
import { AppError } from "../../../error/instances/app";
|
||||||
|
|
||||||
export const jwtDecode = (payload: string) => {
|
export const jwtDecode = (payload: string) => {
|
||||||
// return payload;
|
|
||||||
if (!payload) throw new AppError(401, "Unauthorized");
|
if (!payload) throw new AppError(401, "Unauthorized");
|
||||||
const JWTKey = process.env.JWT_SECRET!;
|
const JWTKey = process.env.JWT_SECRET!;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const decodedPayload = jwt.verify(payload, JWTKey);
|
const decodedPayload = jwt.verify(payload, JWTKey);
|
||||||
return decodedPayload as JWTSessionPayload;
|
return decodedPayload as JWTAuthToken;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new AppError(401, "Invalid or expired token", error);
|
throw new AppError(401, "Invalid or expired token", error);
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/modules/auth/controllers/logout.controller.ts
Normal file
19
src/modules/auth/controllers/logout.controller.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
import { logoutService } from "../services/http/logout.service";
|
||||||
|
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const logoutController = async (ctx: Context) => {
|
||||||
|
try {
|
||||||
|
const jwtToken = ctx.cookie.auth_token?.value;
|
||||||
|
const serviceResponse = await logoutService(jwtToken);
|
||||||
|
return returnWriteResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"Logout successful",
|
||||||
|
serviceResponse,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -6,6 +6,7 @@ import { googleCallbackController } from "./controllers/googleCallback.controlle
|
|||||||
import { getOauthProvidersController } from "./controllers/getOauthProviders.controller";
|
import { getOauthProvidersController } from "./controllers/getOauthProviders.controller";
|
||||||
import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller";
|
import { getCallbackProviderUrlController } from "./controllers/getCallbackProviderUrl.controller";
|
||||||
import { tokenValidationController } from "./controllers/tokenValidation.controller";
|
import { tokenValidationController } from "./controllers/tokenValidation.controller";
|
||||||
|
import { logoutController } from "./controllers/logout.controller";
|
||||||
|
|
||||||
export const authModule = new Elysia({ prefix: "/auth" })
|
export const authModule = new Elysia({ prefix: "/auth" })
|
||||||
.post("/token/validate", tokenValidationController)
|
.post("/token/validate", tokenValidationController)
|
||||||
@ -14,4 +15,5 @@ export const authModule = new Elysia({ prefix: "/auth" })
|
|||||||
.get("/github", githubRequestController)
|
.get("/github", githubRequestController)
|
||||||
.get("/github/callback", githubCallbackController)
|
.get("/github/callback", githubCallbackController)
|
||||||
.get("/google", googleRequestController)
|
.get("/google", googleRequestController)
|
||||||
.get("/google/callback", googleCallbackController);
|
.get("/google/callback", googleCallbackController)
|
||||||
|
.post("/logout", logoutController);
|
||||||
|
|||||||
14
src/modules/auth/services/http/logout.service.ts
Normal file
14
src/modules/auth/services/http/logout.service.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||||
|
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
||||||
|
|
||||||
|
export const logoutService = async (jwtToken?: any) => {
|
||||||
|
try {
|
||||||
|
if (!jwtToken) throw new AppError(403, "No auth token provided");
|
||||||
|
|
||||||
|
const jwtPayload = jwtDecode(jwtToken);
|
||||||
|
return jwtPayload;
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user