You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
1 year ago
|
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 })
|
||
|
}
|
||
|
}
|
||
|
|