complete: complete login process
This commit is contained in:
@ -1,31 +1,41 @@
|
||||
import { Context } from "elysia";
|
||||
import { loginWithPasswordService } from "../services/loginWithPassword.service";
|
||||
import { loginWithPasswordSchema } from "../auth.schema";
|
||||
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";
|
||||
|
||||
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
|
||||
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) {
|
||||
// Handle any errors that occur during the login process
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user