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")
@ -12,13 +12,15 @@ const supplierController = require("../controllers/supplierController")
exports.installSignUp = async (request, reply) => { exports.installSignUp = async (request, reply) => {
try { try {
const i_id = await generateinstallationId();
const installationId = `AWIN${i_id}`;
const { const {
//name, // name,
phone, phone,
address, address,
address1, address1,
address2, address2,
installationId,
emails, emails,
password, password,
profile, profile,
@ -35,12 +37,6 @@ exports.installSignUp = async (request, reply) => {
updatedBy, updatedBy,
} = request.body; } = 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 // Check if a user with the same phone number already exists
const existingInstall = await Install.findOne({ phone }); const existingInstall = await Install.findOne({ phone });
if (existingInstall) { if (existingInstall) {
@ -52,12 +48,12 @@ exports.installSignUp = async (request, reply) => {
// Create a new install object with the hashed password and other details // Create a new install object with the hashed password and other details
const install = new Install({ const install = new Install({
//name, // name,
installationId,
phone, phone,
address, address,
address1, address1,
address2, address2,
installationId,
emails, emails,
services: { password: { bcrypt: hashedPassword } }, services: { password: { bcrypt: hashedPassword } },
profile, profile,
@ -72,7 +68,6 @@ exports.installSignUp = async (request, reply) => {
city, city,
createdBy, createdBy,
updatedBy, updatedBy,
}); });
// Save the new install to the database // Save the new install to the database
@ -82,33 +77,36 @@ exports.installSignUp = async (request, reply) => {
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); 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