deviceId and fcmId passes in login

master
Bhaskar 1 year ago
parent b6f9dee284
commit d079195797

@ -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 // 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 // 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 { try {
const phone = req.body.phone; const phone = req.body.phone;
const password = req.body.password; const password = req.body.password;
const user = await User.findOne({ phone: phone }); const user = await User.findOne({ phone: phone });
// compare users password with what is supplied
if (user) { if (user) {
isSame = await bcryptComparePassword( const isSame = await bcryptComparePassword(
password, password,
user.services.password.bcrypt user.services.password.bcrypt
); );
// if password supplied matches return object
if (isSame) { 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 }; return { same: true, user: user };
} else { } else {
return { same: false }; return { same: false };
@ -305,6 +308,7 @@ exports.loginUser = async (req) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.loginUserWithOTP = async (req) => { exports.loginUserWithOTP = async (req) => {
try { try {
const phone = req.body.phone; const phone = req.body.phone;

@ -161,11 +161,16 @@ fastify.post("/api/login", {
properties: { properties: {
phone: { type: "string" }, phone: { type: "string" },
password: { type: "string" }, password: { type: "string" },
fcmId: { type: "string" }, // Add this line
deviceId: { type: "string" } // Add this line
}, },
}, },
}, },
async handler(req, reply) { 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) { if (loginObject.same) {
const phoneVerified = loginObject.user.phoneVerified; const phoneVerified = loginObject.user.phoneVerified;
const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag; const oneTimePasswordSetFlag = loginObject.user.oneTimePasswordSetFlag;
@ -180,7 +185,6 @@ fastify.post("/api/login", {
simplydata: { simplydata: {
error: false, error: false,
phoneVerified: false, phoneVerified: false,
phone: loginObject.user.phone, phone: loginObject.user.phone,
oneTimePasswordSetFlag: oneTimePasswordSetFlag, oneTimePasswordSetFlag: oneTimePasswordSetFlag,
message: "Please Verify your phone number", message: "Please Verify your phone number",
@ -203,87 +207,72 @@ fastify.post("/api/login", {
userId: loginObject.user._id, userId: loginObject.user._id,
roles: loginObject.user.profile.role, 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" } { expiresIn: "30d" }
); );
var arr = loginObject.user.profile.role; const arr = loginObject.user.profile.role;
var arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string const arrayToString = JSON.stringify(Object.assign({}, arr)); // convert array to string
var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object const stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
var c_id = loginObject.user.customerId const c_id = loginObject.user.customerId;
var profilePicture = await ProfilePicture.findOne({ customerId:c_id}); const profilePicture = await ProfilePicture.findOne({ customerId: c_id });
if (!profilePicture) { if (!profilePicture) {
reply.send({ reply.send({
simplydata: { simplydata: {
error: false, error: false,
apiversion: fastify.config.APIVERSION, apiversion: fastify.config.APIVERSION,
access_token: token, access_token: token,
buildingName:loginObject.user.buildingName, buildingName: loginObject.user.buildingName,
email: loginObject.user.emails, email: loginObject.user.emails,
phone: loginObject.user.phone, phone: loginObject.user.phone,
customerId: loginObject.user.customerId, customerId: loginObject.user.customerId,
username: loginObject.user.username, username: loginObject.user.username,
address1: loginObject.user.profile.address1, address1: loginObject.user.profile.address1,
address2: loginObject.user.profile.address2, address2: loginObject.user.profile.address2,
phoneVerified: loginObject.user.phoneVerified, phoneVerified: loginObject.user.phoneVerified,
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag, oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
latitude: loginObject.user.latitude, latitude: loginObject.user.latitude,
longitude: loginObject.user.longitude, longitude: loginObject.user.longitude,
type: loginObject.user.profile.role, type: loginObject.user.profile.role,
fcmId: loginObject.user.fcmId, typeasobj: stringToJsonObject,
deviceId: loginObject.user.deviceId,
typeasobj: stringToJsonObject,
}, },
}); });
}if (profilePicture) { } else {
reply.send({ 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, picture: profilePicture.picture,
email: loginObject.user.emails, email: loginObject.user.emails,
phone: loginObject.user.phone, phone: loginObject.user.phone,
buildingName:loginObject.user.buildingName, buildingName: loginObject.user.buildingName,
customerId: loginObject.user.customerId, customerId: loginObject.user.customerId,
username: loginObject.user.username, username: loginObject.user.username,
address1: loginObject.user.profile.address1, address1: loginObject.user.profile.address1,
address2: loginObject.user.profile.address2, address2: loginObject.user.profile.address2,
phoneVerified: loginObject.user.phoneVerified, phoneVerified: loginObject.user.phoneVerified,
oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag, oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag,
latitude: loginObject.user.latitude, latitude: loginObject.user.latitude,
longitude: loginObject.user.longitude, longitude: loginObject.user.longitude,
type: loginObject.user.profile.role, type: loginObject.user.profile.role,
deviceId: loginObject.user.deviceId, typeasobj: stringToJsonObject,
fcmId: loginObject.user.fcmId,
typeasobj: stringToJsonObject,
}, },
}); });
} }
// console.log({
// username: loginObject.user.username,
// roles: loginObject.user.profile.role,
// rolesasobj: stringToJsonObject,
// });
// console.log("sending token \n");
// console.log(token);
} }
} else { } else {
error = { reply.send({
simplydata: { simplydata: {
error: true, error: true,
code: 400, code: 400,
message: "Invalid UserId , Password supplied", message: "Invalid UserId or Password supplied",
}, },
}; });
reply.send(error);
} }
}, },
}); });
fastify.post("/api/installotplogin", { fastify.post("/api/installotplogin", {
schema: { schema: {
description: "This is for Login Otp Installation", description: "This is for Login Otp Installation",

Loading…
Cancel
Save