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 jwt = require('jsonwebtoken');
const fastify = require("fastify");
const { Install, ProfilePictureInstall, generateinstallationId } = require("../models/store");
const fastify = require("fastify")({
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")
@ -84,33 +90,80 @@ exports.installSignUp = async (request, reply) => {
exports.installLogin = async (request, reply) => {
try {
const { phone, password } = request.body
// Check if an admin with the email address exists
const install = await Install.findOne({ phone })
if (!install) {
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)
if (!isPasswordValid) {
return reply.status(401).send({ message: 'Invalid phone or password' })
}
// Generate a JWT token for the authenticated admin
const token = jwt.sign({ phone: install.phone }, 'secret')
// exports.installLogin = async (request, reply) => {
// try {
// const { phone, password } = request.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
// }
// });
// }
// };
// Return the token and user details to the client
return { token, user: install }
} catch (err) {
reply.status(500).send({ message: err.message })
}
}

@ -6,6 +6,8 @@ const tankersController = require("./controllers/tankersController.js");
const createConnectionController = require("./controllers/createConnectionController");
const storeController = require("./controllers/storeController.js")
const boom = require("boom");
const bcrypt = require('bcrypt');
const cors = require("cors");
const swagger = require("./config/swagger");
@ -355,6 +357,7 @@ console.log(user)
apiversion: fastify.config.APIVERSION,
access_token: token,
email: user.emails,
installationId: user.installationId,
phone: user.phone,
//name: user.name,
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!
const start = async () => {

@ -53,22 +53,22 @@ module.exports = function (fastify, opts, next) {
},
handler: storeController.installSignUp,
});
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" },
},
},
},
handler: storeController.installLogin,
});
// 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" },
// },
// },
// },
// handler: storeController.installLogin,
// });
// fastify.post("/api/installotplogin", {
// schema: {

Loading…
Cancel
Save