I am trying to authenticate user and after it only will take to the feed's page and I m using reqres api for login it was working fine now it is not working error Cannot read properties of null (reading 'from') state.form of useLocation please look into it and help me solve this
Below is code please check once. I am trying to expect by giving
{
"email": "eve.holt@reqres.in",
"password": "cityslicka"
}
user will be redirected to feed page
authContext code I am trying to authenticate user and after it only will take to the feed's page and I m using reqres api for login it was working fine now it is not working error Cannot read properties of null (reading 'from') state.form of useLocation please look into it and help me solve this I am trying to authenticate user and after it only will take to the feed's page and I m using reqres api for login it was working fine now it is not working error Cannot read properties of null (reading 'from') state.form of useLocation please look into it and help me solve this I am trying to authenticate user and after it only will take to the feed's page and I m using reqres api for login it was working fine now it is not working error Cannot read properties of null (reading 'from') state.form of useLocation please look into it and help me solve this
import axios from "axios";
import React, { createContext, useState } from "react";
import { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
export const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [isAuth, setIsAuth] = useState(false);
const navigate = useNavigate();
const { state } = useLocation();
const login = (creds) => {
axios
.post("https://reqres.in/api/login", creds)
.then((d) => {
setIsAuth(true);
localStorage.setItem("token", d.token);
if (state.from) {
navigate(state.from, { replace: true });
} else {
navigate("/");
}
})
.catch((e) => {
console.log(e.message);
setIsAuth(false);
localStorage.removeItem("token");
});
};
const logout = () => {
setIsAuth(false);
navigate("/");
localStorage.setItem("token", "");
};
useEffect(() => {
let token = localStorage.getItem("token");
if (token) {
setIsAuth(token);
}
}, []);
return (
<AuthContext.Provider value={{ isAuth, login, logout }}>
{children}
</AuthContext.Provider>
);
};