parent
4856e74bf0
commit
143063557f
@ -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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -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};
|
||||||
@ -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();
|
||||||
|
};
|
||||||
Loading…
Reference in new issue