diff --git a/src/index.js b/src/index.js index 477c4bd9..f29461b6 100644 --- a/src/index.js +++ b/src/index.js @@ -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,162 +166,120 @@ 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) { - const loginObject = await userController.loginUser(req, fcmId, deviceId); - if (loginObject.same) { - const phoneVerified = loginObject.user.phoneVerified; - const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag; - - if (!phoneVerified) { - return reply.send({ + 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) { + 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: { error: false, - phoneVerified: false, + apiversion: fastify.config.APIVERSION, + access_token: token, + buildingName: loginObject.user.buildingName, + email: loginObject.user.emails, phone: loginObject.user.phone, - oneTimePasswordSetFlag: oneTimePasswordSetFlag, - message: "Please Verify your phone number", + 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 if (oneTimePasswordSetFlag) { - return reply.send({ + } else { + reply.send({ simplydata: { error: false, - phoneVerified: phoneVerified, + apiversion: fastify.config.APIVERSION, + access_token: token, + picture: profilePicture.picture, + email: loginObject.user.emails, phone: loginObject.user.phone, - oneTimePasswordSetFlag: true, - message: "Password must be reset", - }, - }); - } else { - const token = fastify.jwt.sign( - { + buildingName: loginObject.user.buildingName, + customerId: loginObject.user.customerId, username: loginObject.user.username, - userId: loginObject.user._id, - roles: loginObject.user.profile.role, + 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, }, - { 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", { schema: { description: "This is for Login Otp Installation", @@ -981,4 +940,4 @@ const start = async () => { process.exit(1); } }; -start(); +start(); \ No newline at end of file