rewrite jwt set in login service
This commit is contained in:
@ -4,23 +4,6 @@ import { JWTAuthToken } from "./types";
|
|||||||
import { parse } from "cookie";
|
import { parse } from "cookie";
|
||||||
import { returnErrorResponse } from "../../../callback/httpResponse";
|
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 => {
|
export const JWTDecodeToken = (ctx: Context): JWTAuthToken => {
|
||||||
const cookiePayload = ctx.request.headers.get("Cookie");
|
const cookiePayload = ctx.request.headers.get("Cookie");
|
||||||
if (!cookiePayload)
|
if (!cookiePayload)
|
||||||
@ -7,7 +7,7 @@ import {
|
|||||||
} from "../../../helpers/callback/httpResponse";
|
} from "../../../helpers/callback/httpResponse";
|
||||||
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/cookies/userHeader/getUserHeaderInformation";
|
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||||
|
|
||||||
export const loginWithPassword = async (
|
export const loginWithPassword = async (
|
||||||
ctx: Context & { body: LoginWithPasswordRequest }
|
ctx: Context & { body: LoginWithPasswordRequest }
|
||||||
|
|||||||
@ -2,10 +2,9 @@ import bcrypt from "bcrypt";
|
|||||||
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
|
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
|
||||||
import { LoginWithPasswordRequest } from "../auth.types";
|
import { LoginWithPasswordRequest } from "../auth.types";
|
||||||
import { AppError } from "../../../helpers/error/instances/app";
|
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 { createUserSessionService } from "../../userSession/services/createUserSession.service";
|
||||||
import { jwtEncode } from "../../../helpers/cookies/jwt/encode";
|
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||||
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
|
||||||
|
|
||||||
export const loginWithPasswordService = async (
|
export const loginWithPasswordService = async (
|
||||||
request: LoginWithPasswordRequest,
|
request: LoginWithPasswordRequest,
|
||||||
@ -15,15 +14,17 @@ 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);
|
||||||
|
|
||||||
// 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");
|
||||||
|
|
||||||
|
// create new user session
|
||||||
const userSession = await createUserSessionService({
|
const userSession = await createUserSessionService({
|
||||||
userId: userData.id,
|
userId: userData.id,
|
||||||
userHeaderInformation: userHeaderInfo,
|
userHeaderInformation: userHeaderInfo,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// create JWT token that contain user session
|
||||||
const jwtToken = jwtEncode(userSession);
|
const jwtToken = jwtEncode(userSession);
|
||||||
|
|
||||||
return jwtToken;
|
return jwtToken;
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {
|
|||||||
returnWriteResponse,
|
returnWriteResponse,
|
||||||
} from "../../../helpers/callback/httpResponse";
|
} from "../../../helpers/callback/httpResponse";
|
||||||
import { createUserRoleService } from "../services/createUserRole.service";
|
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 { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { Context } from "elysia";
|
import { Context } from "elysia";
|
||||||
import { createUserSessionService } from "../services/createUserSession.service";
|
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 { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import {
|
import {
|
||||||
returnErrorResponse,
|
returnErrorResponse,
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { UserHeaderInformation } from "../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
import { UserHeaderInformation } from "../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||||
|
|
||||||
export interface createUserSessionServiceParams {
|
export interface createUserSessionServiceParams {
|
||||||
userId: string;
|
userId: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user