High-Table-Consortium / fullstack-capstone-dockerized

0 stars 3 forks source link

Add Admin Routes for Registration, Login, and Protected Access #31

Closed Tumelo2748 closed 1 month ago

Tumelo2748 commented 1 month ago

Description:

This pull request introduces new routes for managing admin functions within the Express application. The routes include registration, login, and a protected endpoint accessible only to authenticated admins.

Changes Made:

  1. Added /register Route:

    • Method: POST
    • Handler: registerAdmin from adminController
    • Description: Allows new admins to register.
  2. Added /login Route:

    • Method: POST
    • Handler: loginAdmin from adminController
    • Description: Handles admin login and generates an authentication token.
  3. Added /protected Route:

    • Method: GET
    • Middleware: authenticateToken and isAdmin
    • Description: A protected route accessible only to admins with a valid token.

Code:


const express = require('express');
const router = express.Router();
const { registerAdmin, loginAdmin } = require('../controllers/adminController');
const { authenticateToken, isAdmin } = require('../middleware/authMiddleware');

// Route to handle admin registration
router.post('/register', registerAdmin);

// Route to handle admin login
router.post('/login', loginAdmin);

// Protected route that only admins can access
router.get('/protected', authenticateToken, isAdmin, (req, res) => {
  res.status(200).json({ message: 'This is a protected admin route.' });
});

module.exports = router;