From d07919579786bfc799c827430f1fa8da79134a85 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 30 Aug 2024 13:29:26 +0530 Subject: [PATCH] deviceId and fcmId passes in login --- src/controllers/userController.js | 12 ++-- src/index.js | 109 ++++++++++++++---------------- 2 files changed, 57 insertions(+), 64 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index f3e7dc9f..8b536c56 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -280,20 +280,23 @@ exports.addUser = async (req, reply) => { // Accepts a user , password , and checks in the system to see if user exists , and password is valid // returns a user object so that jwt token can be created and sent back to the client -exports.loginUser = async (req) => { +exports.loginUser = async (req, fcmId, deviceId) => { try { const phone = req.body.phone; const password = req.body.password; const user = await User.findOne({ phone: phone }); - // compare users password with what is supplied if (user) { - isSame = await bcryptComparePassword( + const isSame = await bcryptComparePassword( password, user.services.password.bcrypt ); - // if password supplied matches return object if (isSame) { + // Optionally, you can save/update fcmId and deviceId here + user.fcmId = fcmId; + user.deviceId = deviceId; + await user.save(); + return { same: true, user: user }; } else { return { same: false }; @@ -305,6 +308,7 @@ exports.loginUser = async (req) => { throw boom.boomify(err); } }; + exports.loginUserWithOTP = async (req) => { try { const phone = req.body.phone; diff --git a/src/index.js b/src/index.js index 072e0237..2ad09141 100644 --- a/src/index.js +++ b/src/index.js @@ -161,11 +161,16 @@ fastify.post("/api/login", { properties: { phone: { type: "string" }, password: { type: "string" }, + fcmId: { type: "string" }, // Add this line + deviceId: { type: "string" } // Add this line }, }, }, async handler(req, reply) { - loginObject = await userController.loginUser(req); + // Pass fcmId and deviceId to the loginUser function + const { phone, password, fcmId, deviceId } = req.body; + const loginObject = await userController.loginUser(req, fcmId, deviceId); + if (loginObject.same) { const phoneVerified = loginObject.user.phoneVerified; const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag; @@ -180,7 +185,6 @@ fastify.post("/api/login", { simplydata: { error: false, phoneVerified: false, - phone: loginObject.user.phone, oneTimePasswordSetFlag: oneTimePasswordSetFlag, message: "Please Verify your phone number", @@ -203,87 +207,72 @@ fastify.post("/api/login", { userId: loginObject.user._id, roles: loginObject.user.profile.role, }, - //expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d". - //A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), - //otherwise milliseconds unit is used by default ("120" is equal to "120ms"). { expiresIn: "30d" } ); - var arr = loginObject.user.profile.role; - var arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string - var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object - var c_id = loginObject.user.customerId - var profilePicture = await ProfilePicture.findOne({ customerId:c_id}); + 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, - 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, - fcmId: loginObject.user.fcmId, - deviceId: loginObject.user.deviceId, - typeasobj: stringToJsonObject, + 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, }, }); - }if (profilePicture) { + } else { 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, - deviceId: loginObject.user.deviceId, - fcmId: loginObject.user.fcmId, - typeasobj: stringToJsonObject, + 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, }, }); } - - // console.log({ - // username: loginObject.user.username, - // roles: loginObject.user.profile.role, - // rolesasobj: stringToJsonObject, - // }); - // console.log("sending token \n"); - // console.log(token); - } } else { - error = { + reply.send({ simplydata: { error: true, code: 400, - message: "Invalid UserId , Password supplied", + message: "Invalid UserId or Password supplied", }, - }; - reply.send(error); + }); } }, }); + fastify.post("/api/installotplogin", { schema: { description: "This is for Login Otp Installation",