All files / src/controllers/system-controller logInUser.js

69.23% Statements 9/13
66.66% Branches 4/6
100% Functions 1/1
69.23% Lines 9/13

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            2x 2x 2x 1x     1x 1x 1x 1x       1x          
import UsersModel from "../../models/Users.js";
import { compareSync } from "bcrypt";
import { generateJWT } from "../../utilities/jwtUtilities.js";
 
 
export default async function logInUser({ userEmail, userPassword }) {
    try {
        const userData = await UsersModel.findOne({ userEmail }, { userPassword: true, signingMethod: 1 });
        if (userData) {
            Iif (userData.signingMethod === "Google") {
                return { message: "This email signed up with another sign up method" }
            } else {
                const pass = compareSync(userPassword, userData.userPassword);
                if (pass) {
                    const token = generateJWT({ userId: userData._id, role: "user" })
                    return { status: true, userId: userData._id, accessToken: token };
                }
                else Ereturn { message: "There is error in Email or Password" };
            }
        } else return { message: "This email did not signed up with us before, Go to Sign up page" };
    } catch (error) {
        console.log(error)
        return { message: "There is unexpected error" }
    }
}