|
|
|
@ -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")
|
|
|
|
|
|
|
|
|
@ -12,13 +12,15 @@ const supplierController = require("../controllers/supplierController")
|
|
|
|
|
|
|
|
|
|
exports.installSignUp = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const i_id = await generateinstallationId();
|
|
|
|
|
const installationId = `AWIN${i_id}`;
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
// name,
|
|
|
|
|
phone,
|
|
|
|
|
address,
|
|
|
|
|
address1,
|
|
|
|
|
address2,
|
|
|
|
|
installationId,
|
|
|
|
|
emails,
|
|
|
|
|
password,
|
|
|
|
|
profile,
|
|
|
|
@ -35,12 +37,6 @@ exports.installSignUp = async (request, reply) => {
|
|
|
|
|
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) {
|
|
|
|
@ -53,11 +49,11 @@ exports.installSignUp = async (request, reply) => {
|
|
|
|
|
// Create a new install object with the hashed password and other details
|
|
|
|
|
const install = new Install({
|
|
|
|
|
// name,
|
|
|
|
|
installationId,
|
|
|
|
|
phone,
|
|
|
|
|
address,
|
|
|
|
|
address1,
|
|
|
|
|
address2,
|
|
|
|
|
installationId,
|
|
|
|
|
emails,
|
|
|
|
|
services: { password: { bcrypt: hashedPassword } },
|
|
|
|
|
profile,
|
|
|
|
@ -72,7 +68,6 @@ exports.installSignUp = async (request, reply) => {
|
|
|
|
|
city,
|
|
|
|
|
createdBy,
|
|
|
|
|
updatedBy,
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Save the new install to the database
|
|
|
|
@ -86,29 +81,32 @@ exports.installSignUp = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 })
|
|
|
|
|
}
|
|
|
|
|