0
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>
  );
};
  • I don't see where any `useLocation` hook is used. Please edit the post to include the relevant code as a properly formatted and readable code snippet instead of as an image. Images are less accessible, can be more difficult to read, and aren't copy/pasteable. See [mcve]. – Drew Reese Oct 28 '22 at 19:42
  • @DrewReese sir please check now sorry took long time to write code in block I m new to StackOverflow so now code is updated please check once more – Nilesh chauhan Oct 28 '22 at 20:15
  • Where is any `from` state value being passed when issuing a navigation action? I.e. `...` or `navigate("...", { state: { from } })`? Are you attempting to implement route protection? Does this help answer your question? https://stackoverflow.com/a/66289280/8690857 – Drew Reese Oct 28 '22 at 20:21
  • Ok sir got it TY @DrewReese yes i am implementing route protection – Nilesh chauhan Oct 28 '22 at 20:32

0 Answers0