auth fixes
This commit is contained in:
@@ -12,6 +12,9 @@ const authenticateToken = async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log token for debugging (remove in production)
|
||||||
|
console.log('Token received:', token.substring(0, 20) + '...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
"typescript": "^4.9.5"
|
"typescript": "^4.9.5"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "DANGEROUSLY_DISABLE_HOST_CHECK=true react-scripts start",
|
"start": "DANGEROUSLY_DISABLE_HOST_CHECK=true WDS_SOCKET_PORT=0 react-scripts start",
|
||||||
"build": "react-scripts build",
|
"build": "react-scripts build",
|
||||||
"test": "react-scripts test",
|
"test": "react-scripts test",
|
||||||
"eject": "react-scripts eject"
|
"eject": "react-scripts eject"
|
||||||
|
|||||||
@@ -32,16 +32,14 @@ apiClient.interceptors.response.use(
|
|||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
// Handle specific error codes
|
// Handle specific error codes
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401 || error.response?.status === 403) {
|
||||||
// Unauthorized - clear token and redirect to login
|
// Unauthorized or malformed token - clear token and redirect to login
|
||||||
|
console.log('Clearing invalid token due to auth error:', error.response?.status);
|
||||||
localStorage.removeItem('authToken');
|
localStorage.removeItem('authToken');
|
||||||
// Use React Router navigation instead of hard redirect
|
// Use React Router navigation instead of hard redirect
|
||||||
if (window.location.pathname !== '/login' && window.location.pathname !== '/register') {
|
if (window.location.pathname !== '/login' && window.location.pathname !== '/register') {
|
||||||
window.location.href = '/login';
|
window.location.href = '/login';
|
||||||
}
|
}
|
||||||
} else if (error.response?.status === 403) {
|
|
||||||
// Forbidden
|
|
||||||
toast.error('You do not have permission to perform this action');
|
|
||||||
} else if (error.response?.status >= 500) {
|
} else if (error.response?.status >= 500) {
|
||||||
// Server error
|
// Server error
|
||||||
toast.error('Server error. Please try again later.');
|
toast.error('Server error. Please try again later.');
|
||||||
|
|||||||
Reference in New Issue
Block a user