complete: complete login process

This commit is contained in:
rafiarrafif
2025-05-27 23:06:37 +07:00
parent 90302eab6c
commit ab84abf366
5 changed files with 50 additions and 32 deletions

View File

@ -1,31 +1,41 @@
import { Context } from "elysia";
import { loginWithPasswordService } from "../services/loginWithPassword.service";
import { loginWithPasswordSchema } from "../auth.schema";
import { import {
returnErrorResponse, returnErrorResponse,
returnWriteResponse, returnWriteResponse,
} from "../../../helpers/callback/httpResponse"; } from "../../../helpers/callback/httpResponse";
import { Context } from "elysia";
import { loginWithPasswordService } from "../services/loginWithPassword.service";
import { LoginWithPasswordRequest } from "../auth.types"; import { LoginWithPasswordRequest } from "../auth.types";
import { mainErrorHandler } from "../../../helpers/error/handler"; import { mainErrorHandler } from "../../../helpers/error/handler";
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation"; import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies"; import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
import { COOKIE_KEYS } from "../../../constants/cookie.keys"; import { COOKIE_KEYS } from "../../../constants/cookie.keys";
import { loginWithPasswordSchema } from "../schemas/loginWithPassword";
export const loginWithPassword = async ( export const loginWithPassword = async (
ctx: Context & { body: LoginWithPasswordRequest } ctx: Context & { body: LoginWithPasswordRequest }
) => { ) => {
// Validate the request body against the schema
const { error } = loginWithPasswordSchema.validate(ctx.body); const { error } = loginWithPasswordSchema.validate(ctx.body);
if (error || !ctx.body) if (error || !ctx.body)
return returnErrorResponse(ctx.set, 400, "Invalid user input", error); return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
// Extract user header information
const userHeaderInfo = getUserHeaderInformation(ctx); const userHeaderInfo = getUserHeaderInformation(ctx);
try { try {
// Call the service to handle login with password
const jwtToken = await loginWithPasswordService(ctx.body, userHeaderInfo); const jwtToken = await loginWithPasswordService(ctx.body, userHeaderInfo);
// Set the authentication cookie with the JWT token
const cookie = setCookie(ctx.set, COOKIE_KEYS.AUTH, jwtToken); const cookie = setCookie(ctx.set, COOKIE_KEYS.AUTH, jwtToken);
return returnWriteResponse(ctx.set, 200, "Authentication Success", cookie); return returnWriteResponse(
ctx.set,
200,
"Authentication Success",
jwtToken
);
} catch (error) { } catch (error) {
// Handle any errors that occur during the login process
return mainErrorHandler(ctx.set, error); return mainErrorHandler(ctx.set, error);
} }
}; };

View File

@ -5,6 +5,7 @@ import { AppError } from "../../../helpers/error/instances/app";
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types"; import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
import { createUserSessionService } from "../../userSession/services/createUserSession.service"; import { createUserSessionService } from "../../userSession/services/createUserSession.service";
import { jwtEncode } from "../../../helpers/http/jwt/encode"; import { jwtEncode } from "../../../helpers/http/jwt/encode";
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
export const loginWithPasswordService = async ( export const loginWithPasswordService = async (
request: LoginWithPasswordRequest, request: LoginWithPasswordRequest,
@ -14,6 +15,9 @@ export const loginWithPasswordService = async (
// search for user data using an identifier (username or email) // search for user data using an identifier (username or email)
const userData = await findUserByEmailOrUsernameService(request.identifier); const userData = await findUserByEmailOrUsernameService(request.identifier);
// 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 // validate the password in the request with the existing one
if (!(await bcrypt.compare(request.password, userData.password))) if (!(await bcrypt.compare(request.password, userData.password)))
throw new AppError(401, "Password incorrect"); throw new AppError(401, "Password incorrect");
@ -26,9 +30,8 @@ export const loginWithPasswordService = async (
// create JWT token that contain user session // create JWT token that contain user session
const jwtToken = jwtEncode(userSession); const jwtToken = jwtEncode(userSession);
return jwtToken; return jwtToken;
} catch (error) { } catch (error) {
throw error; ErrorForwarder(error);
} }
}; };

View File

@ -2,6 +2,7 @@ import { AppError } from "../../../helpers/error/instances/app";
import { userModel } from "../user.model"; import { userModel } from "../user.model";
export const findUserByEmailOrUsernameRepo = async (identifier: string) => { export const findUserByEmailOrUsernameRepo = async (identifier: string) => {
try {
const userData = const userData =
(await userModel.findUnique({ (await userModel.findUnique({
where: { email: identifier }, where: { email: identifier },
@ -30,6 +31,9 @@ export const findUserByEmailOrUsernameRepo = async (identifier: string) => {
}, },
})); }));
if (!userData) throw new AppError(404, "User not exist"); if (!userData) return false;
return userData; return userData;
} catch (error) {
throw new AppError(500, "Database error", error);
}
}; };

View File

@ -1,3 +1,4 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { findUserByEmailOrUsernameRepo } from "../repositories/findUserByEmailOrUsername.repository"; import { findUserByEmailOrUsernameRepo } from "../repositories/findUserByEmailOrUsername.repository";
export const findUserByEmailOrUsernameService = async (identifier: string) => { export const findUserByEmailOrUsernameService = async (identifier: string) => {
@ -5,6 +6,6 @@ export const findUserByEmailOrUsernameService = async (identifier: string) => {
const userData = await findUserByEmailOrUsernameRepo(identifier); const userData = await findUserByEmailOrUsernameRepo(identifier);
return userData; return userData;
} catch (error) { } catch (error) {
throw error; ErrorForwarder(error);
} }
}; };