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,
address1,
address2,
installationId,
emails,
password,
profile,
team,
manager,
longitude,
latitude,
fcmId,
alternativeNumber,
firstName,
lastName,
city,
createdBy,
updatedBy,
} = request.body;
// Check if the email address is valid
// const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// if (emails.some((emailObj) => !emailRegex.test(emailObj.email))) {
// return reply.status(400).send({ message: 'Invalid email address' });
// }
// 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
const install = new Install({
//name,
phone,
address,
address1,
address2,
installationId,
emails,
services: { password: { bcrypt: hashedPassword } },
profile,
team,
manager,
longitude,
latitude,
fcmId,
alternativeNumber,
firstName,
lastName,
city,
createdBy,
updatedBy,
}); const {
// name,
// Save the new install to the database phone,
await install.save(); address,
address1,
reply.send({ message: 'Install Account Created Successfully' }); address2,
} catch (err) { emails,
reply.status(500).send({ message: err.message }); password,
profile,
team,
manager,
longitude,
latitude,
fcmId,
alternativeNumber,
firstName,
lastName,
city,
createdBy,
updatedBy,
} = request.body;
// 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
const install = new Install({
// name,
installationId,
phone,
address,
address1,
address2,
emails,
services: { password: { bcrypt: hashedPassword } },
profile,
team,
manager,
longitude,
latitude,
fcmId,
alternativeNumber,
firstName,
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 });
}
};
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