add default value in forwarder error
This commit is contained in:
@ -11,13 +11,16 @@ import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||
|
||||
export const authVerification = async (ctx: Context) => {
|
||||
try {
|
||||
// Get the auth token from cookies
|
||||
const cookie = getCookie(ctx);
|
||||
if (!cookie.auth_token)
|
||||
return returnErrorResponse(ctx.set, 401, "Auth token not found");
|
||||
|
||||
// Verify the auth token and get the user session
|
||||
const authService = await authVerificationService(cookie.auth_token);
|
||||
return returnWriteResponse(ctx.set, 200, "User authenticated", authService);
|
||||
} catch (error) {
|
||||
// If token is invalid or expired, clear the auth cookie and return an error response
|
||||
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
import { checkUserSessionInCacheService } from "../../userSession/services/checkUserSessionInCache.service";
|
||||
import { getUserSessionService } from "../../userSession/services/getUserSession.service";
|
||||
import { getUserSessionFromDBService } from "../../userSession/services/getUserSessionFromDB.service";
|
||||
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
|
||||
import { JWTSessionPayload } from "../auth.types";
|
||||
|
||||
@ -18,14 +19,10 @@ export const authVerificationService = async (cookie: string) => {
|
||||
|
||||
if (!sessionCheckOnRedis) {
|
||||
// If not found in Redis, check the database
|
||||
const sessionCheckOnDB = await getUserSessionService(jwtSession.id);
|
||||
const sessionCheckOnDB = await getUserSessionFromDBService(jwtSession.id);
|
||||
|
||||
// If the session found in the database, store it in Redis. if not, throw an error
|
||||
if (
|
||||
!sessionCheckOnDB ||
|
||||
!sessionCheckOnDB.isAuthenticated ||
|
||||
new Date(sessionCheckOnDB.validUntil) < new Date()
|
||||
) {
|
||||
if (!sessionCheckOnDB) {
|
||||
throw new AppError(401, "Session invalid or expired");
|
||||
} else {
|
||||
// Store the session in Redis with the remaining time until expiration
|
||||
@ -38,9 +35,10 @@ export const authVerificationService = async (cookie: string) => {
|
||||
return sessionCheckOnDB;
|
||||
}
|
||||
} else {
|
||||
// If the session is found in Redis, return it
|
||||
return jwtSession;
|
||||
}
|
||||
} catch (error) {
|
||||
throw new AppError(401, "Token is invalid", error);
|
||||
ErrorForwarder(error, 401, "Token is invalid");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user