store login creation

master^2
Bhaskar 7 months ago
parent da1aeec667
commit cd8e951e7f

@ -411,105 +411,105 @@ console.log(user)
}); });
fastify.post("/api/storelogin", { // fastify.post("/api/storelogin", {
schema: { // schema: {
description: "This is for Store Login", // description: "This is for Store Login",
tags: ["Store-Data"], // tags: ["Store-Data"],
summary: "This is for Store Login", // summary: "This is for Store Login",
body: { // body: {
type: "object", // type: "object",
required: ["phone", "password"], // required: ["phone", "password"],
properties: { // properties: {
phone: { type: "string" }, // phone: { type: "string" },
password: { type: "string" }, // password: { type: "string" },
}, // },
}, // },
}, // },
async handler(request, reply) { // async handler(request, reply) {
try { // try {
let store = await Store.findOne({ phone: request.body.phone }); // let store = await Store.findOne({ phone: request.body.phone });
if (!store) { // if (!store) {
return reply.code(400).send({ // return reply.code(400).send({
simplydata: { // simplydata: {
error: true, // error: true,
code: 400, // code: 400,
message: "Invalid Phone or Password", // message: "Invalid Phone or Password",
}, // },
}); // });
} // }
const isMatch = await bcrypt.compare(request.body.password, store.services.password.bcrypt); // const isMatch = await bcrypt.compare(request.body.password, store.services.password.bcrypt);
if (!isMatch) { // if (!isMatch) {
return reply.code(400).send({ // return reply.code(400).send({
simplydata: { // simplydata: {
error: true, // error: true,
code: 400, // code: 400,
message: "Invalid Phone or Password", // message: "Invalid Phone or Password",
}, // },
}); // });
} // }
const token = fastify.jwt.sign( // const token = fastify.jwt.sign(
{ // {
storename: store.storename, // storename: store.storename,
storeId: store._id, // storeId: store._id,
roles: store.profile.role, // roles: store.profile.role,
}, // },
{ expiresIn: "30d" } // { expiresIn: "30d" }
); // );
var profilePicture = await ProfilePictureStore.findOne({ storeId: store.storeId }); // var profilePicture = await ProfilePictureStore.findOne({ storeId: store.storeId });
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,
email: store.emails, // email: store.emails,
phone: store.phone, // phone: store.phone,
storeId: store.storeId, // storeId: store.storeId,
storename: store.storename, // storename: store.storename,
office_address: store.profile.office_address, // office_address: store.profile.office_address,
phoneVerified: store.phoneVerified, // phoneVerified: store.phoneVerified,
oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, // oneTimePasswordSetFlag: store.oneTimePasswordSetFlag,
latitude: store.latitude, // latitude: store.latitude,
longitude: store.longitude, // longitude: store.longitude,
description: store.description, // description: store.description,
type: store.profile.role, // type: store.profile.role,
typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), // typeasobj: JSON.stringify(Object.assign({}, store.profile.role)),
}, // },
}); // });
} else { // } 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: store.emails, // email: store.emails,
phone: store.phone, // phone: store.phone,
storeId: store.storeId, // storeId: store.storeId,
storename: store.storename, // storename: store.storename,
office_address: store.profile.office_address, // office_address: store.profile.office_address,
phoneVerified: store.phoneVerified, // phoneVerified: store.phoneVerified,
oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, // oneTimePasswordSetFlag: store.oneTimePasswordSetFlag,
latitude: store.latitude, // latitude: store.latitude,
longitude: store.longitude, // longitude: store.longitude,
description: store.description, // description: store.description,
type: store.profile.role, // type: store.profile.role,
typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), // typeasobj: JSON.stringify(Object.assign({}, store.profile.role)),
}, // },
}); // });
} // }
} catch (err) { // } catch (err) {
throw boom.boomify(err); // throw boom.boomify(err);
} // }
}, // },
}); // });
fastify.get("/api/reset_token/:customerId", { fastify.get("/api/reset_token/:customerId", {
@ -1216,6 +1216,106 @@ fastify.post("/api/installLogin", {
} }
}, },
}); });
fastify.post("/api/storelogin", {
schema: {
description: "This is for Store Login",
tags: ["Store-Data"],
summary: "This is for Store Login",
body: {
type: "object",
required: [ "phone", "password"],
properties: {
phone: { type: "string", description: "Registered phone number" },
password: { type: "string", description: "Password for authentication" },
},
},
},
async handler(req, reply) {
try {
const { phone, password } = 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 store = await Store.findOne({ phone });
if (!store) {
store = new Survey({
phone: user.phone,
storeId: 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
},
});
await store.save();
}
const token = fastify.jwt.sign(
{ phone: user.phone },
"Scret",
{ expiresIn: "1h" }
);
return reply.send({
simplydata: {
error: false,
message: "Login successful",
access_token: token,
phone: user.phone,
storeId: 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
},
},
});
// return reply.send(survey);
} catch (error) {
console.error("Login Error:", error);
return reply.code(500).send({ message: "Internal server error" });
}
},
});

Loading…
Cancel
Save