Merge branch 'master' of http://35.207.205.18:3000/Arminta/watermanagement-backend
commit
0be274c3ce
@ -0,0 +1,73 @@
|
||||
const Admin = require('../models/admin')
|
||||
const boom = require("boom");
|
||||
const jwt = require('jsonwebtoken')
|
||||
const bcrypt = require('bcrypt')
|
||||
|
||||
const fastify = require("fastify");
|
||||
|
||||
exports.adminSignUp = async (request, reply) => {
|
||||
|
||||
try {
|
||||
const { email, password } = request.body
|
||||
|
||||
// Check if an admin with the same email address already exists
|
||||
const existingAdmin = await Admin.findOne({ email })
|
||||
|
||||
if (existingAdmin) {
|
||||
return reply.status(400).send({ message: 'Email already registered' })
|
||||
}
|
||||
|
||||
// Hash the password using bcrypt
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
|
||||
// Create a new admin object with the hashed password
|
||||
const admin = new Admin({ email, password: hashedPassword })
|
||||
|
||||
// Save the new admin to the database
|
||||
await admin.save()
|
||||
|
||||
// Generate a JWT token for the new admin
|
||||
// const token = jwt.sign({ email: admin.email }, 'secret')
|
||||
|
||||
// // Return the token to the client
|
||||
// return { token }
|
||||
reply.send({message : "Admin Account Created Sucessfully"})
|
||||
} catch (err) {
|
||||
reply.status(500).send({ message: err.message })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
exports.adminLogin = async (request, reply) => {
|
||||
try {
|
||||
const { email, password } = request.body
|
||||
|
||||
// Check if an admin with the email address exists
|
||||
const admin = await Admin.findOne({ email })
|
||||
|
||||
if (!admin) {
|
||||
return reply.status(401).send({ message: 'Invalid email or password' })
|
||||
}
|
||||
|
||||
// Compare the password entered by the user with the hashed password stored in the database
|
||||
const isPasswordValid = await bcrypt.compare(password, admin.password)
|
||||
|
||||
if (!isPasswordValid) {
|
||||
return reply.status(401).send({ message: 'Invalid email or password' })
|
||||
}
|
||||
|
||||
// Generate a JWT token for the authenticated admin
|
||||
const token = jwt.sign({ email: admin.email }, 'secret')
|
||||
|
||||
// Return the token to the client
|
||||
return { token }
|
||||
} catch (err) {
|
||||
reply.status(500).send({ message: err.message })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,18 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const adminSchema = new mongoose.Schema({
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
lowercase: true
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
})
|
||||
|
||||
const Admin = mongoose.model('Admin', adminSchema)
|
||||
|
||||
module.exports = Admin
|
@ -0,0 +1,51 @@
|
||||
const fastify = require("fastify");
|
||||
const adminController = require('../controllers/admincontroller')
|
||||
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.route({
|
||||
method: "POST",
|
||||
url: "/api/adminSignup",
|
||||
schema: {
|
||||
tags: ["Admin"],
|
||||
description: "This is for cretae New Admin Account",
|
||||
summary: "This is for cretae New Admin Account",
|
||||
body: {
|
||||
type: "object",
|
||||
properties: {
|
||||
email: { type: "string" },
|
||||
password: { type: "string" },
|
||||
|
||||
},
|
||||
},
|
||||
security: [
|
||||
{
|
||||
basicAuth: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
handler: adminController.adminSignUp,
|
||||
|
||||
});
|
||||
|
||||
fastify.post("/api/adminLogin", {
|
||||
schema: {
|
||||
description: "This is for Login Admin",
|
||||
tags: ["Admin"],
|
||||
summary: "This is for Login Admin",
|
||||
body: {
|
||||
type: "object",
|
||||
required: ["email", "password"],
|
||||
properties: {
|
||||
email: { type: "string" },
|
||||
password: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
handler: adminController.adminLogin,
|
||||
});
|
||||
|
||||
|
||||
next();
|
||||
};
|
Loading…
Reference in new issue