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

View File

@@ -68,17 +68,28 @@ export const AuthProvider = ({ children }) => {
const checkAuthStatus = async () => {
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');
dispatch({
type: 'LOGIN_SUCCESS',
payload: {
user: response.data.data,
token: localStorage.getItem('token')
token: token
}
});
} catch (error) {
console.log('Token validation failed:', error.response?.status);
localStorage.removeItem('token');
dispatch({ type: 'LOGOUT' });
} finally {
dispatch({ type: 'SET_LOADING', payload: false });
}
};

View File

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