rewrite jwt set in login service
This commit is contained in:
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user