From f21aa8bd73ea44cd183549cd8635bc982c386e9e Mon Sep 17 00:00:00 2001 From: Naidu Date: Sun, 30 Jun 2024 13:50:42 +0530 Subject: [PATCH] changes on user login --- src/controllers/storeController.js | 154 ++++++++++++++--------------- src/models/store.js | 13 ++- src/routes/storeRoute.js | 6 +- 3 files changed, 91 insertions(+), 82 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 02d0153a..010f2a7f 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -3,7 +3,7 @@ const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const fastify = require("fastify"); -const { Install, ProfilePictureInstall } = require("../models/store"); +const { Install, ProfilePictureInstall, generateinstallationId } = require("../models/store"); const supplierController = require("../controllers/supplierController") @@ -11,104 +11,102 @@ const supplierController = require("../controllers/supplierController") exports.installSignUp = async (request, reply) => { - try { - const { - //name, - 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, + try { + const i_id = await generateinstallationId(); + const installationId = `AWIN${i_id}`; - }); - - // 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 }); + const { + // name, + phone, + address, + address1, + address2, + emails, + 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) => { try { - const { phone1, password } = request.body + const { phone, password } = request.body // Check if an admin with the email address exists - const install = await Install.findOne({ phone1 }) + const install = await Install.findOne({ phone }) 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 - const isPasswordValid = await bcrypt.compare(password, install.password) + 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({ phone1: install.phone1 }, 'secret') + const token = jwt.sign({ phone: install.phone }, 'secret') - // Return the token to the client - return { token } + // Return the token and user details to the client + return { token, user: install } } catch (err) { reply.status(500).send({ message: err.message }) } diff --git a/src/models/store.js b/src/models/store.js index 1f11d440..f0e5c692 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -1,8 +1,18 @@ const mongoose = require('mongoose') const Schema = mongoose.Schema; 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({ // name: { type: String }, @@ -74,4 +84,5 @@ const installationschema = new mongoose.Schema({ const Install = mongoose.model("Install", installationschema); - module.exports = { Install, ProfilePictureInstall}; + + module.exports = { Install, ProfilePictureInstall, generateinstallationId}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index c131b5a2..bd03ff24 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -26,6 +26,7 @@ module.exports = function (fastify, opts, next) { }, }, //name: { type: 'string' }, + team: { type: 'string', default: null }, manager: { 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", body: { type: "object", - required: ["phone1", "password"], + required: ["phone", "password"], properties: { - phone1: { type: "string" }, + phone: { type: "string" }, password: { type: "string" }, - phoneVerificationCode: { type: "string" }, }, }, },