installation login

master^2
Bhaskar 7 months ago
parent 40ac93a540
commit 421f16c812

@ -0,0 +1,17 @@
const boom = require("boom");
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department");
const { Install } = require("../models/store");
const fastify = require("fastify")({
logger: true,
//disableRequestLogging: true,
genReqId(req) {
// you get access to the req here if you need it - must be a synchronous function
return uuidv4();
},
});

@ -616,7 +616,7 @@ const {Storage} = require('@google-cloud/storage');
const { Supplier, profilePictureSupplier } = require("./models/supplier");
const multer = require('fastify-multer');
const { ProfilePictureInstall, Install } = require("./models/store.js");
const { TeamMemberProfilePicture, CompanyProfilePicture } = require("./models/Department.js");
const { TeamMemberProfilePicture, CompanyProfilePicture, Deparments } = require("./models/Department.js");
fastify.register(require('fastify-formbody'));
// fastify.register(multer.contentParser);
// const multipart = require('fastify-multipart');
@ -941,73 +941,86 @@ fastify.post("/api/insatllLogin", {
try {
const { phone, password } = req.body;
// Check if an install with the phone number exists
const install = await Install.findOne({ phone });
// Check if user exists in the Department Schema
const user = await Deparments.findOne({ phone });
if (!install) {
return reply.status(401).send({
simplydata: {
error: true,
message: 'Invalid Phone or password'
}
});
if (!user) {
return reply.code(400).send({ message: "User not found" });
}
// Compare the password entered by the user with the hashed password stored in the database
const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt);
// Verify Password
const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
if (!isPasswordValid) {
return reply.status(401).send({
simplydata: {
error: true,
message: 'Invalid phone or password'
if (!isMatch) {
return reply.code(400).send({ message: "Invalid credentials" });
}
// Check if department details already exist in installationschema
let installation = await Install.findOne({ phone });
if (!installation) {
// Create a new entry in installationschema with departmentId as installationId
installation = new Install({
phone: user.phone,
installationId: user.departmentId, // Store departmentId in installationId
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
alternativeNumber: user.alternativeContactNumber,
departmentName: user.departmentName,
designation: user.desginationName,
reportingManager: user.reportingManager,
city: user.city,
zone: user.zone,
address1: user.address1,
address2: user.address2,
profile: {
state: user.state,
country: user.country,
},
});
await installation.save();
}
// Generate a JWT token for the authenticated install
const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' });
// Fetch profile picture if available
const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id });
// Fetch the profile picture if it exists
const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id });
// Generate JWT Token
const token =fastify.jwt.sign({ userId: user._id, phone: user.phone }, "your_secret_key", {
expiresIn: "7d",
});
// Construct response payload
const responsePayload = {
simplydata: {
error: false,
apiversion: fastify.config.APIVERSION,
access_token: token,
email: install.emails,
installationId: install.installationId,
phone: install.phone,
address1: install.address1,
address2: install.address2,
phoneVerified: install.phoneVerified,
oneTimePasswordSetFlag: install.oneTimePasswordSetFlag,
type: install.profile.role,
fcmIds: install.fcmIds,
team: install.team,
city: install.city,
manager: install.manager,
firstName: install.firstName,
lastName: install.lastName,
address: install.address,
alternativeNumber: install.alternativeNumber,
email: installation.emails || [],
installationId: installation.installationId,
phone: installation.phone,
address1: installation.address1,
address2: installation.address2,
phoneVerified: installation.phoneVerified,
oneTimePasswordSetFlag: installation.oneTimePasswordSetFlag,
type: installation.profile?.role || "user", // Default to "user" if not available
fcmIds: installation.fcmId || null,
team: installation.team,
city: installation.city,
manager: installation.manager,
firstName: installation.firstName,
lastName: installation.lastName,
address: installation.address || "",
alternativeNumber: installation.alternativeNumber || null,
profilePicture: profilePicture ? profilePicture.pictureUrl : null, // Include profile picture URL if available
}
};
if (profilePicture) {
responsePayload.simplydata.picture = profilePicture.picture;
}
// Return the token and user details to the client
return reply.send(responsePayload);
} catch (err) {
reply.status(500).send({
simplydata: {
error: true,
message: err.message
}
});
} catch (error) {
console.error("Login Error:", error);
return reply.code(500).send({ message: "Internal server error" });
}
},});

@ -54,6 +54,21 @@ const installationschema = new mongoose.Schema({
},
team : { type: String, default: null},
manager : { type: String, default: null},
team_member: {
team_member: [
{
name: { type: String },
phone: { type: String },
installationTeamMemId: { type: String },
password: { type: String, default: null },
status: { type: String, default: "active" },
}
],
},
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},

@ -0,0 +1,7 @@
const installationController = require("../controllers/installationController")
module.exports = function (fastify, opts, next) {
next();
}
Loading…
Cancel
Save