feat/logout #17
@ -3,6 +3,7 @@ import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
|||||||
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
||||||
import { redis } from "../../../../utils/databases/redis/connection";
|
import { redis } from "../../../../utils/databases/redis/connection";
|
||||||
import { checkUserSessionInDBService } from "../../../userSession/services/internal/checkUserSessionInDB.service";
|
import { checkUserSessionInDBService } from "../../../userSession/services/internal/checkUserSessionInDB.service";
|
||||||
|
import { createUserSessionInRedisService } from "../../../userSession/services/internal/createUserSessionInRedis.service";
|
||||||
|
|
||||||
export const tokenValidationService = async (payload: string) => {
|
export const tokenValidationService = async (payload: string) => {
|
||||||
try {
|
try {
|
||||||
@ -20,13 +21,11 @@ export const tokenValidationService = async (payload: string) => {
|
|||||||
const dbValidationResult = await checkUserSessionInDBService(decoded.id);
|
const dbValidationResult = await checkUserSessionInDBService(decoded.id);
|
||||||
if (!dbValidationResult)
|
if (!dbValidationResult)
|
||||||
throw new AppError(403, "Unauthorized: Invalid session");
|
throw new AppError(403, "Unauthorized: Invalid session");
|
||||||
const createRedisKey = `${process.env.APP_NAME}:users:${decoded.user.id}:sessions:${decoded.id}`;
|
await createUserSessionInRedisService({
|
||||||
await redis.hset(createRedisKey, {
|
|
||||||
userId: decoded.user.id,
|
userId: decoded.user.id,
|
||||||
sessionId: decoded.id,
|
sessionId: decoded.id,
|
||||||
validUntil: decoded.validUntil,
|
validUntil: decoded.validUntil,
|
||||||
});
|
});
|
||||||
await redis.expire(createRedisKey, Number(process.env.SESSION_EXPIRE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return decoded;
|
return decoded;
|
||||||
|
|||||||
@ -4,10 +4,11 @@ import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
|||||||
import { createUserSessionRepository } from "../repositories/createUserSession.repository";
|
import { createUserSessionRepository } from "../repositories/createUserSession.repository";
|
||||||
import { redis } from "../../../utils/databases/redis/connection";
|
import { redis } from "../../../utils/databases/redis/connection";
|
||||||
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||||
|
import { createUserSessionInRedisService } from "./internal/createUserSessionInRedis.service";
|
||||||
|
|
||||||
export const createUserSessionService = async (
|
export const createUserSessionService = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
userHeaderInfo: UserHeaderInformation
|
userHeaderInfo: UserHeaderInformation,
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
// set the date when the token will expire
|
// set the date when the token will expire
|
||||||
@ -29,13 +30,11 @@ export const createUserSessionService = async (
|
|||||||
const createUserSession = await createUserSessionRepository(constructData);
|
const createUserSession = await createUserSessionRepository(constructData);
|
||||||
|
|
||||||
// caching user session data into Redis
|
// caching user session data into Redis
|
||||||
const createRedisKey = `${process.env.APP_NAME}:users:${userId}:sessions:${createUserSession.id}`;
|
await createUserSessionInRedisService({
|
||||||
await redis.hset(createRedisKey, {
|
|
||||||
userId,
|
userId,
|
||||||
sessionId: createUserSession.id,
|
sessionId: createUserSession.id,
|
||||||
validUntil: createUserSession.validUntil,
|
validUntil: createUserSession.validUntil,
|
||||||
});
|
});
|
||||||
await redis.expire(createRedisKey, Number(process.env.SESSION_EXPIRE));
|
|
||||||
|
|
||||||
// create a jwt token with a payload containing the created user session, then return jwt
|
// create a jwt token with a payload containing the created user session, then return jwt
|
||||||
return jwtEncode(createUserSession);
|
return jwtEncode(createUserSession);
|
||||||
|
|||||||
@ -0,0 +1,30 @@
|
|||||||
|
import { AppError } from "../../../../helpers/error/instances/app";
|
||||||
|
import { redis } from "../../../../utils/databases/redis/connection";
|
||||||
|
|
||||||
|
export const createUserSessionInRedisService = async ({
|
||||||
|
userId,
|
||||||
|
sessionId,
|
||||||
|
validUntil,
|
||||||
|
exp,
|
||||||
|
}: {
|
||||||
|
userId: string;
|
||||||
|
sessionId: string;
|
||||||
|
validUntil?: Date;
|
||||||
|
exp?: number;
|
||||||
|
}) => {
|
||||||
|
try {
|
||||||
|
const createRedisKey = `${process.env.APP_NAME}:users:${userId}:sessions:${sessionId}`;
|
||||||
|
await redis.hset(createRedisKey, {
|
||||||
|
userId,
|
||||||
|
sessionId,
|
||||||
|
validUntil: validUntil,
|
||||||
|
});
|
||||||
|
await redis.expire(
|
||||||
|
createRedisKey,
|
||||||
|
exp || Number(process.env.SESSION_CACHE_EXPIRE!),
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Error creating user session in Redis", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user