From 143063557f4234206619e12e795c3eec0dd021e0 Mon Sep 17 00:00:00 2001 From: Naidu Date: Thu, 27 Jun 2024 14:40:58 +0530 Subject: [PATCH] Store Signup and login --- src/controllers/storeController.js | 71 ++++++++++++++++++++++++++++++ src/index.js | 3 ++ src/models/store.js | 55 +++++++++++++++++++++++ src/routes/storeRoute.js | 63 ++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 src/controllers/storeController.js create mode 100644 src/models/store.js create mode 100644 src/routes/storeRoute.js diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js new file mode 100644 index 00000000..bbaac118 --- /dev/null +++ b/src/controllers/storeController.js @@ -0,0 +1,71 @@ +const boom = require("boom"); +const jwt = require('jsonwebtoken') +const bcrypt = require('bcrypt') + +const fastify = require("fastify"); +const { Store } = require("../models/store"); + +exports.storeSignUp = async (request, reply) => { + + try { + const { phone1,name,phone2, city,team,manager,picture,email, password } = request.body + + // Check if the email address is valid + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ + if (!emailRegex.test(email)) { + return reply.status(400).send({ message: 'Invalid email address' }) + } + + // Check if an admin with the same email address already exists + const existingstore = await Store.findOne({ phone1 }) + + if (existingstore) { + 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 admin object with the hashed password + const store = new Store({ phone1,name,phone2, city,team,manager,picture,email, password: hashedPassword }) + + // Save the new admin to the database + await store.save() + + + reply.send({message : "Store Account Created Sucessfully"}) + } catch (err) { + reply.status(500).send({ message: err.message }) + } + } + + + + exports.storeLogin = async (request, reply) => { + try { + const { phone1, password } = request.body + + // Check if an admin with the email address exists + const store = await Store.findOne({ phone1 }) + + if (!store) { + return reply.status(401).send({ message: 'Invalid Phone1 or password' }) + } + + // Compare the password entered by the user with the hashed password stored in the database + const isPasswordValid = await bcrypt.compare(password, store.password) + + 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: store.phone1 }, 'secret') + + // Return the token to the client + return { token } + } catch (err) { + reply.status(500).send({ message: err.message }) + } + } + diff --git a/src/index.js b/src/index.js index e9df1586..e08cba81 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,8 @@ const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfileP //const tanksController = require("./controllers/tanksController"); const tankersController = require("./controllers/tankersController.js"); const createConnectionController = require("./controllers/createConnectionController"); +const storeController = require("./controllers/storeController.js") + const cors = require("cors"); const swagger = require("./config/swagger"); const rawBody = require('raw-body') @@ -346,6 +348,7 @@ fastify.register(require("./routes/supplierRoute")); fastify.register(require("./routes/supplierOrdersRoutes")); fastify.register(require("./routes/friendRequestRoute")); fastify.register(require("./routes/adminRoute")); +fastify.register(require("./routes/storeRoute")); // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number diff --git a/src/models/store.js b/src/models/store.js new file mode 100644 index 00000000..24eabcc7 --- /dev/null +++ b/src/models/store.js @@ -0,0 +1,55 @@ +const mongoose = require('mongoose') +const Schema = mongoose.Schema; +const ObjectId = Schema.Types.ObjectId; + + + +const storeschema = new mongoose.Schema({ + name: { + type: String, + required: true, + + }, + email: { + type: String, + required: true, + unique: true, + lowercase: true + }, + + phone1: { + type: String, + default: false, + + }, + phone2: { + type: String, + default: false, + + }, + city: { + type: String, + required: true, + }, + team:{ + type:String + + }, + location: { + type:String, + default: false, + }, + picture: + { type: String }, + manager:{ + type: String, + default:false, + }, + password: + { type: String}, + + }); + + const Store = mongoose.model("Store", storeschema); + + module.exports = { Store}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js new file mode 100644 index 00000000..4935bcc5 --- /dev/null +++ b/src/routes/storeRoute.js @@ -0,0 +1,63 @@ +const fastify = require("fastify"); +const storeController = require('../controllers/storeController') + + +module.exports = function (fastify, opts, next) { +fastify.route({ + method: "POST", + url: "/api/storeSignup", + schema: { + tags: ["Store"], + description: "This is for cretae New Store Account", + summary: "This is for cretae New Store Account", + body: { + type: "object", + properties: { + name:{ type: "string"}, + phone1: { type: "string"}, + phone2: { type: "string"}, + city: { type: "string"}, + location: { type: "string"}, + picture: { type: "string"}, + team: { type: "string"}, + manager: { type: "string"}, + + email: { type: "string" }, + password: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + + handler: storeController.storeSignUp, + + }); + + fastify.post("/api/storeLogin", { + schema: { + description: "This is for Login Store", + tags: ["Store"], + summary: "This is for Login Store", + body: { + type: "object", + required: ["phone1", "password"], + properties: { + phone1: { type: "string" }, + password: { type: "string" }, + }, + }, + }, + handler: storeController.storeLogin, +}); + + + + + +next(); +};