🚚 create backup folder

create backup folder for archive the old modules
This commit is contained in:
Rafi Arrafif
2025-07-18 23:20:15 +07:00
parent 8eb68cf0ba
commit 8532d7e104
40 changed files with 671 additions and 671 deletions

View File

@ -0,0 +1,44 @@
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 { getUserSessionFromDBService } from "../../userSession/services/getUserSessionFromDB.service";
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
import { JWTSessionPayload } from "../auth.types";
export const authVerificationService = async (cookie: string) => {
try {
// 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 checkUserSessionInCacheService(
jwtSession.userId,
jwtSession.id
);
if (!sessionCheckOnRedis) {
// If not found in Redis, check the database
const sessionCheckOnDB = await getUserSessionFromDBService(jwtSession.id);
// If the session found in the database, store it in Redis. if not, throw an error
if (!sessionCheckOnDB) {
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 {
// If the session is found in Redis, return it
return jwtSession;
}
} catch (error) {
ErrorForwarder(error, 401, "Token is invalid");
}
};

View File

@ -0,0 +1,18 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
import { createUserSessionService } from "../../userSession/services/createUserSession.service";
export const loginFromSystemService = async (
userId: string,
userHeaderInfo: UserHeaderInformation
) => {
try {
const userSession = await createUserSessionService({
userId,
userHeaderInformation: userHeaderInfo,
});
return userSession;
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,40 @@
import bcrypt from "bcrypt";
import { findUserByEmailOrUsernameService } from "../../user/services/getUserData.service";
import { LoginWithPasswordRequest } from "../auth.types";
import { AppError } from "../../../helpers/error/instances/app";
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
import { createUserSessionService } from "../../userSession/services/createUserSession.service";
import { jwtEncode } from "../../../helpers/http/jwt/encode";
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
export const loginWithPasswordService = async (
request: LoginWithPasswordRequest,
userHeaderInfo: UserHeaderInformation
) => {
try {
// search for user data using an identifier (username or email)
const userData = await findUserByEmailOrUsernameService(
request.identifier,
{ verbose: true }
);
// if user data is not found, throw an error
if (!userData) throw new AppError(404, "User not found");
// validate the password in the request with the existing one
if (!(await bcrypt.compare(request.password, userData.password)))
throw new AppError(401, "Password incorrect");
// create new user session
const userSession = await createUserSessionService({
userId: userData.id,
userHeaderInformation: userHeaderInfo,
});
// create JWT token that contain user session
const jwtToken = jwtEncode(userSession);
return jwtToken;
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,19 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { jwtDecode } from "../../../helpers/http/jwt/decode";
import { deleteUserSessionInCacheAndDBService } from "../../userSession/services/deleteUserSessionInCacheAndDB.service";
export const logoutService = async (userCookie: string) => {
try {
// Decode the JWT token to get the user session
const jwtToken = jwtDecode(userCookie);
// Delete the user session from cache and database
const deleteUserSessionInCacheAndDB =
deleteUserSessionInCacheAndDBService(jwtToken);
return deleteUserSessionInCacheAndDB;
// If the session was not found in the cache or database, throw an error
} catch (error) {
ErrorForwarder(error, 500, "Logout service had encountered error");
}
};