All files / src/controllers/users-controllers passwordChecker.js

66.66% Statements 10/15
62.5% Branches 5/8
100% Functions 1/1
66.66% Lines 10/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26          2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                      
import { compare } from "bcrypt";
import UsersModel from "../../models/Users.js";
import isUserAllowedToChangeHisPassword from "./isUserAllowedToChangeHisPassword.js";
 
export default async function passwordChecker(userId, password) {
    const notAllowedMessage = "You have changed your password since less than a month, You can't change it again, Try again after a month since last change"
    try {
        const projection = { userPassword: 1, lastPasswordChange: 1, signingMethod: 1 };
        const user = await UsersModel.findById(userId, projection);
        if (user) {
            const { userPassword, lastPasswordChange, signingMethod } = user;
            if (signingMethod === "Email & Password") {
                if (isUserAllowedToChangeHisPassword(lastPasswordChange)) {
                    const result = await compare(password, userPassword);
                    return { status: result, message: result ? "" : "Wrong password !" };
                }
                else Ereturn { status: false, message: notAllowedMessage };
            }
            else Ereturn { status: false, message: "You didn't signed up using (Email & Password) method" };
        }
        else Ereturn { status: false, message: "Error !" }
    } catch (error) {
        console.log(error)
        return { status: false, message: "Unexpected Error !" }
    }
}