changes on user login

master
Naidu 1 year ago
parent 1bb7817f49
commit f21aa8bd73

@ -3,7 +3,7 @@ const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const fastify = require("fastify"); const fastify = require("fastify");
const { Install, ProfilePictureInstall } = require("../models/store"); const { Install, ProfilePictureInstall, generateinstallationId } = require("../models/store");
const supplierController = require("../controllers/supplierController") const supplierController = require("../controllers/supplierController")
@ -11,104 +11,102 @@ const supplierController = require("../controllers/supplierController")
exports.installSignUp = async (request, reply) => { exports.installSignUp = async (request, reply) => {
try { try {
const { const i_id = await generateinstallationId();
//name, const installationId = `AWIN${i_id}`;
phone,
address, const {
address1, // name,
address2, phone,
installationId, address,
emails, address1,
password, address2,
profile, emails,
team, password,
manager, profile,
longitude, team,
latitude, manager,
fcmId, longitude,
alternativeNumber, latitude,
firstName, fcmId,
lastName, alternativeNumber,
city, firstName,
createdBy, lastName,
updatedBy, city,
} = request.body; createdBy,
updatedBy,
// Check if the email address is valid } = request.body;
// const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// if (emails.some((emailObj) => !emailRegex.test(emailObj.email))) { // Check if a user with the same phone number already exists
// return reply.status(400).send({ message: 'Invalid email address' }); const existingInstall = await Install.findOne({ phone });
// } if (existingInstall) {
return reply.status(400).send({ message: 'Phone is already registered' });
// Check if a user with the same phone number already exists }
const existingInstall = await Install.findOne({ phone });
if (existingInstall) {
return reply.status(400).send({ message: 'Phone is already registered' });
}
// Hash the password using bcrypt
const hashedPassword = await bcrypt.hash(password, 10);
// Create a new install object with the hashed password and other details // Hash the password using bcrypt
const install = new Install({ const hashedPassword = await bcrypt.hash(password, 10);
//name,
phone, // Create a new install object with the hashed password and other details
address, const install = new Install({
address1, // name,
address2, installationId,
installationId, phone,
emails, address,
services: { password: { bcrypt: hashedPassword } }, address1,
profile, address2,
team, emails,
manager, services: { password: { bcrypt: hashedPassword } },
longitude, profile,
latitude, team,
fcmId, manager,
alternativeNumber, longitude,
firstName, latitude,
lastName, fcmId,
city, alternativeNumber,
createdBy, firstName,
updatedBy, lastName,
city,
createdBy,
updatedBy,
});
// Save the new install to the database
await install.save();
reply.send({ message: 'Install Account Created Successfully' });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
});
// Save the new install to the database
await install.save();
reply.send({ message: 'Install Account Created Successfully' });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.installLogin = async (request, reply) => { exports.installLogin = async (request, reply) => {
try { try {
const { phone1, password } = request.body const { phone, password } = request.body
// Check if an admin with the email address exists // Check if an admin with the email address exists
const install = await Install.findOne({ phone1 }) const install = await Install.findOne({ phone })
if (!install) { if (!install) {
return reply.status(401).send({ message: 'Invalid Phone1 or password' }) 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 // Compare the password entered by the user with the hashed password stored in the database
const isPasswordValid = await bcrypt.compare(password, install.password) const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt)
if (!isPasswordValid) { if (!isPasswordValid) {
return reply.status(401).send({ message: 'Invalid phone or password' }) return reply.status(401).send({ message: 'Invalid phone or password' })
} }
// Generate a JWT token for the authenticated admin // Generate a JWT token for the authenticated admin
const token = jwt.sign({ phone1: install.phone1 }, 'secret') const token = jwt.sign({ phone: install.phone }, 'secret')
// Return the token to the client // Return the token and user details to the client
return { token } return { token, user: install }
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }) reply.status(500).send({ message: err.message })
} }

@ -1,8 +1,18 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId; const ObjectId = Schema.Types.ObjectId;
const { Counter} = require('../models/User')
const generateinstallationId = async () => {
var result = await Counter.findOneAndUpdate(
{ _id: 'installation_id' },
{ $inc: { seq: 1 } },
{ upsert: true, new: true }
);
return result.seq;
};
const installationschema = new mongoose.Schema({ const installationschema = new mongoose.Schema({
// name: { type: String }, // name: { type: String },
@ -74,4 +84,5 @@ const installationschema = new mongoose.Schema({
const Install = mongoose.model("Install", installationschema); const Install = mongoose.model("Install", installationschema);
module.exports = { Install, ProfilePictureInstall};
module.exports = { Install, ProfilePictureInstall, generateinstallationId};

@ -26,6 +26,7 @@ module.exports = function (fastify, opts, next) {
}, },
}, },
//name: { type: 'string' }, //name: { type: 'string' },
team: { type: 'string', default: null }, team: { type: 'string', default: null },
manager: { type: 'string', default: null }, manager: { type: 'string', default: null },
address: { type: 'string', default: null }, address: { type: 'string', default: null },
@ -59,11 +60,10 @@ module.exports = function (fastify, opts, next) {
summary: "This is for Login Install", summary: "This is for Login Install",
body: { body: {
type: "object", type: "object",
required: ["phone1", "password"], required: ["phone", "password"],
properties: { properties: {
phone1: { type: "string" }, phone: { type: "string" },
password: { type: "string" }, password: { type: "string" },
phoneVerificationCode: { type: "string" },
}, },
}, },
}, },

Loading…
Cancel
Save