|
|
|
@ -154,6 +154,7 @@ fastify.register(require('point-of-view'), {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// * This is for login user as a simply user *
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/login", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login User",
|
|
|
|
@ -165,24 +166,29 @@ fastify.post("/api/login", {
|
|
|
|
|
properties: {
|
|
|
|
|
phone: { type: "string" },
|
|
|
|
|
password: { type: "string" },
|
|
|
|
|
fcmId: { type: "string" },
|
|
|
|
|
deviceId: { type: "string" },
|
|
|
|
|
fcmId: { type: "string" }, // Add this line
|
|
|
|
|
deviceId: { type: "string" } // Add this line
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
async handler(req, reply) {
|
|
|
|
|
// Pass fcmId and deviceId to the loginUser function
|
|
|
|
|
const { phone, password, fcmId, deviceId } = req.body;
|
|
|
|
|
|
|
|
|
|
// First check for user login
|
|
|
|
|
const user = await User.findOne({ phone });
|
|
|
|
|
if (user) {
|
|
|
|
|
console.log(password,phone)
|
|
|
|
|
const loginObject = await userController.loginUser(req, fcmId, deviceId);
|
|
|
|
|
|
|
|
|
|
if (loginObject.same) {
|
|
|
|
|
console.log("entered 1st loop")
|
|
|
|
|
const phoneVerified = loginObject.user.phoneVerified;
|
|
|
|
|
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
"oneTimePasswordSetFlag is ......",
|
|
|
|
|
oneTimePasswordSetFlag,
|
|
|
|
|
typeof oneTimePasswordSetFlag,
|
|
|
|
|
typeof phoneVerified
|
|
|
|
|
);
|
|
|
|
|
if (!phoneVerified) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
phoneVerified: false,
|
|
|
|
@ -192,7 +198,7 @@ fastify.post("/api/login", {
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else if (oneTimePasswordSetFlag) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
phoneVerified: phoneVerified,
|
|
|
|
@ -210,7 +216,6 @@ fastify.post("/api/login", {
|
|
|
|
|
},
|
|
|
|
|
{ expiresIn: "30d" }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const arr = loginObject.user.profile.role;
|
|
|
|
|
const arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string
|
|
|
|
|
const stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
|
|
|
|
@ -218,7 +223,7 @@ fastify.post("/api/login", {
|
|
|
|
|
const profilePicture = await ProfilePicture.findOne({ customerId: c_id });
|
|
|
|
|
|
|
|
|
|
if (!profilePicture) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
@ -239,7 +244,7 @@ fastify.post("/api/login", {
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return reply.send({
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
@ -262,65 +267,19 @@ fastify.post("/api/login", {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check for staff login
|
|
|
|
|
const allUsers = await User.find({ "staff.staff": { $elemMatch: { phone } } });
|
|
|
|
|
for (const currentUser of allUsers) {
|
|
|
|
|
const staffMember = currentUser.staff.staff.find(staff => staff.phone === phone && staff.status === "active");
|
|
|
|
|
if (staffMember && staffMember.password === password) {
|
|
|
|
|
|
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
|
{
|
|
|
|
|
name: staffMember.name,
|
|
|
|
|
phone: staffMember.phone,
|
|
|
|
|
customerId: currentUser.customerId,
|
|
|
|
|
role: 'staff',
|
|
|
|
|
},
|
|
|
|
|
{ expiresIn: "30d" }
|
|
|
|
|
);
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
access_token: token,
|
|
|
|
|
name: staffMember.name,
|
|
|
|
|
phone: staffMember.phone,
|
|
|
|
|
customerId: currentUser.customerId,
|
|
|
|
|
|
|
|
|
|
buildingName: currentUser.buildingName,
|
|
|
|
|
email: currentUser.emails,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
address1: currentUser.address1,
|
|
|
|
|
address2: currentUser.address2,
|
|
|
|
|
phoneVerified: true,
|
|
|
|
|
|
|
|
|
|
latitude: currentUser.latitude,
|
|
|
|
|
longitude: currentUser.longitude,
|
|
|
|
|
type: "staff",
|
|
|
|
|
message: "Staff login successful",
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no user or staff found
|
|
|
|
|
return reply.send({
|
|
|
|
|
} else {
|
|
|
|
|
reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: true,
|
|
|
|
|
code: 400,
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/installotplogin", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login Otp Installation",
|
|
|
|
|