update auth
This commit is contained in:
@@ -7,6 +7,7 @@ const pool = require('../config/database');
|
|||||||
const { validateRequest } = require('../utils/validation');
|
const { validateRequest } = require('../utils/validation');
|
||||||
const { registerSchema, loginSchema, changePasswordSchema } = require('../utils/validation');
|
const { registerSchema, loginSchema, changePasswordSchema } = require('../utils/validation');
|
||||||
const { AppError } = require('../middleware/errorHandler');
|
const { AppError } = require('../middleware/errorHandler');
|
||||||
|
const { authenticateToken } = require('../middleware/auth');
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -231,7 +232,7 @@ router.get('/authentik/callback',
|
|||||||
// @route POST /api/auth/change-password
|
// @route POST /api/auth/change-password
|
||||||
// @desc Change user password
|
// @desc Change user password
|
||||||
// @access Private
|
// @access Private
|
||||||
router.post('/change-password', validateRequest(changePasswordSchema), async (req, res, next) => {
|
router.post('/change-password', authenticateToken, validateRequest(changePasswordSchema), async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { currentPassword, newPassword } = req.body;
|
const { currentPassword, newPassword } = req.body;
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
@@ -305,7 +306,7 @@ router.post('/forgot-password', async (req, res, next) => {
|
|||||||
// @route GET /api/auth/me
|
// @route GET /api/auth/me
|
||||||
// @desc Get current user info
|
// @desc Get current user info
|
||||||
// @access Private
|
// @access Private
|
||||||
router.get('/me', async (req, res, next) => {
|
router.get('/me', authenticateToken, async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const userResult = await pool.query(
|
const userResult = await pool.query(
|
||||||
'SELECT id, email, first_name, last_name, role, created_at FROM users WHERE id = $1',
|
'SELECT id, email, first_name, last_name, role, created_at FROM users WHERE id = $1',
|
||||||
|
|||||||
@@ -35,7 +35,10 @@ apiClient.interceptors.response.use(
|
|||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
// Unauthorized - clear token and redirect to login
|
// Unauthorized - clear token and redirect to login
|
||||||
localStorage.removeItem('authToken');
|
localStorage.removeItem('authToken');
|
||||||
window.location.href = '/login';
|
// Use React Router navigation instead of hard redirect
|
||||||
|
if (window.location.pathname !== '/login' && window.location.pathname !== '/register') {
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
} else if (error.response?.status === 403) {
|
} else if (error.response?.status === 403) {
|
||||||
// Forbidden
|
// Forbidden
|
||||||
toast.error('You do not have permission to perform this action');
|
toast.error('You do not have permission to perform this action');
|
||||||
|
|||||||
Reference in New Issue
Block a user