changes reverted

master^2
Varun 9 months ago
parent 8b3ef81b21
commit 6a74807380

@ -154,6 +154,7 @@ fastify.register(require('point-of-view'), {
}); });
// * This is for login user as a simply user * // * This is for login user as a simply user *
fastify.post("/api/login", { fastify.post("/api/login", {
schema: { schema: {
description: "This is for Login User", description: "This is for Login User",
@ -165,162 +166,120 @@ fastify.post("/api/login", {
properties: { properties: {
phone: { type: "string" }, phone: { type: "string" },
password: { type: "string" }, password: { type: "string" },
fcmId: { type: "string" }, fcmId: { type: "string" }, // Add this line
deviceId: { type: "string" }, 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, fcmId, deviceId } = req.body; const { phone, password, fcmId, deviceId } = req.body;
console.log(password,phone)
// First check for user login const loginObject = await userController.loginUser(req, fcmId, deviceId);
const user = await User.findOne({ phone });
if (user) { if (loginObject.same) {
const loginObject = await userController.loginUser(req, fcmId, deviceId); console.log("entered 1st loop")
if (loginObject.same) { const phoneVerified = loginObject.user.phoneVerified;
const phoneVerified = loginObject.user.phoneVerified; const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag; console.log(
"oneTimePasswordSetFlag is ......",
if (!phoneVerified) { oneTimePasswordSetFlag,
return reply.send({ typeof oneTimePasswordSetFlag,
typeof phoneVerified
);
if (!phoneVerified) {
reply.send({
simplydata: {
error: false,
phoneVerified: false,
phone: loginObject.user.phone,
oneTimePasswordSetFlag: oneTimePasswordSetFlag,
message: "Please Verify your phone number",
},
});
} else if (oneTimePasswordSetFlag) {
reply.send({
simplydata: {
error: false,
phoneVerified: phoneVerified,
phone: loginObject.user.phone,
oneTimePasswordSetFlag: true,
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) {
reply.send({
simplydata: { simplydata: {
error: false, error: false,
phoneVerified: false, apiversion: fastify.config.APIVERSION,
access_token: token,
buildingName: loginObject.user.buildingName,
email: loginObject.user.emails,
phone: loginObject.user.phone, phone: loginObject.user.phone,
oneTimePasswordSetFlag: oneTimePasswordSetFlag, customerId: loginObject.user.customerId,
message: "Please Verify your phone number", 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 if (oneTimePasswordSetFlag) { } else {
return reply.send({ reply.send({
simplydata: { simplydata: {
error: false, error: false,
phoneVerified: phoneVerified, apiversion: fastify.config.APIVERSION,
access_token: token,
picture: profilePicture.picture,
email: loginObject.user.emails,
phone: loginObject.user.phone, phone: loginObject.user.phone,
oneTimePasswordSetFlag: true, buildingName: loginObject.user.buildingName,
message: "Password must be reset", customerId: loginObject.user.customerId,
},
});
} else {
const token = fastify.jwt.sign(
{
username: loginObject.user.username, username: loginObject.user.username,
userId: loginObject.user._id, address1: loginObject.user.profile.address1,
roles: loginObject.user.profile.role, 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,
}, },
{ 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) {
return reply.send({
simplydata: {
error: false,
apiversion: fastify.config.APIVERSION,
access_token: token,
buildingName: loginObject.user.buildingName,
email: loginObject.user.emails,
phone: loginObject.user.phone,
customerId: loginObject.user.customerId,
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 {
return reply.send({
simplydata: {
error: false,
apiversion: fastify.config.APIVERSION,
access_token: token,
picture: profilePicture.picture,
email: loginObject.user.emails,
phone: loginObject.user.phone,
buildingName: loginObject.user.buildingName,
customerId: loginObject.user.customerId,
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: {
error: true,
code: 400,
message: "Invalid UserId or Password supplied",
},
});
} }
// 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({
simplydata: {
error: true,
code: 400,
message: "Invalid UserId or Password supplied",
},
});
}, },
}); });
fastify.post("/api/installotplogin", { fastify.post("/api/installotplogin", {
schema: { schema: {
description: "This is for Login Otp Installation", description: "This is for Login Otp Installation",
@ -981,4 +940,4 @@ const start = async () => {
process.exit(1); process.exit(1);
} }
}; };
start(); start();
Loading…
Cancel
Save