auth stuff
This commit is contained in:
@@ -5,7 +5,7 @@ const authenticateToken = async (req, res, next) => {
|
|||||||
const authHeader = req.headers['authorization'];
|
const authHeader = req.headers['authorization'];
|
||||||
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
||||||
|
|
||||||
if (!token) {
|
if (!token || token === 'undefined' || token === 'null') {
|
||||||
return res.status(401).json({
|
return res.status(401).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Access token required'
|
message: 'Access token required'
|
||||||
|
|||||||
@@ -3,9 +3,18 @@ import { authAPI } from '../services/api';
|
|||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
// Initial state
|
// Initial state
|
||||||
|
const getValidToken = () => {
|
||||||
|
const token = localStorage.getItem('authToken');
|
||||||
|
if (!token || token === 'undefined' || token === 'null') {
|
||||||
|
localStorage.removeItem('authToken');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
};
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
user: null,
|
user: null,
|
||||||
token: localStorage.getItem('authToken'),
|
token: getValidToken(),
|
||||||
loading: true,
|
loading: true,
|
||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
@@ -95,7 +104,8 @@ export const AuthProvider = ({ children }) => {
|
|||||||
const checkAuth = async () => {
|
const checkAuth = async () => {
|
||||||
const token = localStorage.getItem('authToken');
|
const token = localStorage.getItem('authToken');
|
||||||
|
|
||||||
if (!token) {
|
if (!token || token === 'undefined' || token === 'null') {
|
||||||
|
localStorage.removeItem('authToken');
|
||||||
dispatch({ type: actionTypes.SET_LOADING, payload: false });
|
dispatch({ type: actionTypes.SET_LOADING, payload: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ const apiClient = axios.create({
|
|||||||
apiClient.interceptors.request.use(
|
apiClient.interceptors.request.use(
|
||||||
(config) => {
|
(config) => {
|
||||||
const token = localStorage.getItem('authToken');
|
const token = localStorage.getItem('authToken');
|
||||||
if (token) {
|
console.log('Token from localStorage:', token);
|
||||||
|
if (token && token !== 'undefined' && token !== 'null') {
|
||||||
config.headers.Authorization = `Bearer ${token}`;
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
|
|||||||
Reference in New Issue
Block a user