Logs out a user when access token expires

PHOTO EMBED

Fri Nov 19 2021 12:42:00 GMT+0000 (UTC)

Saved by @blackboi #react.js

const [ user, setUser ] = useState(JSON.parse(localStorage.getItem('profile'))); //convert to object
 
const logout = () =>{
        dispatch({type: 'LOGOUT'});
        history.push("/");
        setUser(null);
    }
 
    useEffect(()=>{
        const token = user?.token;
 
        //JWT check if token expired
        if(token){
            const decodedToken = decode(token)
            if(decodedToken.exp*1000 < newDate().getTime()) logout();
        }
        setUser(JSON.parse(localStorage.getItem('profile')))
    },[location])
content_copyCOPY