🚚 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,27 @@
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { Context } from "elysia";
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
import { authVerificationService } from "../services/authVerification.service";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { clearCookies } from "../../../helpers/http/userHeader/cookies/clearCookies";
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
export const authVerification = async (ctx: Context) => {
try {
// Get the auth token from cookies
const cookie = getCookie(ctx);
if (!cookie.auth_token)
return returnErrorResponse(ctx.set, 401, "Auth token not found");
// Verify the auth token and get the user session
const authService = await authVerificationService(cookie.auth_token);
return returnWriteResponse(ctx.set, 200, "User authenticated", authService);
} catch (error) {
// If token is invalid or expired, clear the auth cookie and return an error response
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,74 @@
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { Context } from "elysia";
import { loginWithPasswordService } from "../services/loginWithPassword.service";
import { LoginWithPasswordRequest } from "../auth.types";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
import { loginWithPasswordSchema } from "../schemas/loginWithPassword";
/**
* @function loginWithPassword
* @description Authenticates user using username/email and password.
* On successful login, sets JWT token in cookies and returns token in response (development only).
* In production environment, only sets cookie without returning token in response body.
*
* @param {Context & { body: LoginWithPasswordRequest }} ctx - The context object containing request information.
* @param {Object} ctx.body - The login credentials.
*
* @returns {Promise<Object>} A response object indicating authentication success or failure.
* @throws {Object} An error response if validation fails or authentication error occurs.
*
* @example
* Request route: POST /auth/legacy
* Request body:
* {
* "identifier": "user@example.com" or "username123",
* "password": "securePassword123"
* }
*
* Success Response:
* Status: 200 OK
* Development:
* {
* "message": "Authentication Success",
* "token": "<JWT_TOKEN>" // Only in development environment
* }
*
* Failure Responses:
* - 400 Bad Request: Invalid user input or missing fields
* - 401 Unauthorized: Invalid credentials
* - 500 Internal Server Error: Server error during authentication
*/
export const loginWithPassword = async (
ctx: Context & { body: LoginWithPasswordRequest }
) => {
// Validate the request body against the schema
const { error } = loginWithPasswordSchema.validate(ctx.body);
if (error || !ctx.body)
return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
// Extract user header information
const userHeaderInfo = getUserHeaderInformation(ctx);
try {
// Call the service to handle login with password
const jwtToken = await loginWithPasswordService(ctx.body, userHeaderInfo);
// Set the authentication cookie with the JWT token
setCookie(ctx.set, COOKIE_KEYS.AUTH, jwtToken);
return returnWriteResponse(
ctx.set,
200,
"Authentication Success",
jwtToken
);
} catch (error) {
// Handle any errors that occur during the login process
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,65 @@
import { Context } from "elysia";
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
import { clearCookies } from "../../../helpers/http/userHeader/cookies/clearCookies";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { logoutService } from "../services/logout.service";
/**
* @function logoutController
* @description Handles user logout by clearing authentication token from cookies.
* Requires valid active session (auth token in cookies).
* In development environment, returns additional session clearing details.
*
* @param {Context} ctx - The context object containing request information.
*
* @returns {Promise<Object>} A response object indicating logout success or failure.
* @throws {Object} An error response if no active session found or server error occurs.
*
* @example
* Request route: POST /logout
* Request headers:
* {
* "Cookie": "auth_token=<JWT_TOKEN>"
* }
*
* Success Response:
* Status: 200 OK
* {
* "message": "Successfully logged out",
* "data": { ...clearSessionDetails } // Only in development environment
* }
*
* Failure Responses:
* - 401 Unauthorized: No active session found (not logged in)
* - 500 Internal Server Error: Server error during logout process
*/
export const logoutController = async (ctx: Context) => {
try {
// Get the user cookie from the request, if not found, return an error
const userCookie = getCookie(ctx);
if (!userCookie || !userCookie.auth_token) {
return returnErrorResponse(ctx.set, 401, "You're not logged in yet");
}
// Call the logout service to clear the user session
const clearSession = logoutService(userCookie.auth_token);
// Clear the auth cookie from the user session
clearCookies(ctx.set, [COOKIE_KEYS.AUTH]);
return returnWriteResponse(
ctx.set,
200,
"Successfully logged out",
clearSession
);
// If there's an error during the logout process, handle it
} catch (error) {
return mainErrorHandler(ctx.set, error);
}
};