auth stuff
This commit is contained in:
@@ -5,7 +5,7 @@ const authenticateToken = async (req, res, next) => {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
||||
|
||||
if (!token) {
|
||||
if (!token || token === 'undefined' || token === 'null') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: 'Access token required'
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user