add userSession module
This commit is contained in:
@ -14,7 +14,8 @@
|
|||||||
"cookie": "^1.0.2",
|
"cookie": "^1.0.2",
|
||||||
"elysia": "latest",
|
"elysia": "latest",
|
||||||
"joi": "^17.13.3",
|
"joi": "^17.13.3",
|
||||||
"jsonwebtoken": "^9.0.2"
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"ua-parser-js": "^2.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bun-types": "latest",
|
"bun-types": "latest",
|
||||||
|
|||||||
@ -0,0 +1,26 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { UAParser } from "ua-parser-js";
|
||||||
|
import { UserHeaderInformation } from "./types";
|
||||||
|
|
||||||
|
export const getUserHeaderInformation = (
|
||||||
|
ctx: Context
|
||||||
|
): UserHeaderInformation => {
|
||||||
|
const headers = ctx.request.headers;
|
||||||
|
const userAgentHeader = headers.get("user-agent") || "desktop";
|
||||||
|
const userAgent = new UAParser(userAgentHeader);
|
||||||
|
|
||||||
|
const userIP =
|
||||||
|
headers.get("cf-connecting-ip") ||
|
||||||
|
headers.get("x-real-ip") ||
|
||||||
|
headers.get("x-forwarded-for")?.split(",")[0] ||
|
||||||
|
"Unknown IP";
|
||||||
|
|
||||||
|
const userHeaderInformation = {
|
||||||
|
ip: userIP,
|
||||||
|
deviceType: userAgent.getDevice().type || "desktop",
|
||||||
|
deviceOS: userAgent.getOS().name + " " + userAgent.getOS().version,
|
||||||
|
browser: userAgent.getBrowser().name + " " + userAgent.getBrowser().version,
|
||||||
|
};
|
||||||
|
|
||||||
|
return userHeaderInformation;
|
||||||
|
};
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
export interface UserHeaderInformation {
|
||||||
|
ip: string;
|
||||||
|
deviceType: string;
|
||||||
|
deviceOS: string;
|
||||||
|
browser: string;
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { createUserSessionService } from "../services/createUserSession.service";
|
||||||
|
import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation";
|
||||||
|
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
|
||||||
|
import {
|
||||||
|
returnErrorResponse,
|
||||||
|
returnWriteResponse,
|
||||||
|
} from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const createUserSessionRole = async (
|
||||||
|
ctx: Context & { body: { userId: string } }
|
||||||
|
) => {
|
||||||
|
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) {
|
||||||
|
const { status, message, details } = handlePrismaError(error);
|
||||||
|
return returnErrorResponse(ctx.set, status, message, details);
|
||||||
|
}
|
||||||
|
};
|
||||||
7
src/modules/userSession/index.ts
Normal file
7
src/modules/userSession/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import Elysia from "elysia";
|
||||||
|
import { createUserSessionRole } from "./controller/createUserSession.controller";
|
||||||
|
|
||||||
|
export const userSessionModule = new Elysia({ prefix: "/user-sessions" }).post(
|
||||||
|
"/",
|
||||||
|
createUserSessionRole
|
||||||
|
);
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
import { createUserSessionServiceParams } from "../userSession.types";
|
||||||
|
import { createUserSessionRepo } from "../userSession.repository";
|
||||||
|
|
||||||
|
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),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
3
src/modules/userSession/userSession.model.ts
Normal file
3
src/modules/userSession/userSession.model.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { prisma } from "../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const userSessionModel = prisma.userSession;
|
||||||
25
src/modules/userSession/userSession.repository.ts
Normal file
25
src/modules/userSession/userSession.repository.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { userSessionModel } from "./userSession.model";
|
||||||
|
|
||||||
|
export const createUserSessionRepo = async (
|
||||||
|
data: Prisma.UserSessionUncheckedCreateInput
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const newUserRole = await userSessionModel.create({
|
||||||
|
data: data,
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
omit: {
|
||||||
|
password: true,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
roles: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return newUserRole;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
0
src/modules/userSession/userSession.schema.ts
Normal file
0
src/modules/userSession/userSession.schema.ts
Normal file
6
src/modules/userSession/userSession.types.ts
Normal file
6
src/modules/userSession/userSession.types.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { UserHeaderInformation } from "../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
||||||
|
|
||||||
|
export interface createUserSessionServiceParams {
|
||||||
|
userId: string;
|
||||||
|
userHeaderInformation: UserHeaderInformation;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user