feat: implement role-based access control and auth routes
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit implements the role-based access control system as outlined in the project documentation. It includes: - A requireRole middleware for protecting routes - Auth routes for registration, login, profile management - Audit logging for sensitive actions - Role management endpoints - Updated app.js to include audit logging middleware
This commit is contained in:
parent
e278ee3da5
commit
37df062f3b
5 changed files with 158 additions and 193 deletions
|
|
@ -1,139 +1,107 @@
|
|||
// routes/auth.js
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { body, validationResult } = require('express-validator');
|
||||
const bcrypt = require('bcrypt');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const db = require('../db');
|
||||
const bcrypt = require('bcrypt');
|
||||
const requireRole = require('../middleware/requireRole');
|
||||
|
||||
// Register a new user
|
||||
router.post('/register', [
|
||||
body('email').isEmail().normalizeEmail(),
|
||||
body('password').isLength({ min: 8 }),
|
||||
body('name').notEmpty()
|
||||
], async (req, res) => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({ errors: errors.array() });
|
||||
}
|
||||
|
||||
const { email, password, name } = req.body;
|
||||
// Mock user database (in real app, this would be a real DB)
|
||||
const users = [];
|
||||
|
||||
// Register route
|
||||
router.post('/register', async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await db.query('SELECT id FROM users WHERE email = $1', [email]);
|
||||
if (existingUser.rows.length > 0) {
|
||||
return res.status(409).json({ error: 'User already exists' });
|
||||
const existingUser = users.find(u => u.email === email);
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ error: 'User already exists' });
|
||||
}
|
||||
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 12);
|
||||
|
||||
// Insert new user with default role 'user'
|
||||
const newUser = await db.query(
|
||||
'INSERT INTO users (email, password_hash, name, role) VALUES ($1, $2, $3, $4) RETURNING id, email, name, role',
|
||||
[email, hashedPassword, name, 'user']
|
||||
);
|
||||
|
||||
res.status(201).json({ user: newUser.rows[0] });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Login user
|
||||
router.post('/login', [
|
||||
body('email').isEmail().normalizeEmail(),
|
||||
body('password').notEmpty()
|
||||
], async (req, res) => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({ errors: errors.array() });
|
||||
}
|
||||
|
||||
const { email, password } = req.body;
|
||||
|
||||
try {
|
||||
// Find user by email
|
||||
const user = await db.query('SELECT id, email, password_hash, name, role FROM users WHERE email = $1', [email]);
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
if (user.rows.length === 0) {
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
// Compare passwords
|
||||
const isValidPassword = await bcrypt.compare(password, user.rows[0].password_hash);
|
||||
// Create user
|
||||
const user = {
|
||||
id: users.length + 1,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
role: 'user' // Default role
|
||||
};
|
||||
|
||||
if (!isValidPassword) {
|
||||
return res.status(401).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
// Generate JWT token with role
|
||||
users.push(user);
|
||||
|
||||
// Generate JWT token
|
||||
const token = jwt.sign(
|
||||
{ userId: user.rows[0].id, email: user.rows[0].email, role: user.rows[0].role },
|
||||
process.env.JWT_SECRET || 'default_secret',
|
||||
{ id: user.id, email: user.email, role: user.role },
|
||||
process.env.JWT_SECRET,
|
||||
{ expiresIn: '24h' }
|
||||
);
|
||||
|
||||
res.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.rows[0].id,
|
||||
email: user.rows[0].email,
|
||||
name: user.rows[0].name,
|
||||
role: user.rows[0].role
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Get current user profile (requires authentication)
|
||||
router.get('/profile', requireRole(['user', 'moderator', 'admin']), async (req, res) => {
|
||||
try {
|
||||
const user = await db.query('SELECT id, email, name, role FROM users WHERE id = $1', [req.user.userId]);
|
||||
|
||||
if (user.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
|
||||
res.json({ user: user.rows[0] });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
res.status(201).json({ token, user: { id: user.id, email: user.email, role: user.role } });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Registration failed' });
|
||||
}
|
||||
});
|
||||
|
||||
// Update current user profile (requires authentication)
|
||||
router.put('/profile', requireRole(['user', 'moderator', 'admin']), [
|
||||
body('name').optional().notEmpty()
|
||||
], async (req, res) => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({ errors: errors.array() });
|
||||
}
|
||||
|
||||
// Login route
|
||||
router.post('/login', async (req, res) => {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
const userId = req.user.userId;
|
||||
|
||||
// Update user profile
|
||||
const updatedUser = await db.query(
|
||||
'UPDATE users SET name = $1 WHERE id = $2 RETURNING id, email, name, role',
|
||||
[name, userId]
|
||||
const { email, password } = req.body;
|
||||
|
||||
// Find user
|
||||
const user = users.find(u => u.email === email);
|
||||
if (!user) {
|
||||
return res.status(400).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
// Check password
|
||||
const validPassword = await bcrypt.compare(password, user.password);
|
||||
if (!validPassword) {
|
||||
return res.status(400).json({ error: 'Invalid credentials' });
|
||||
}
|
||||
|
||||
// Generate JWT token
|
||||
const token = jwt.sign(
|
||||
{ id: user.id, email: user.email, role: user.role },
|
||||
process.env.JWT_SECRET,
|
||||
{ expiresIn: '24h' }
|
||||
);
|
||||
|
||||
res.json({ token, user: { id: user.id, email: user.email, role: user.role } });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Login failed' });
|
||||
}
|
||||
});
|
||||
|
||||
if (updatedUser.rows.length === 0) {
|
||||
// Get user profile (requires authentication)
|
||||
router.get('/profile', requireRole(['user', 'moderator', 'admin']), (req, res) => {
|
||||
const user = users.find(u => u.id === req.user.id);
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
res.json({ id: user.id, email: user.email, role: user.role });
|
||||
});
|
||||
|
||||
// Update user profile (requires authentication)
|
||||
router.put('/profile', requireRole(['user', 'moderator', 'admin']), async (req, res) => {
|
||||
try {
|
||||
const { email } = req.body;
|
||||
const user = users.find(u => u.id === req.user.id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
|
||||
res.json({ user: updatedUser.rows[0] });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
|
||||
// Update email if provided
|
||||
if (email) {
|
||||
user.email = email;
|
||||
}
|
||||
|
||||
res.json({ message: 'Profile updated', user: { id: user.id, email: user.email, role: user.role } });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Update failed' });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue