modified access token & simply data

master
Naidu 1 year ago
parent f21aa8bd73
commit 2218d1b38f

@ -2,8 +2,14 @@ const boom = require("boom");
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const fastify = require("fastify"); const fastify = require("fastify")({
const { Install, ProfilePictureInstall, generateinstallationId } = require("../models/store"); logger: true,
//disableRequestLogging: true,
genReqId(req) {
// you get access to the req here if you need it - must be a synchronous function
return uuidv4();
},
});const { Install, ProfilePictureInstall, generateinstallationId } = require("../models/store");
const supplierController = require("../controllers/supplierController") const supplierController = require("../controllers/supplierController")
@ -84,33 +90,80 @@ exports.installSignUp = async (request, reply) => {
exports.installLogin = async (request, reply) => { // exports.installLogin = async (request, reply) => {
try { // try {
const { phone, password } = request.body // const { phone, password } = request.body;
// Check if an admin with the email address exists // // Check if an install with the phone number exists
const install = await Install.findOne({ phone }) // const install = await Install.findOne({ phone });
if (!install) { // if (!install) {
return reply.status(401).send({ message: 'Invalid Phone or password' }) // return reply.status(401).send({
} // simplydata: {
// error: true,
// Compare the password entered by the user with the hashed password stored in the database // message: 'Invalid Phone or password'
const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt) // }
// });
if (!isPasswordValid) { // }
return reply.status(401).send({ message: 'Invalid phone or password' })
} // // Compare the password entered by the user with the hashed password stored in the database
// const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt);
// Generate a JWT token for the authenticated admin
const token = jwt.sign({ phone: install.phone }, 'secret') // if (!isPasswordValid) {
// return reply.status(401).send({
// Return the token and user details to the client // simplydata: {
return { token, user: install } // error: true,
} catch (err) { // message: 'Invalid phone or password'
reply.status(500).send({ message: err.message }) // }
} // });
} // }
// // Generate a JWT token for the authenticated install
// const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' });
// // Fetch the profile picture if it exists
// const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id });
// const responsePayload = {
// simplydata: {
// error: false,
// apiversion: fastify.config.APIVERSION,
// access_token: token,
// email: install.emails,
// installationId: install.installationId,
// phone: install.phone,
// address1: install.address1,
// address2: install.address2,
// phoneVerified: install.phoneVerified,
// oneTimePasswordSetFlag: install.oneTimePasswordSetFlag,
// type: install.profile.role,
// fcmId: install.fcmId,
// team: install.team,
// city: install.city,
// manager: install.manager,
// firstName: install.firstName,
// lastName: install.lastName,
// address: install.address,
// alternativeNumber: install.alternativeNumber,
// }
// };
// if (profilePicture) {
// responsePayload.simplydata.picture = profilePicture.picture;
// }
// // Return the token and user details to the client
// return reply.send(responsePayload);
// } catch (err) {
// reply.status(500).send({
// simplydata: {
// error: true,
// message: err.message
// }
// });
// }
// };

@ -6,6 +6,8 @@ const tankersController = require("./controllers/tankersController.js");
const createConnectionController = require("./controllers/createConnectionController"); const createConnectionController = require("./controllers/createConnectionController");
const storeController = require("./controllers/storeController.js") const storeController = require("./controllers/storeController.js")
const boom = require("boom"); const boom = require("boom");
const bcrypt = require('bcrypt');
const cors = require("cors"); const cors = require("cors");
const swagger = require("./config/swagger"); const swagger = require("./config/swagger");
@ -355,6 +357,7 @@ console.log(user)
apiversion: fastify.config.APIVERSION, apiversion: fastify.config.APIVERSION,
access_token: token, access_token: token,
email: user.emails, email: user.emails,
installationId: user.installationId,
phone: user.phone, phone: user.phone,
//name: user.name, //name: user.name,
address1: user.address1, address1: user.address1,
@ -732,6 +735,94 @@ fastify.post('/api/uploads-user/:customerId', async (request, reply) => {
} }
}); });
fastify.post("/api/insatllLogin", {
schema: {
description: "This is for Login Install",
tags: ["Install"],
summary: "This is for Login Install",
body: {
type: "object",
required: ["phone", "password"],
properties: {
phone: { type: "string" },
password: { type: "string" },
},
},
},
async handler(req, reply) {
try {
const { phone, password } = req.body;
// Check if an install with the phone number exists
const install = await Install.findOne({ phone });
if (!install) {
return reply.status(401).send({
simplydata: {
error: true,
message: 'Invalid Phone or password'
}
});
}
// Compare the password entered by the user with the hashed password stored in the database
const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt);
if (!isPasswordValid) {
return reply.status(401).send({
simplydata: {
error: true,
message: 'Invalid phone or password'
}
});
}
// Generate a JWT token for the authenticated install
const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' });
// Fetch the profile picture if it exists
const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id });
const responsePayload = {
simplydata: {
error: false,
apiversion: fastify.config.APIVERSION,
access_token: token,
email: install.emails,
installationId: install.installationId,
phone: install.phone,
address1: install.address1,
address2: install.address2,
phoneVerified: install.phoneVerified,
oneTimePasswordSetFlag: install.oneTimePasswordSetFlag,
type: install.profile.role,
fcmId: install.fcmId,
team: install.team,
city: install.city,
manager: install.manager,
firstName: install.firstName,
lastName: install.lastName,
address: install.address,
alternativeNumber: install.alternativeNumber,
}
};
if (profilePicture) {
responsePayload.simplydata.picture = profilePicture.picture;
}
// Return the token and user details to the client
return reply.send(responsePayload);
} catch (err) {
reply.status(500).send({
simplydata: {
error: true,
message: err.message
}
});
}
},});
// Run the server! // Run the server!
const start = async () => { const start = async () => {

@ -53,22 +53,22 @@ module.exports = function (fastify, opts, next) {
}, },
handler: storeController.installSignUp, handler: storeController.installSignUp,
}); });
fastify.post("/api/insatllLogin", { // fastify.post("/api/insatllLogin", {
schema: { // schema: {
description: "This is for Login Install", // description: "This is for Login Install",
tags: ["Install"], // tags: ["Install"],
summary: "This is for Login Install", // summary: "This is for Login Install",
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" },
}, // },
}, // },
}, // },
handler: storeController.installLogin, // handler: storeController.installLogin,
}); // });
// fastify.post("/api/installotplogin", { // fastify.post("/api/installotplogin", {
// schema: { // schema: {

Loading…
Cancel
Save