While testing a dynamically created link TO my React app something exploded and now redux is returning the content of the index.html. I have seen this once before but forgot how I fixed it. The current setup has worked for months so I don't want to go crazy changing everything. I got around the html issue for the user login by checking for '<!doctype html>' before dispatching but then subsequent dispatches would succeed then immediately fail and also contain the contents of index.html. I created a new user and the same thing happens. I checked the database, cleared the browser data, checked the application storage in DevTools, and reset my computer. The really strange part is that the version I have on a test server is doing the same thing and it wouldn't load on another device. Here is an example of what I am seeing in React Developer Tools for state:
token:null
isAuthenticated:true
loading:false
user:"<!doctype html><html lang="en"><head><meta charset="utf-8"/> .."
If it's not my computer then how could comparing the two versions break both of them?
EDIT
Load User Action
export const loadUser = () => async dispatch => {
if (localStorage.token) {
setAuthToken(localStorage.token);
}
try {
const res = await axios.get(`${API_PREFIX}/auth`);
dispatch({
type: USER_LOADED,
payload: res.data
});
} catch (error) {
dispatch({
type: AUTH_ERROR,
payload: error.message
});
}}
Backend
router.get('/', auth, async (req, res) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch (err) {
console.error(err.message);
res.send(500).json({msg: 'User error'});
}
});
Reducer
const initialState = {
token: localStorage.getItem('token'),
isAuthenticated: null,
loading: true,
user: null,
online: null,
}
export default function(state = initialState, action) {
const { type, payload } = action;
switch(type){
case USER_LOADED:
return {
...state,
isAuthenticated: true,
loading: false,
user: payload
}
case REGISTER_SUCCESS:
case LOGIN_SUCCESS:
console.log(`\n\n\nLOGIN PAYLOAD: ${JSON.stringify(payload)}`)
localStorage.setItem('token', payload.token);
return {
...state,
...payload,
isAuthenticated: true,
loading: false
}
case REGISTER_FAIL:
case AUTH_ERROR:
case LOGIN_FAIL:
case LOGOUT:
localStorage.removeItem('token');
return {
...state,
token: null,
isAuthenticated: false,
loading: false
}
case ONLINE:
return{
...state,
online: payload
}
default:
return state;
}
}