Fix jwt-token

This commit is contained in:
2025-08-18 05:47:56 +02:00
parent 4c67e49621
commit ea76efb3f1
3 changed files with 24 additions and 9 deletions

View File

@@ -31,7 +31,7 @@ const Layout = () => {
const location = useLocation(); const location = useLocation();
return ( return (
<div className="h-screen flex overflow-hidden bg-gray-100"> <div className="min-h-screen flex bg-gray-100">
{/* Mobile sidebar */} {/* Mobile sidebar */}
<div className={classNames( <div className={classNames(
'fixed inset-0 flex z-40 md:hidden', 'fixed inset-0 flex z-40 md:hidden',
@@ -62,7 +62,7 @@ const Layout = () => {
</div> </div>
{/* Main content */} {/* Main content */}
<div className="flex-1 overflow-hidden"> <div className="flex-1 flex flex-col min-h-screen">
<div className="relative z-10 flex-shrink-0 flex h-16 bg-white border-b border-gray-200"> <div className="relative z-10 flex-shrink-0 flex h-16 bg-white border-b border-gray-200">
<button <button
type="button" type="button"
@@ -126,7 +126,7 @@ const Layout = () => {
</div> </div>
{/* Page content */} {/* Page content */}
<main className="flex-1 relative overflow-y-auto focus:outline-none"> <main className="flex-1">
<div className="py-6"> <div className="py-6">
<div className="max-w-7xl mx-auto px-4 sm:px-6 md:px-8"> <div className="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<Outlet /> <Outlet />

View File

@@ -68,17 +68,28 @@ export const AuthProvider = ({ children }) => {
const checkAuthStatus = async () => { const checkAuthStatus = async () => {
try { try {
dispatch({ type: 'SET_LOADING', payload: true });
const token = localStorage.getItem('token');
if (!token) {
dispatch({ type: 'SET_LOADING', payload: false });
return;
}
const response = await api.get('/users/profile'); const response = await api.get('/users/profile');
dispatch({ dispatch({
type: 'LOGIN_SUCCESS', type: 'LOGIN_SUCCESS',
payload: { payload: {
user: response.data.data, user: response.data.data,
token: localStorage.getItem('token') token: token
} }
}); });
} catch (error) { } catch (error) {
console.log('Token validation failed:', error.response?.status);
localStorage.removeItem('token'); localStorage.removeItem('token');
dispatch({ type: 'LOGOUT' }); dispatch({ type: 'LOGOUT' });
} finally {
dispatch({ type: 'SET_LOADING', payload: false });
} }
}; };

View File

@@ -1,8 +1,9 @@
import axios from 'axios'; import axios from 'axios';
const API_BASE_URL = process.env.NODE_ENV === 'production' const API_BASE_URL = import.meta.env.VITE_API_URL ||
? '/drones/api' (process.env.NODE_ENV === 'production'
: 'http://localhost:3001/api'; ? '/drones/api'
: 'http://localhost:3002/api');
const api = axios.create({ const api = axios.create({
baseURL: API_BASE_URL, baseURL: API_BASE_URL,
@@ -30,9 +31,12 @@ api.interceptors.response.use(
(response) => response, (response) => response,
(error) => { (error) => {
if (error.response?.status === 401) { if (error.response?.status === 401) {
// Token expired or invalid // Token expired or invalid - let AuthContext handle this
localStorage.removeItem('token'); localStorage.removeItem('token');
window.location.href = '/login'; // Only redirect if not already on login page
if (window.location.pathname !== '/login') {
window.location.href = '/login';
}
} }
return Promise.reject(error); return Promise.reject(error);
} }