add step in auth verification
This commit is contained in:
@ -2,3 +2,64 @@ export interface LoginWithPasswordRequest {
|
||||
identifier: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface JWTSessionPayload {
|
||||
id: string;
|
||||
isAuthenticated: boolean;
|
||||
userId: string;
|
||||
deviceType: string;
|
||||
deviceOs: string;
|
||||
deviceIp: string;
|
||||
isOnline: boolean;
|
||||
lastOnline: Date;
|
||||
validUntil: Date;
|
||||
deletedAt: null;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
user: User;
|
||||
iat: number;
|
||||
exp: number;
|
||||
}
|
||||
interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
username: string;
|
||||
email: string;
|
||||
birthDate: null;
|
||||
gender: null;
|
||||
phoneCC: null;
|
||||
phoneNumber: null;
|
||||
bioProfile: null;
|
||||
profilePicture: null;
|
||||
commentPicture: null;
|
||||
preferenceId: null;
|
||||
verifiedAt: null;
|
||||
disabledAt: null;
|
||||
deletedAt: null;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
roles: Role[];
|
||||
}
|
||||
interface Role {
|
||||
id: string;
|
||||
name: string;
|
||||
primaryColor: string;
|
||||
secondaryColor: string;
|
||||
pictureImage: string;
|
||||
badgeImage: null;
|
||||
isSuperadmin: boolean;
|
||||
canEditMedia: boolean;
|
||||
canManageMedia: boolean;
|
||||
canEditEpisodes: boolean;
|
||||
canManageEpisodes: boolean;
|
||||
canEditComment: boolean;
|
||||
canManageComment: boolean;
|
||||
canEditUser: boolean;
|
||||
canManageUser: boolean;
|
||||
canEditSystem: boolean;
|
||||
canManageSystem: boolean;
|
||||
createdBy: string;
|
||||
deletedAt: null;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
import { Context } from "elysia";
|
||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
import { authVerificationService } from "../services/authVerification.service";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { Context } from "elysia";
|
||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
import { authVerificationService } from "../services/authVerification.service";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { clearCookies } from "../../../helpers/http/userHeader/cookies/clearCookies";
|
||||
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||
|
||||
export const authVerification = async (ctx: Context) => {
|
||||
try {
|
||||
@ -13,9 +15,10 @@ export const authVerification = async (ctx: Context) => {
|
||||
if (!cookie.auth_token)
|
||||
return returnErrorResponse(ctx.set, 401, "Auth token not found");
|
||||
|
||||
const authService = authVerificationService(cookie.auth_token);
|
||||
const authService = await authVerificationService(cookie.auth_token);
|
||||
return returnWriteResponse(ctx.set, 200, "User authenticated", authService);
|
||||
} catch (error) {
|
||||
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -9,6 +9,7 @@ import { LoginWithPasswordRequest } from "../auth.types";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
|
||||
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||
|
||||
export const loginWithPassword = async (
|
||||
ctx: Context & { body: LoginWithPasswordRequest }
|
||||
@ -22,8 +23,8 @@ export const loginWithPassword = async (
|
||||
try {
|
||||
const jwtToken = await loginWithPasswordService(ctx.body, userHeaderInfo);
|
||||
|
||||
const cookie = setCookie(ctx.set, jwtToken);
|
||||
return returnWriteResponse(ctx.set, 200, "Autentication Success", cookie);
|
||||
const cookie = setCookie(ctx.set, COOKIE_KEYS.AUTH, jwtToken);
|
||||
return returnWriteResponse(ctx.set, 200, "Authentication Success", cookie);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
|
||||
@ -1,11 +1,47 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
import { storeUserSessionToCacheRepo } from "../../userSession/repositories/storeUserSessionToCache.repository";
|
||||
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
|
||||
import { JWTSessionPayload } from "../auth.types";
|
||||
|
||||
export const authVerificationService = (cookie: string) => {
|
||||
export const authVerificationService = async (cookie: string) => {
|
||||
try {
|
||||
const userToken = jwtDecode(cookie);
|
||||
return userToken;
|
||||
// Decode the JWT token to get the session payload
|
||||
const jwtSession = jwtDecode(cookie) as JWTSessionPayload;
|
||||
|
||||
// Check if the session exists in Redis
|
||||
const sessionCheckOnRedis = await redis.exists(jwtSession.id);
|
||||
if (!sessionCheckOnRedis) {
|
||||
// If not found in Redis, check the database
|
||||
const sessionCheckOnDB = await prisma.userSession.findUnique({
|
||||
where: {
|
||||
id: 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()
|
||||
) {
|
||||
throw new AppError(401, "Session invalid or expired");
|
||||
} else {
|
||||
// Store the session in Redis with the remaining time until expiration
|
||||
const timeExpires = Math.floor(
|
||||
(new Date(sessionCheckOnDB.validUntil).getTime() -
|
||||
new Date().getTime()) /
|
||||
1000
|
||||
);
|
||||
await storeUserSessionToCacheService(sessionCheckOnDB!, timeExpires);
|
||||
return sessionCheckOnDB;
|
||||
}
|
||||
} else {
|
||||
return jwtSession;
|
||||
}
|
||||
} catch (error) {
|
||||
throw new AppError(401, "Token is invalid");
|
||||
throw new AppError(401, "Token is invalid", error);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user