sorry if this is something extremely simple that I am overlooking, but I am trying to make a component that basically restricts certain react-router routes only to users that have an active token.
import Axios from "axios";
import React, {useState, useEffect, useSelector} from 'react';
import {Route, Redirect} from 'react-router-dom';
function isLogin(){
const result = Axios.get('http://localhost:8081/authentication', {withCredentials: true})
.then(res => {
console.log(res);
})
console.log(result);
}
const PrivateRoute = ({component: Component, ...rest}) => {
return (
<Route {...rest} render={props => (
isLogin() ?
<Component {...props} />
: <Redirect to="/login" />
)} />
);
};
export default PrivateRoute;
It seems like (as expected) only the "console.log(result)" gets executed with the pending promise, but in an end result, I am trying to code some logic into the response given from my backend (true or false), which then should be sent down to the PrivateRoute component to determine if the user should be sent back to login, etc..
I understand this has to be because of the async nature of axios.get which is causing my problems, but I have tried several different methods:
Making the isLogin function async, then await the axios request
Create another function inside the PrivateRoute return that is async, with await.
It seems like whatever I try here is not properly waiting the result from axios.get, and thus for giving unwanted results.. Would appreciate any advice, even the right direction to head in.
Thank you.