📝 (user) docs for user check password

This commit is contained in:
unknown
2025-07-01 01:01:12 +07:00
parent 3ef7f1a249
commit 5465ba4e81
4 changed files with 14 additions and 6 deletions

View File

@ -2,11 +2,12 @@ import { AppError } from "../../error/instances/app";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
export const comparePassword = async ( export const comparePassword = async (
passwordInput: string, providedPassword: string,
passwordRaw: string storedPassword: string
) => { ) => {
try { try {
return bcrypt.compare(passwordInput, passwordRaw); // Compare the provided password with the stored password
return bcrypt.compare(providedPassword, storedPassword);
} catch (error) { } catch (error) {
throw new AppError(401, "Invalid credentials", error); throw new AppError(401, "Invalid credentials", error);
} }

View File

@ -9,12 +9,17 @@ export const checkUserPasswordController = async (
ctx: Context & { body: { password: string } } ctx: Context & { body: { password: string } }
) => { ) => {
try { try {
// Get the credentials information from cookies
const cookie = getCookie(ctx); const cookie = getCookie(ctx);
const jwtPayload = jwtDecode(cookie.auth_token!); const jwtPayload = jwtDecode(cookie.auth_token!);
// Execute the check user password service
const checkUserPassword = await checkUserPasswordService( const checkUserPassword = await checkUserPasswordService(
jwtPayload.user.username, jwtPayload.user.username,
ctx.body.password ctx.body.password
); );
// If the password is valid, return a success response
return returnWriteResponse( return returnWriteResponse(
ctx.set, ctx.set,
204, 204,

View File

@ -9,12 +9,14 @@ export const checkUserPasswordService = async (
password: string password: string
) => { ) => {
try { try {
// find user by username and get the password
const userData = (await findUserByEmailOrUsernameService(username, { const userData = (await findUserByEmailOrUsernameService(username, {
verbose: true, verbose: true,
})) as User; })) as User;
const RawPassword = userData.password; const StoredPassword = userData.password;
const matchingPassword = await comparePassword(password, RawPassword); // compare the provided password with the stored password
const matchingPassword = await comparePassword(password, StoredPassword);
if (!matchingPassword) { if (!matchingPassword) {
throw new AppError(401, "Invalid Credential"); throw new AppError(401, "Invalid Credential");
} }

View File

@ -3,7 +3,7 @@ import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { findUserByEmailOrUsernameRepo } from "../repositories/findUserByEmailOrUsername.repository"; import { findUserByEmailOrUsernameRepo } from "../repositories/findUserByEmailOrUsername.repository";
export interface FindUserByEmailOrUsernameOptions { export interface FindUserByEmailOrUsernameOptions {
verbose?: boolean; verbose?: boolean; // If true, returns the user with all details including sensitive information
} }
export const findUserByEmailOrUsernameService = async ( export const findUserByEmailOrUsernameService = async (