login responses chnages

master^2
Bhaskar 5 months ago
parent 1872c9ae35
commit 63f418c70a

@ -1435,34 +1435,29 @@ fastify.post("/api/teamMemberLogin", {
summary: "This is for Login Support", summary: "This is for Login Support",
body: { body: {
type: "object", type: "object",
required: [ "phone", "password"], required: ["phone", "password", "type"],
properties: { properties: {
phone: { type: "string", description: "Registered phone number" }, phone: { type: "string", description: "Registered phone number" },
password: { type: "string", description: "Password for authentication" }, password: { type: "string", description: "Password for authentication" },
type: { type: "string", description: "Role of the user (e.g., 'Support_Manager')" },
}, },
}, },
}, },
async handler(req, reply) { async handler(req, reply) {
try { try {
const { phone, password } = req.body; const { phone, password, type } = req.body;
// Check if user exists in the Department Schema
const user = await Deparments.findOne({ phone }); const user = await Deparments.findOne({ phone });
console.log("user", user)
if (!user) { if (!user) {
return reply.code(400).send({ message: "User not found" }); return reply.code(400).send({ message: "User not found" });
} }
// Verify Password
const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
if (!isMatch) { if (!isMatch) {
return reply.code(400).send({ message: "Invalid credentials" }); return reply.code(400).send({ message: "Invalid credentials" });
} }
let survey = await Support.findOne({ phone }); let survey = await Support.findOne({ phone });
if (!survey) { if (!survey) {
survey = new Support({ survey = new Support({
phone: user.phone, phone: user.phone,
@ -1481,15 +1476,17 @@ fastify.post("/api/teamMemberLogin", {
profile: { profile: {
state: user.state, state: user.state,
country: user.country, country: user.country,
//role: type, // Store type in profile.role
}, },
}); });
await survey.save(); await survey.save();
} }
const token = fastify.jwt.sign( const token = fastify.jwt.sign(
{ phone: user.phone }, {
userId: user._id,
phone: user.phone,
role: type
},
"Scret", "Scret",
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
@ -1497,35 +1494,35 @@ fastify.post("/api/teamMemberLogin", {
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
message: "Login successful", apiversion: "1.0.0",
access_token: token, access_token: token,
email: user.email || [],
installationId: user.departmentId || "",
phone: user.phone, phone: user.phone,
supportId: user.departmentId, address1: user.address1 || "",
firstName: user.firstName, address2: user.address2 || "",
lastName: user.lastName, phoneVerified: false,
email: user.email, oneTimePasswordSetFlag: false,
alternativeNumber: user.alternativeContactNumber, type: type, // <== returned from the request
departmentName: user.departmentName, fcmIds: null,
designation: user.desginationName, team: null,
reportingManager: user.reportingManager, city: user.city || "",
city: user.city, manager: user.reportingManager || null,
zone: user.zone, firstName: user.firstName || "",
address1: user.address1, lastName: user.lastName || "",
address2: user.address2, address: "",
profile: { alternativeNumber: user.alternativeContactNumber || "",
state: user.state, profilePicture: null
country: user.country, }
//role: type, // Store type in profile.role
},
},
}); });
// return reply.send(survey);
} catch (error) { } catch (error) {
console.error("Login Error:", error); console.error("Login Error:", error);
return reply.code(500).send({ message: "Internal server error" }); return reply.code(500).send({ message: "Internal server error" });
} }
}, },
}); });
fastify.post("/api/supportTeamMemberLogin", { fastify.post("/api/supportTeamMemberLogin", {
schema: { schema: {
description: "Login Support TeamMember", description: "Login Support TeamMember",
@ -1537,7 +1534,7 @@ fastify.post("/api/teamMemberLogin", {
properties: { properties: {
type: { type: {
type: "string", type: "string",
description: "Role type of the user (e.g., 'team_member')" description: "Role type of the user (e.g., 'Support_TeamMember')"
}, },
phone: { phone: {
type: "string", type: "string",
@ -1588,17 +1585,35 @@ fastify.post("/api/teamMemberLogin", {
support_teamMemberId: teamMember.support_teamMemberId, support_teamMemberId: teamMember.support_teamMemberId,
name: teamMember.name, name: teamMember.name,
phone: teamMember.phone, phone: teamMember.phone,
type: "Support_TeamMember" type: type
}, },
"JWT_SECRET", "JWT_SECRET",
{ expiresIn: '7d' } { expiresIn: '7d' }
); );
return reply.send({ return reply.send({
status_code: 200, simplydata: {
message: 'Login successful', error: false,
token, apiversion: "1.0.0",
teamMember access_token: token,
email: [],
installationId: teamMember.support_teamMemberId || "",
phone: teamMember.phone,
address1: teamMember.address1 || "",
address2: teamMember.address2 || "",
phoneVerified: false,
oneTimePasswordSetFlag: false,
type: type,
fcmIds: null,
team: null,
city: teamMember.city || "",
manager: null,
firstName: teamMember.name?.split(" ")[0] || "",
lastName: teamMember.name?.split(" ")[1] || "",
address: "",
alternativeNumber: teamMember.alternativeNumber || "",
profilePicture: null
}
}); });
} catch (error) { } catch (error) {
@ -1610,6 +1625,7 @@ fastify.post("/api/teamMemberLogin", {
fastify.post("/api/storelogin", { fastify.post("/api/storelogin", {
schema: { schema: {
description: "This is for Store Login", description: "This is for Store Login",

Loading…
Cancel
Save