add:module:auth:*logout | add logout module and clean all session in system
This commit is contained in:
31
src/modules/auth/controller/logout.controller.ts
Normal file
31
src/modules/auth/controller/logout.controller.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Context } from "elysia";
|
||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
import { clearCookies } from "../../../helpers/http/userHeader/cookies/clearCookies";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { logoutService } from "../services/logout.service";
|
||||
|
||||
export const logoutController = async (ctx: Context) => {
|
||||
try {
|
||||
const userCookie = getCookie(ctx);
|
||||
if (!userCookie || !userCookie.auth_token) {
|
||||
return returnErrorResponse(ctx.set, 401, "You're not logged in yet");
|
||||
}
|
||||
|
||||
const clearSession = logoutService(userCookie.auth_token);
|
||||
|
||||
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
200,
|
||||
"Successfully logged out",
|
||||
clearSession
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
@ -2,9 +2,11 @@ import Elysia from "elysia";
|
||||
import { loginWithPassword } from "./controller/loginWithPassword.controller";
|
||||
import { authMiddleware } from "../../middleware/auth.middleware";
|
||||
import { authVerification } from "./controller/authVerification.controller";
|
||||
import { logoutController } from "./controller/logout.controller";
|
||||
|
||||
export const authModule = new Elysia({ prefix: "/auth" })
|
||||
.post("/legacy", loginWithPassword)
|
||||
.post("/verification", authVerification, {
|
||||
beforeHandle: authMiddleware,
|
||||
});
|
||||
})
|
||||
.post("/logout", logoutController);
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
|
||||
1
src/modules/auth/services/loginFromSystem.service.ts
Normal file
1
src/modules/auth/services/loginFromSystem.service.ts
Normal file
@ -0,0 +1 @@
|
||||
export const loginFromSystemService = (userId: string) => {};
|
||||
15
src/modules/auth/services/logout.service.ts
Normal file
15
src/modules/auth/services/logout.service.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
import { deleteUserSessionInCacheAndDBService } from "../../userSession/services/deleteUserSessionInCacheAndDB.service";
|
||||
|
||||
export const logoutService = async (userCookie: string) => {
|
||||
try {
|
||||
const jwtToken = jwtDecode(userCookie);
|
||||
const deleteUserSessionInCacheAndDB =
|
||||
deleteUserSessionInCacheAndDBService(jwtToken);
|
||||
|
||||
return deleteUserSessionInCacheAndDB;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error, 500, "Logout service had encountered error");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user