auth stuff

This commit is contained in:
Jake Kasper
2025-08-21 12:57:23 -05:00
parent da43e32ade
commit be4951153c
3 changed files with 15 additions and 4 deletions

View File

@@ -3,9 +3,18 @@ import { authAPI } from '../services/api';
import toast from 'react-hot-toast';
// Initial state
const getValidToken = () => {
const token = localStorage.getItem('authToken');
if (!token || token === 'undefined' || token === 'null') {
localStorage.removeItem('authToken');
return null;
}
return token;
};
const initialState = {
user: null,
token: localStorage.getItem('authToken'),
token: getValidToken(),
loading: true,
error: null,
};
@@ -95,7 +104,8 @@ export const AuthProvider = ({ children }) => {
const checkAuth = async () => {
const token = localStorage.getItem('authToken');
if (!token) {
if (!token || token === 'undefined' || token === 'null') {
localStorage.removeItem('authToken');
dispatch({ type: actionTypes.SET_LOADING, payload: false });
return;
}

View File

@@ -17,7 +17,8 @@ const apiClient = axios.create({
apiClient.interceptors.request.use(
(config) => {
const token = localStorage.getItem('authToken');
if (token) {
console.log('Token from localStorage:', token);
if (token && token !== 'undefined' && token !== 'null') {
config.headers.Authorization = `Bearer ${token}`;
}
return config;