|
|
@ -166,131 +166,114 @@ fastify.post("/api/login", {
|
|
|
|
properties: {
|
|
|
|
properties: {
|
|
|
|
phone: { type: "string" },
|
|
|
|
phone: { type: "string" },
|
|
|
|
password: { type: "string" },
|
|
|
|
password: { type: "string" },
|
|
|
|
// fcmId: { type: "string" }, // Add this line
|
|
|
|
fcmIds: { type: "array", items: { type: "string" }, default: [] },
|
|
|
|
fcmIds: {
|
|
|
|
deviceId: { type: "string" },
|
|
|
|
type: "array", // Change this to allow an array
|
|
|
|
|
|
|
|
items: { type: "string" }, // Each item in the array is a string
|
|
|
|
|
|
|
|
default: [], // Default value if not provided
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
deviceId: { type: "string" } // Add this line
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
async handler(req, reply) {
|
|
|
|
async handler(req, reply) {
|
|
|
|
// Pass fcmId and deviceId to the loginUser function
|
|
|
|
|
|
|
|
const { phone, password, fcmIds, deviceId } = req.body;
|
|
|
|
const { phone, password, fcmIds, deviceId } = req.body;
|
|
|
|
console.log(password,phone)
|
|
|
|
console.log(password, phone);
|
|
|
|
|
|
|
|
|
|
|
|
const loginObject = await userController.loginUser(req, fcmIds, deviceId);
|
|
|
|
const loginObject = await userController.loginUser(req, fcmIds, deviceId);
|
|
|
|
|
|
|
|
if (!loginObject.same) {
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
code: 400,
|
|
|
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (loginObject.same) {
|
|
|
|
const user = loginObject.user;
|
|
|
|
console.log("entered 1st loop")
|
|
|
|
const phoneVerified = user.phoneVerified;
|
|
|
|
const phoneVerified = loginObject.user.phoneVerified;
|
|
|
|
const oneTimePasswordSetFlag = user.oneTimePasswordSetFlag;
|
|
|
|
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
if (fcmIds.length > 0) {
|
|
|
|
"oneTimePasswordSetFlag is ......",
|
|
|
|
|
|
|
|
oneTimePasswordSetFlag,
|
|
|
|
|
|
|
|
typeof oneTimePasswordSetFlag,
|
|
|
|
|
|
|
|
typeof phoneVerified
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fcmIds && fcmIds.length > 0) {
|
|
|
|
|
|
|
|
await User.updateOne(
|
|
|
|
await User.updateOne(
|
|
|
|
{ customerId: loginObject.user.customerId },
|
|
|
|
{ customerId: user.customerId },
|
|
|
|
{ $addToSet: { fcmIds: { $each: fcmIds } } } // Add multiple FCM IDs, avoiding duplicates
|
|
|
|
{ $addToSet: { fcmIds: { $each: fcmIds } } }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!phoneVerified) {
|
|
|
|
if (!phoneVerified) {
|
|
|
|
reply.send({
|
|
|
|
return reply.send({
|
|
|
|
simplydata: {
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
error: false,
|
|
|
|
phoneVerified: false,
|
|
|
|
phoneVerified: false,
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
|
|
|
|
oneTimePasswordSetFlag: oneTimePasswordSetFlag,
|
|
|
|
oneTimePasswordSetFlag,
|
|
|
|
message: "Please Verify your phone number",
|
|
|
|
message: "Please Verify your phone number",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (oneTimePasswordSetFlag) {
|
|
|
|
}
|
|
|
|
reply.send({
|
|
|
|
|
|
|
|
|
|
|
|
if (oneTimePasswordSetFlag) {
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
simplydata: {
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
error: false,
|
|
|
|
phoneVerified: phoneVerified,
|
|
|
|
phoneVerified,
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
|
|
|
|
oneTimePasswordSetFlag: true,
|
|
|
|
oneTimePasswordSetFlag: true,
|
|
|
|
message: "Password must be reset",
|
|
|
|
message: "Password must be reset",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
|
|
|
userId: loginObject.user._id,
|
|
|
|
|
|
|
|
roles: loginObject.user.profile.role,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{ 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
|
|
|
|
|
|
|
|
const c_id = loginObject.user.customerId;
|
|
|
|
|
|
|
|
const profilePicture = await ProfilePicture.findOne({ customerId: c_id });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!profilePicture) {
|
|
|
|
const tokenPayload = {
|
|
|
|
reply.send({
|
|
|
|
username: loginObject.isStaff ? loginObject.staffMember.name : user.username,
|
|
|
|
simplydata: {
|
|
|
|
userId: user._id,
|
|
|
|
error: false,
|
|
|
|
roles: user.profile.role,
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
};
|
|
|
|
access_token: token,
|
|
|
|
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
const token = fastify.jwt.sign(tokenPayload, { expiresIn: "30d" });
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
const profilePicture = await ProfilePicture.findOne({ customerId: user.customerId });
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
const responsePayload = {
|
|
|
|
username: loginObject.user.username,
|
|
|
|
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
error: false,
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
apiversion: fastify.config.APIVERSION,
|
|
|
|
access_token: token,
|
|
|
|
access_token: token,
|
|
|
|
picture: profilePicture.picture,
|
|
|
|
buildingName: user.buildingName,
|
|
|
|
email: loginObject.user.emails,
|
|
|
|
email: user.emails,
|
|
|
|
phone: loginObject.user.phone,
|
|
|
|
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
|
|
|
|
buildingName: loginObject.user.buildingName,
|
|
|
|
customerId: user.customerId,
|
|
|
|
customerId: loginObject.user.customerId,
|
|
|
|
username: loginObject.isStaff ? loginObject.staffMember.name : user.username,
|
|
|
|
username: loginObject.user.username,
|
|
|
|
address1: user.profile.address1,
|
|
|
|
address1: loginObject.user.profile.address1,
|
|
|
|
address2: user.profile.address2,
|
|
|
|
address2: loginObject.user.profile.address2,
|
|
|
|
phoneVerified: user.phoneVerified,
|
|
|
|
phoneVerified: loginObject.user.phoneVerified,
|
|
|
|
oneTimePasswordSetFlag: user.oneTimePasswordSetFlag,
|
|
|
|
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
|
|
|
|
latitude: user.latitude,
|
|
|
|
latitude: loginObject.user.latitude,
|
|
|
|
longitude: user.longitude,
|
|
|
|
longitude: loginObject.user.longitude,
|
|
|
|
type: user.profile.role,
|
|
|
|
type: loginObject.user.profile.role,
|
|
|
|
loginType: loginObject.isStaff ? "staff" : "user",
|
|
|
|
typeasobj: stringToJsonObject,
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (loginObject.isStaff) {
|
|
|
|
|
|
|
|
let allMotorAccess = loginObject.staffMember.all_motor_access;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize the value if it matches the given variations
|
|
|
|
|
|
|
|
if (["view", "view only", "View", "View Only"].includes(allMotorAccess)) {
|
|
|
|
|
|
|
|
allMotorAccess = "view";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
responsePayload.simplydata.all_motor_access = allMotorAccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
if (profilePicture) {
|
|
|
|
simplydata: {
|
|
|
|
responsePayload.simplydata.picture = profilePicture.picture;
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
code: 400,
|
|
|
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send(responsePayload);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/installotplogin", {
|
|
|
|
fastify.post("/api/installotplogin", {
|
|
|
|
schema: {
|
|
|
|
schema: {
|
|
|
|
description: "This is for Login Otp Installation",
|
|
|
|
description: "This is for Login Otp Installation",
|
|
|
|