Skip to content
Snippets Groups Projects
Commit 384e0b75 authored by AF7626's avatar AF7626
Browse files

authorization working for admin

parent 256ad2fc
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@ const loginAdmin = async (req, res) => {
if (email === process.env.ADMIN_EMAIL && password === process.env.ADMIN_PASSWORD) {
//store the web token in the variable called with JWT secret token
const token = jwt.sign(email+password,process.env.JWT_SECRET)
console.log(token);
//console.log(token);
//send a response back json format
res.json({success:true,token})
}
......
import jwt from 'jsonwebtoken';
//create a function that authenticates the admin using token
const authAdmin = async (req, res, next) => {
// get authorization token
const authorization = req.headers.authorization
//If no token is provided, return an error message
if (!authorization || !authorization.startsWith('Bearer')) {
return res.json({ success: false, message: 'User token missing or invalid!' })
} else {
const AdminToken = authorization.split(' ')[1];
try {
// verity token
const decode_Token = jwt.verify(AdminToken, process.env.JWT_SECRET)
req.decode_Token= decode_Token;
//callback function
next();
} catch (error) {
console.log(error);
res.json({ success: false, message: error.message })
}
}
}
//export the middleware
export default authAdmin;
import express from 'express';
import { addDoctor ,loginAdmin} from '../controllers/adminController.js';
import { addDoctor, loginAdmin } from '../controllers/adminController.js';
import authAdmin from '../middlewares/authAdmin.js';
// Create a new router instance for handling admin-related routes
const adminRouter = express.Router();
// This handles POST requests to the endpoint: http://localhost:3000/api/v1/admin/add-doctor
adminRouter.post('/add-doctor', addDoctor);
adminRouter.post('/add-doctor', authAdmin, addDoctor);
adminRouter.post('/login', loginAdmin);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment