|
|
|
@ -155,6 +155,123 @@ 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",
|
|
|
|
|
// 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", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login User",
|
|
|
|
@ -164,110 +281,111 @@ fastify.post("/api/login", {
|
|
|
|
|
type: "object",
|
|
|
|
|
required: ["phone", "password"],
|
|
|
|
|
properties: {
|
|
|
|
|
phone: { type: "string" },
|
|
|
|
|
password: { type: "string" },
|
|
|
|
|
phone: { type: "string", description: "Registered phone number" },
|
|
|
|
|
password: { type: "string", description: "Password for authentication" },
|
|
|
|
|
fcmIds: { type: "array", items: { type: "string" }, default: [] },
|
|
|
|
|
deviceId: { type: "string" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
deviceId: { type: "string" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
async handler(req, reply) {
|
|
|
|
|
const { phone, password, fcmIds, deviceId } = req.body;
|
|
|
|
|
console.log(password, phone);
|
|
|
|
|
try {
|
|
|
|
|
const { phone, password, fcmIds = [], deviceId } = req.body;
|
|
|
|
|
|
|
|
|
|
const loginObject = await userController.loginUser(req, fcmIds, deviceId);
|
|
|
|
|
if (!loginObject.same) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: true,
|
|
|
|
|
code: 400,
|
|
|
|
|
message: "Invalid UserId or Password supplied",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Find user by phone
|
|
|
|
|
const user = await User.findOne({ phone });
|
|
|
|
|
console.log("user",user)
|
|
|
|
|
if (!user) {
|
|
|
|
|
return reply.code(400).send({ simplydata: { error: true, message: "User not found" } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = loginObject.user;
|
|
|
|
|
const phoneVerified = user.phoneVerified;
|
|
|
|
|
const oneTimePasswordSetFlag = user.oneTimePasswordSetFlag;
|
|
|
|
|
// Verify password (bcrypt)
|
|
|
|
|
const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
|
|
|
|
|
if (!isMatch) {
|
|
|
|
|
return reply.code(400).send({ simplydata: { error: true, message: "Invalid credentials" } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fcmIds.length > 0) {
|
|
|
|
|
await User.updateOne(
|
|
|
|
|
{ customerId: user.customerId },
|
|
|
|
|
{ $addToSet: { fcmIds: { $each: fcmIds } } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Update FCM Ids if present
|
|
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Phone Verification
|
|
|
|
|
if (!user.phoneVerified) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
phoneVerified: false,
|
|
|
|
|
phone: user.phone,
|
|
|
|
|
oneTimePasswordSetFlag: user.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",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Password reset flag
|
|
|
|
|
if (user.oneTimePasswordSetFlag) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
phoneVerified: user.phoneVerified,
|
|
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
// JWT Token Payload
|
|
|
|
|
const tokenPayload = {
|
|
|
|
|
username: user.username,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
roles: user.profile.role
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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 });
|
|
|
|
|
|
|
|
|
|
if (loginObject.isStaff) {
|
|
|
|
|
let allMotorAccess = loginObject.staffMember.all_motor_access;
|
|
|
|
|
// Response Construction
|
|
|
|
|
const responsePayload = {
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "Login successful",
|
|
|
|
|
apiversion: fastify.config ? fastify.config.APIVERSION : undefined,
|
|
|
|
|
access_token: token,
|
|
|
|
|
buildingName: user.buildingName,
|
|
|
|
|
email: user.emails,
|
|
|
|
|
phone: user.phone,
|
|
|
|
|
customerId: user.customerId,
|
|
|
|
|
username: 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: "user"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Normalize the value if it matches the given variations
|
|
|
|
|
if (["view", "view only", "View", "View Only"].includes(allMotorAccess)) {
|
|
|
|
|
allMotorAccess = "view";
|
|
|
|
|
if (profilePicture) {
|
|
|
|
|
responsePayload.simplydata.picture = profilePicture.picture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responsePayload.simplydata.all_motor_access = allMotorAccess;
|
|
|
|
|
}
|
|
|
|
|
return reply.send(responsePayload);
|
|
|
|
|
|
|
|
|
|
if (profilePicture) {
|
|
|
|
|
responsePayload.simplydata.picture = profilePicture.picture;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Login Error:", error);
|
|
|
|
|
return reply.code(500).send({ simplydata: { error: true, message: "Internal server error" } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send(responsePayload);
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|