edit:module:auth:*logout | add single line documentation for logout module
This commit is contained in:
@ -11,13 +11,16 @@ import { logoutService } from "../services/logout.service";
|
|||||||
|
|
||||||
export const logoutController = async (ctx: Context) => {
|
export const logoutController = async (ctx: Context) => {
|
||||||
try {
|
try {
|
||||||
|
// Get the user cookie from the request, if not found, return an error
|
||||||
const userCookie = getCookie(ctx);
|
const userCookie = getCookie(ctx);
|
||||||
if (!userCookie || !userCookie.auth_token) {
|
if (!userCookie || !userCookie.auth_token) {
|
||||||
return returnErrorResponse(ctx.set, 401, "You're not logged in yet");
|
return returnErrorResponse(ctx.set, 401, "You're not logged in yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call the logout service to clear the user session
|
||||||
const clearSession = logoutService(userCookie.auth_token);
|
const clearSession = logoutService(userCookie.auth_token);
|
||||||
|
|
||||||
|
// Clear the auth cookie from the user session
|
||||||
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
|
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
|
||||||
return returnWriteResponse(
|
return returnWriteResponse(
|
||||||
ctx.set,
|
ctx.set,
|
||||||
@ -25,6 +28,8 @@ export const logoutController = async (ctx: Context) => {
|
|||||||
"Successfully logged out",
|
"Successfully logged out",
|
||||||
clearSession
|
clearSession
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If there's an error during the logout process, handle it
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,15 @@ import { deleteUserSessionInCacheAndDBService } from "../../userSession/services
|
|||||||
|
|
||||||
export const logoutService = async (userCookie: string) => {
|
export const logoutService = async (userCookie: string) => {
|
||||||
try {
|
try {
|
||||||
|
// Decode the JWT token to get the user session
|
||||||
const jwtToken = jwtDecode(userCookie);
|
const jwtToken = jwtDecode(userCookie);
|
||||||
|
|
||||||
|
// Delete the user session from cache and database
|
||||||
const deleteUserSessionInCacheAndDB =
|
const deleteUserSessionInCacheAndDB =
|
||||||
deleteUserSessionInCacheAndDBService(jwtToken);
|
deleteUserSessionInCacheAndDBService(jwtToken);
|
||||||
|
|
||||||
return deleteUserSessionInCacheAndDB;
|
return deleteUserSessionInCacheAndDB;
|
||||||
|
|
||||||
|
// If the session was not found in the cache or database, throw an error
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(error, 500, "Logout service had encountered error");
|
ErrorForwarder(error, 500, "Logout service had encountered error");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,14 +7,19 @@ export const deleteUserSessionInCacheAndDBService = async (
|
|||||||
jwtToken: JWTAuthToken
|
jwtToken: JWTAuthToken
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
|
// Construct the userId and sessionId from the JWT token
|
||||||
const userId = jwtToken.userId;
|
const userId = jwtToken.userId;
|
||||||
const sessionId = jwtToken.id;
|
const sessionId = jwtToken.id;
|
||||||
|
|
||||||
|
// Delete the user session from cache and database
|
||||||
await deleteUserSessionFromCacheRepo(userId, sessionId);
|
await deleteUserSessionFromCacheRepo(userId, sessionId);
|
||||||
const deleteUserSessionFromDB = await deleteUserSessionFromDBRepo(
|
const deleteUserSessionFromDB = await deleteUserSessionFromDBRepo(
|
||||||
sessionId
|
sessionId
|
||||||
);
|
);
|
||||||
|
|
||||||
return deleteUserSessionFromDB;
|
return deleteUserSessionFromDB;
|
||||||
|
|
||||||
|
// If the session was not found in the cache or database, throw an error
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ErrorForwarder(
|
ErrorForwarder(
|
||||||
error,
|
error,
|
||||||
|
|||||||
Reference in New Issue
Block a user