fix: fix.env.example
This commit is contained in:
@ -1,35 +1,35 @@
|
||||
import { Context } from "elysia";
|
||||
import { createUserSessionService } from "../services/createUserSession.service";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
|
||||
export const createUserSessionRole = async (
|
||||
ctx: Context & { body: { userId?: string } }
|
||||
) => {
|
||||
// Validate request body
|
||||
if (!ctx.body?.userId) {
|
||||
return returnErrorResponse(ctx.set, 400, "User ID is required");
|
||||
}
|
||||
|
||||
// Get user device and browser information
|
||||
const userHeaderData = getUserHeaderInformation(ctx);
|
||||
|
||||
try {
|
||||
const newUserSession = await createUserSessionService({
|
||||
userId: ctx.body.userId,
|
||||
userHeaderInformation: userHeaderData,
|
||||
});
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
201,
|
||||
"User session created",
|
||||
newUserSession
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
import { Context } from "elysia";
|
||||
import { createUserSessionService } from "../services/createUserSession.service";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
|
||||
export const createUserSessionRole = async (
|
||||
ctx: Context & { body: { userId?: string } }
|
||||
) => {
|
||||
// Validate request body
|
||||
if (!ctx.body?.userId) {
|
||||
return returnErrorResponse(ctx.set, 400, "User ID is required");
|
||||
}
|
||||
|
||||
// Get user device and browser information
|
||||
const userHeaderData = getUserHeaderInformation(ctx);
|
||||
|
||||
try {
|
||||
const newUserSession = await createUserSessionService({
|
||||
userId: ctx.body.userId,
|
||||
userHeaderInformation: userHeaderData,
|
||||
});
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
201,
|
||||
"User session created",
|
||||
newUserSession
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserSessionRole } from "./controllers/createUserSession.controller";
|
||||
|
||||
export const userSessionModule = new Elysia({ prefix: "/user-sessions" }).post(
|
||||
"/",
|
||||
createUserSessionRole
|
||||
);
|
||||
import Elysia from "elysia";
|
||||
import { createUserSessionRole } from "./controllers/createUserSession.controller";
|
||||
|
||||
export const userSessionModule = new Elysia({ prefix: "/user-sessions" }).post(
|
||||
"/",
|
||||
createUserSessionRole
|
||||
);
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
|
||||
export const checkUserSessionInCacheRepo = async (redisKeyName: string) => {
|
||||
try {
|
||||
const userSessionInRedis = await redis.exists(redisKeyName);
|
||||
if (!userSessionInRedis) return false;
|
||||
|
||||
return userSessionInRedis;
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Server cache error", error);
|
||||
}
|
||||
};
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
|
||||
export const checkUserSessionInCacheRepo = async (redisKeyName: string) => {
|
||||
try {
|
||||
const userSessionInRedis = await redis.exists(redisKeyName);
|
||||
if (!userSessionInRedis) return false;
|
||||
|
||||
return userSessionInRedis;
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Server cache error", error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,33 +1,33 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
|
||||
export const findUniqueUserSessionInDBRepo = async (identifier: string) => {
|
||||
try {
|
||||
const userSession = await prisma.userSession.findUnique({
|
||||
where: {
|
||||
id: identifier,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
deletedAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userSession) return false;
|
||||
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Database Error", error);
|
||||
}
|
||||
};
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
|
||||
export const findUniqueUserSessionInDBRepo = async (identifier: string) => {
|
||||
try {
|
||||
const userSession = await prisma.userSession.findUnique({
|
||||
where: {
|
||||
id: identifier,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
deletedAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userSession) return false;
|
||||
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Database Error", error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userSessionModel } from "../userSession.model";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
|
||||
export const createUserSessionRepo = async (
|
||||
data: Prisma.UserSessionUncheckedCreateInput
|
||||
) => {
|
||||
try {
|
||||
const newUserSession = await userSessionModel.create({
|
||||
data: data,
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
lastOnline: true,
|
||||
deletedAt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
return newUserSession;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userSessionModel } from "../userSession.model";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
|
||||
export const createUserSessionRepo = async (
|
||||
data: Prisma.UserSessionUncheckedCreateInput
|
||||
) => {
|
||||
try {
|
||||
const newUserSession = await userSessionModel.create({
|
||||
data: data,
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
lastOnline: true,
|
||||
deletedAt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
return newUserSession;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
|
||||
export const storeUserSessionToCacheRepo = async (
|
||||
userSession: Prisma.UserSessionUncheckedCreateInput,
|
||||
timeExpires: number
|
||||
) => {
|
||||
try {
|
||||
await redis.set(
|
||||
`${process.env.app_name}:users:${userSession.userId}:sessions:${userSession.id}`,
|
||||
String(userSession.validUntil),
|
||||
"EX",
|
||||
timeExpires
|
||||
);
|
||||
} catch (error) {
|
||||
throw new AppError(401, "Failed to store user session to cache");
|
||||
}
|
||||
};
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
|
||||
export const storeUserSessionToCacheRepo = async (
|
||||
userSession: Prisma.UserSessionUncheckedCreateInput,
|
||||
timeExpires: number
|
||||
) => {
|
||||
try {
|
||||
await redis.set(
|
||||
`${process.env.app_name}:users:${userSession.userId}:sessions:${userSession.id}`,
|
||||
String(userSession.validUntil),
|
||||
"EX",
|
||||
timeExpires
|
||||
);
|
||||
} catch (error) {
|
||||
throw new AppError(401, "Failed to store user session to cache");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { checkUserSessionInCacheRepo } from "../repositories/checkUserSessionInCache.repository";
|
||||
|
||||
export const checkUserSessionInCacheService = async (
|
||||
userId: string,
|
||||
sessionId: string
|
||||
) => {
|
||||
try {
|
||||
// Construct the Redis key name using the userId and sessionId
|
||||
const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`;
|
||||
|
||||
// Check if the user session exists in Redis
|
||||
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
|
||||
return userSessionInRedis;
|
||||
} catch (error) {
|
||||
// Forward the error with a 400 status code and a message
|
||||
ErrorForwarder(error, 400, "Bad Request");
|
||||
}
|
||||
};
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { checkUserSessionInCacheRepo } from "../repositories/checkUserSessionInCache.repository";
|
||||
|
||||
export const checkUserSessionInCacheService = async (
|
||||
userId: string,
|
||||
sessionId: string
|
||||
) => {
|
||||
try {
|
||||
// Construct the Redis key name using the userId and sessionId
|
||||
const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`;
|
||||
|
||||
// Check if the user session exists in Redis
|
||||
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
|
||||
return userSessionInRedis;
|
||||
} catch (error) {
|
||||
// Forward the error with a 400 status code and a message
|
||||
ErrorForwarder(error, 400, "Bad Request");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
import { createUserSessionServiceParams } from "../userSession.types";
|
||||
import { createUserSessionRepo } from "../repositories/insertUserSessionToDB.repository";
|
||||
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const createUserSessionService = async (
|
||||
data: createUserSessionServiceParams
|
||||
) => {
|
||||
const sessionLifetime = Number(process.env.SESSION_EXPIRE!);
|
||||
try {
|
||||
const newUserSession = await createUserSessionRepo({
|
||||
userId: data.userId,
|
||||
isAuthenticated: true,
|
||||
deviceType: data.userHeaderInformation.deviceType,
|
||||
deviceOs: data.userHeaderInformation.deviceOS,
|
||||
deviceIp: data.userHeaderInformation.ip,
|
||||
validUntil: new Date(new Date().getTime() + sessionLifetime * 1000),
|
||||
});
|
||||
|
||||
const timeExpires = Number(process.env.SESSION_EXPIRE!);
|
||||
await storeUserSessionToCacheRepo(newUserSession, timeExpires);
|
||||
|
||||
return newUserSession;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
import { createUserSessionServiceParams } from "../userSession.types";
|
||||
import { createUserSessionRepo } from "../repositories/insertUserSessionToDB.repository";
|
||||
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const createUserSessionService = async (
|
||||
data: createUserSessionServiceParams
|
||||
) => {
|
||||
const sessionLifetime = Number(process.env.SESSION_EXPIRE!);
|
||||
try {
|
||||
const newUserSession = await createUserSessionRepo({
|
||||
userId: data.userId,
|
||||
isAuthenticated: true,
|
||||
deviceType: data.userHeaderInformation.deviceType,
|
||||
deviceOs: data.userHeaderInformation.deviceOS,
|
||||
deviceIp: data.userHeaderInformation.ip,
|
||||
validUntil: new Date(new Date().getTime() + sessionLifetime * 1000),
|
||||
});
|
||||
|
||||
const timeExpires = Number(process.env.SESSION_EXPIRE!);
|
||||
await storeUserSessionToCacheRepo(newUserSession, timeExpires);
|
||||
|
||||
return newUserSession;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { findUniqueUserSessionInDBRepo } from "../repositories/findUniqueUserSessionInDB.repository";
|
||||
|
||||
export const getUserSessionFromDBService = async (identifier: string) => {
|
||||
try {
|
||||
// Check is session exists in DB
|
||||
const userSession = await findUniqueUserSessionInDBRepo(identifier);
|
||||
|
||||
// If session not found, return false
|
||||
if (
|
||||
!userSession ||
|
||||
!userSession.isAuthenticated ||
|
||||
new Date(userSession.validUntil) < new Date()
|
||||
)
|
||||
return false;
|
||||
|
||||
// If session found, return it
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
// If any DB error occurs, throw an AppError
|
||||
ErrorForwarder(error, 401, "Unable to get user session");
|
||||
}
|
||||
};
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { findUniqueUserSessionInDBRepo } from "../repositories/findUniqueUserSessionInDB.repository";
|
||||
|
||||
export const getUserSessionFromDBService = async (identifier: string) => {
|
||||
try {
|
||||
// Check is session exists in DB
|
||||
const userSession = await findUniqueUserSessionInDBRepo(identifier);
|
||||
|
||||
// If session not found, return false
|
||||
if (
|
||||
!userSession ||
|
||||
!userSession.isAuthenticated ||
|
||||
new Date(userSession.validUntil) < new Date()
|
||||
)
|
||||
return false;
|
||||
|
||||
// If session found, return it
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
// If any DB error occurs, throw an AppError
|
||||
ErrorForwarder(error, 401, "Unable to get user session");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const storeUserSessionToCacheService = async (
|
||||
userSession: Prisma.UserSessionUncheckedCreateInput,
|
||||
timeExpires: number
|
||||
) => {
|
||||
try {
|
||||
// Store user session in cache with expiration time
|
||||
await storeUserSessionToCacheRepo(userSession, timeExpires);
|
||||
return;
|
||||
} catch (error) {
|
||||
// If any error occurs while storing session in cache, throw an AppError
|
||||
ErrorForwarder(error, 401, "Failed to store user session to cache");
|
||||
}
|
||||
};
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const storeUserSessionToCacheService = async (
|
||||
userSession: Prisma.UserSessionUncheckedCreateInput,
|
||||
timeExpires: number
|
||||
) => {
|
||||
try {
|
||||
// Store user session in cache with expiration time
|
||||
await storeUserSessionToCacheRepo(userSession, timeExpires);
|
||||
return;
|
||||
} catch (error) {
|
||||
// If any error occurs while storing session in cache, throw an AppError
|
||||
ErrorForwarder(error, 401, "Failed to store user session to cache");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const userSessionModel = prisma.userSession;
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const userSessionModel = prisma.userSession;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { UserHeaderInformation } from "../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||
|
||||
export interface createUserSessionServiceParams {
|
||||
userId: string;
|
||||
userHeaderInformation: UserHeaderInformation;
|
||||
}
|
||||
import { UserHeaderInformation } from "../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||
|
||||
export interface createUserSessionServiceParams {
|
||||
userId: string;
|
||||
userHeaderInformation: UserHeaderInformation;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user