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

0% Statements 0/17
0% Branches 0/8
0% Functions 0/1
0% Lines 0/17

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 27 28 29 30 31 32                                                               
import AdminModel from "../../models/Admins.js";
import googleAccountGetter from "../../utilities/googleAccountGetter.js";
import messageResponse from "../../utilities/messageResponse.js";
import { generateJWT } from "../../utilities/jwtUtilities.js";
import { compareSync } from "bcrypt";
 
export default async function logInAdminWithGoogle(googleUserCredentials) {
    try {
        const { ok, googleResponse } = await googleAccountGetter(googleUserCredentials);
        if (ok) {
            const adminData = await AdminModel.findOne({ adminEmail: googleResponse.email }, { createdAt: 0, updatedAt: 0 });
            if (adminData) {
                if (adminData.signingMethod === "Google") {
                    const correctPassword = compareSync(googleResponse.sub, adminData.adminPassword);
                    if (correctPassword) {
                        const token = generateJWT({ adminId: adminData._id, role: "admin" })
                        adminData.adminPassword = undefined;
                        return { status: 200, response: { adminData, accessToken: token, ok: true } };
                    } else {
                        return messageResponse("Google Authorization failed");
                    }
                }
                else return messageResponse("Your email registred by another signing up method", 200);
            }
            else return messageResponse("You didn't have registered with us before");
        }
        else return googleResponse
    } catch (error) {
        console.log(error);
        return messageResponse("Unexpected Error !");
    }
}