From 63f418c70a87f629a97a5f359b165507d00446f0 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 21 May 2025 15:08:13 +0530 Subject: [PATCH] login responses chnages --- src/index.js | 96 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/src/index.js b/src/index.js index 664bfac8..2279199b 100644 --- a/src/index.js +++ b/src/index.js @@ -1435,38 +1435,33 @@ fastify.post("/api/teamMemberLogin", { summary: "This is for Login Support", body: { type: "object", - required: [ "phone", "password"], + required: ["phone", "password", "type"], properties: { phone: { type: "string", description: "Registered phone number" }, password: { type: "string", description: "Password for authentication" }, + type: { type: "string", description: "Role of the user (e.g., 'Support_Manager')" }, }, }, }, async handler(req, reply) { 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 }); - console.log("user", user) - if (!user) { return reply.code(400).send({ message: "User not found" }); } - // Verify Password const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); - if (!isMatch) { return reply.code(400).send({ message: "Invalid credentials" }); } let survey = await Support.findOne({ phone }); - if (!survey) { survey = new Support({ phone: user.phone, - supportId: user.departmentId, + supportId: user.departmentId, firstName: user.firstName, lastName: user.lastName, email: user.email, @@ -1481,51 +1476,53 @@ fastify.post("/api/teamMemberLogin", { profile: { state: user.state, country: user.country, - //role: type, // Store type in profile.role }, }); - await survey.save(); } - + const token = fastify.jwt.sign( - { phone: user.phone }, + { + userId: user._id, + phone: user.phone, + role: type + }, "Scret", { expiresIn: "1h" } ); - + return reply.send({ simplydata: { error: false, - message: "Login successful", + apiversion: "1.0.0", access_token: token, + email: user.email || [], + installationId: user.departmentId || "", phone: user.phone, - supportId: user.departmentId, - firstName: user.firstName, - lastName: user.lastName, - email: user.email, - alternativeNumber: user.alternativeContactNumber, - departmentName: user.departmentName, - designation: user.desginationName, - reportingManager: user.reportingManager, - city: user.city, - zone: user.zone, - address1: user.address1, - address2: user.address2, - profile: { - state: user.state, - country: user.country, - //role: type, // Store type in profile.role - }, - }, + address1: user.address1 || "", + address2: user.address2 || "", + phoneVerified: false, + oneTimePasswordSetFlag: false, + type: type, // <== returned from the request + fcmIds: null, + team: null, + city: user.city || "", + manager: user.reportingManager || null, + firstName: user.firstName || "", + lastName: user.lastName || "", + address: "", + alternativeNumber: user.alternativeContactNumber || "", + profilePicture: null + } }); - // return reply.send(survey); + } catch (error) { console.error("Login Error:", error); return reply.code(500).send({ message: "Internal server error" }); } }, }); + fastify.post("/api/supportTeamMemberLogin", { schema: { description: "Login Support TeamMember", @@ -1537,7 +1534,7 @@ fastify.post("/api/teamMemberLogin", { properties: { type: { type: "string", - description: "Role type of the user (e.g., 'team_member')" + description: "Role type of the user (e.g., 'Support_TeamMember')" }, phone: { type: "string", @@ -1588,17 +1585,35 @@ fastify.post("/api/teamMemberLogin", { support_teamMemberId: teamMember.support_teamMemberId, name: teamMember.name, phone: teamMember.phone, - type: "Support_TeamMember" + type: type }, "JWT_SECRET", { expiresIn: '7d' } ); return reply.send({ - status_code: 200, - message: 'Login successful', - token, - teamMember + simplydata: { + error: false, + apiversion: "1.0.0", + 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) { @@ -1609,6 +1624,7 @@ fastify.post("/api/teamMemberLogin", { }); + fastify.post("/api/storelogin", { schema: {