changes on login user

master^2
Bhaskar 2 months ago
parent 7d15a3a840
commit c03e6619c3

@ -155,6 +155,123 @@ 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", {
// schema: {
// description: "This is for Login User",
// tags: ["Login"],
// summary: "This is for User Login",
// body: {
// type: "object",
// required: ["phone", "password"],
// properties: {
// phone: { type: "string" },
// password: { type: "string" },
// fcmIds: { type: "array", items: { type: "string" }, default: [] },
// deviceId: { type: "string" },
// },
// },
// },
// async handler(req, reply) {
// const { phone, password, fcmIds, deviceId } = req.body;
// console.log(password, phone);
// const loginObject = await userController.loginUser(req, fcmIds, deviceId);
// console.log("loginObject",loginObject)
// if (!loginObject.same) {
// return reply.send({
// simplydata: {
// error: true,
// code: 400,
// message: "Invalid UserId or Password supplied",
// },
// });
// }
// const user = loginObject.user;
// const phoneVerified = user.phoneVerified;
// const oneTimePasswordSetFlag = user.oneTimePasswordSetFlag;
// if (fcmIds.length > 0) {
// await User.updateOne(
// { customerId: user.customerId },
// { $addToSet: { fcmIds: { $each: fcmIds } } }
// );
// }
// if (!phoneVerified) {
// return reply.send({
// simplydata: {
// error: false,
// phoneVerified: false,
// phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
// oneTimePasswordSetFlag,
// message: "Please Verify your phone number",
// },
// });
// }
// if (oneTimePasswordSetFlag) {
// return reply.send({
// simplydata: {
// error: false,
// phoneVerified,
// phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
// oneTimePasswordSetFlag: true,
// message: "Password must be reset",
// },
// });
// }
// const tokenPayload = {
// username: loginObject.isStaff ? loginObject.staffMember.name : user.username,
// userId: user._id,
// roles: user.profile.role,
// };
// const token = fastify.jwt.sign(tokenPayload, { expiresIn: "30d" });
// const profilePicture = await ProfilePicture.findOne({ customerId: user.customerId });
// const responsePayload = {
// simplydata: {
// error: false,
// apiversion: fastify.config.APIVERSION,
// access_token: token,
// buildingName: user.buildingName,
// email: user.emails,
// phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone,
// customerId: user.customerId,
// username: loginObject.isStaff ? loginObject.staffMember.name : user.username,
// address1: user.profile.address1,
// address2: user.profile.address2,
// phoneVerified: user.phoneVerified,
// oneTimePasswordSetFlag: user.oneTimePasswordSetFlag,
// latitude: user.latitude,
// longitude: user.longitude,
// type: user.profile.role,
// loginType: loginObject.isStaff ? "staff" : "user",
// },
// };
// 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;
// }
// if (profilePicture) {
// responsePayload.simplydata.picture = profilePicture.picture;
// }
// reply.send(responsePayload);
// },
// });
fastify.post("/api/login", { fastify.post("/api/login", {
schema: { schema: {
description: "This is for Login User", description: "This is for Login User",
@ -164,32 +281,31 @@ fastify.post("/api/login", {
type: "object", type: "object",
required: ["phone", "password"], required: ["phone", "password"],
properties: { properties: {
phone: { type: "string" }, phone: { type: "string", description: "Registered phone number" },
password: { type: "string" }, password: { type: "string", description: "Password for authentication" },
fcmIds: { type: "array", items: { type: "string" }, default: [] }, fcmIds: { type: "array", items: { type: "string" }, default: [] },
deviceId: { type: "string" }, deviceId: { type: "string" }
}, }
}, }
}, },
async handler(req, reply) { async handler(req, reply) {
const { phone, password, fcmIds, deviceId } = req.body; try {
console.log(password, phone); const { phone, password, fcmIds = [], deviceId } = req.body;
const loginObject = await userController.loginUser(req, fcmIds, deviceId); // Find user by phone
if (!loginObject.same) { const user = await User.findOne({ phone });
return reply.send({ console.log("user",user)
simplydata: { if (!user) {
error: true, return reply.code(400).send({ simplydata: { error: true, message: "User not found" } });
code: 400,
message: "Invalid UserId or Password supplied",
},
});
} }
const user = loginObject.user; // Verify password (bcrypt)
const phoneVerified = user.phoneVerified; const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
const oneTimePasswordSetFlag = user.oneTimePasswordSetFlag; if (!isMatch) {
return reply.code(400).send({ simplydata: { error: true, message: "Invalid credentials" } });
}
// Update FCM Ids if present
if (fcmIds.length > 0) { if (fcmIds.length > 0) {
await User.updateOne( await User.updateOne(
{ customerId: user.customerId }, { customerId: user.customerId },
@ -197,49 +313,57 @@ fastify.post("/api/login", {
); );
} }
if (!phoneVerified) { // Phone Verification
if (!user.phoneVerified) {
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
phoneVerified: false, phoneVerified: false,
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone, phone: user.phone,
oneTimePasswordSetFlag, oneTimePasswordSetFlag: user.oneTimePasswordSetFlag,
message: "Please Verify your phone number", message: "Please Verify your phone number"
}, }
}); });
} }
if (oneTimePasswordSetFlag) { // Password reset flag
if (user.oneTimePasswordSetFlag) {
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
phoneVerified, phoneVerified: user.phoneVerified,
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone, phone: user.phone,
oneTimePasswordSetFlag: true, oneTimePasswordSetFlag: true,
message: "Password must be reset", message: "Password must be reset"
}, }
}); });
} }
// JWT Token Payload
const tokenPayload = { const tokenPayload = {
username: loginObject.isStaff ? loginObject.staffMember.name : user.username, username: user.username,
userId: user._id, userId: user._id,
roles: user.profile.role, roles: user.profile.role
}; };
const token = fastify.jwt.sign(tokenPayload, { expiresIn: "30d" }); // JWT Token Generation (matches /api/storelogin style)
const token = fastify.jwt.sign(tokenPayload, /* no direct secret here, assumes plugin config */{ expiresIn: "30d" });
// Profile Picture
const profilePicture = await ProfilePicture.findOne({ customerId: user.customerId }); const profilePicture = await ProfilePicture.findOne({ customerId: user.customerId });
// Response Construction
const responsePayload = { const responsePayload = {
simplydata: { simplydata: {
error: false, error: false,
apiversion: fastify.config.APIVERSION, message: "Login successful",
apiversion: fastify.config ? fastify.config.APIVERSION : undefined,
access_token: token, access_token: token,
buildingName: user.buildingName, buildingName: user.buildingName,
email: user.emails, email: user.emails,
phone: loginObject.isStaff ? loginObject.staffMember.phone : user.phone, phone: user.phone,
customerId: user.customerId, customerId: user.customerId,
username: loginObject.isStaff ? loginObject.staffMember.name : user.username, username: user.username,
address1: user.profile.address1, address1: user.profile.address1,
address2: user.profile.address2, address2: user.profile.address2,
phoneVerified: user.phoneVerified, phoneVerified: user.phoneVerified,
@ -247,27 +371,21 @@ fastify.post("/api/login", {
latitude: user.latitude, latitude: user.latitude,
longitude: user.longitude, longitude: user.longitude,
type: user.profile.role, type: user.profile.role,
loginType: loginObject.isStaff ? "staff" : "user", loginType: "user"
},
};
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;
} }
};
if (profilePicture) { if (profilePicture) {
responsePayload.simplydata.picture = profilePicture.picture; responsePayload.simplydata.picture = profilePicture.picture;
} }
reply.send(responsePayload); return reply.send(responsePayload);
},
} catch (error) {
console.error("Login Error:", error);
return reply.code(500).send({ simplydata: { error: true, message: "Internal server error" } });
}
}
}); });

Loading…
Cancel
Save