rewrite jwt set in login service
This commit is contained in:
@ -4,23 +4,6 @@ import { JWTAuthToken } from "./types";
|
||||
import { parse } from "cookie";
|
||||
import { returnErrorResponse } from "../../../callback/httpResponse";
|
||||
|
||||
/**
|
||||
* Verifies the authentication cookie from the request header.
|
||||
*
|
||||
* This helper function is used in an ElysiaJS context to check the validity of
|
||||
* a user's authentication token stored in cookies. If the cookie is not found,
|
||||
* it returns a `400 Bad Request`. If the token is invalid or expired, it returns
|
||||
* a `401 Unauthorized`. If the token is valid, it returns the decoded user data.
|
||||
*
|
||||
* @param ctx - The request context from Elysia, used to read headers and set the response.
|
||||
*
|
||||
* @returns The decoded JWT payload if the token is valid,
|
||||
* or a standardized error response if the cookie is missing or invalid.
|
||||
*
|
||||
* @example
|
||||
* const decodedToken = decodeAuthToken(ctx);
|
||||
* ctx => Elysia context
|
||||
*/
|
||||
export const JWTDecodeToken = (ctx: Context): JWTAuthToken => {
|
||||
const cookiePayload = ctx.request.headers.get("Cookie");
|
||||
if (!cookiePayload)
|
||||
@ -7,7 +7,7 @@ import {
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { LoginWithPasswordRequest } from "../auth.types";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
|
||||
export const loginWithPassword = async (
|
||||
ctx: Context & { body: LoginWithPasswordRequest }
|
||||
|
||||
@ -2,10 +2,9 @@ import bcrypt from "bcrypt";
|
||||
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
|
||||
import { LoginWithPasswordRequest } from "../auth.types";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { UserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
||||
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||
import { createUserSessionService } from "../../userSession/services/createUserSession.service";
|
||||
import { jwtEncode } from "../../../helpers/cookies/jwt/encode";
|
||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||
|
||||
export const loginWithPasswordService = async (
|
||||
request: LoginWithPasswordRequest,
|
||||
@ -15,15 +14,17 @@ export const loginWithPasswordService = async (
|
||||
// search for user data using an identifier (username or email)
|
||||
const userData = await findUserByEmailOrUsernameService(request.identifier);
|
||||
|
||||
// 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)))
|
||||
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;
|
||||
|
||||
@ -5,7 +5,7 @@ import {
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { createUserRoleService } from "../services/createUserRole.service";
|
||||
import { JWTDecodeToken } from "../../../helpers/cookies/jwt/decode";
|
||||
import { JWTDecodeToken } from "../../../helpers/http/jwt/decode";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { Context } from "elysia";
|
||||
import { createUserSessionService } from "../services/createUserSession.service";
|
||||
import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { UserHeaderInformation } from "../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
||||
import { UserHeaderInformation } from "../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||
|
||||
export interface createUserSessionServiceParams {
|
||||
userId: string;
|
||||
|
||||
Reference in New Issue
Block a user