🚚 create backup folder
create backup folder for archive the old modules
This commit is contained in:
44
backup/modules/auth/services/authVerification.service.ts
Normal file
44
backup/modules/auth/services/authVerification.service.ts
Normal 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");
|
||||
}
|
||||
};
|
||||
18
backup/modules/auth/services/loginFromSystem.service.ts
Normal file
18
backup/modules/auth/services/loginFromSystem.service.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
40
backup/modules/auth/services/loginWithPassword.service.ts
Normal file
40
backup/modules/auth/services/loginWithPassword.service.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
19
backup/modules/auth/services/logout.service.ts
Normal file
19
backup/modules/auth/services/logout.service.ts
Normal 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");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user