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

@ -5,6 +5,7 @@ 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,
@ -14,6 +15,9 @@ export const loginWithPasswordService = async (
// search for user data using an identifier (username or email)
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
if (!(await bcrypt.compare(request.password, userData.password)))
throw new AppError(401, "Password incorrect");
@ -26,9 +30,8 @@ export const loginWithPasswordService = async (
// create JWT token that contain user session
const jwtToken = jwtEncode(userSession);
return jwtToken;
} catch (error) {
throw error;
ErrorForwarder(error);
}
};