In my React JS project, I have a RequireAuth.js which is used to check if the user is authorized every time they change pages.
As an example, inside the const "confirmToken", I'm calling an async function that returns true
if the user's token is valid or false
.
const confirmToken = async () => {
return await check();
};
var v = confirmToken().then((result) => {
return result;
});
It's working. But the variable v
is returning as Promise
.
Promise {[[PromiseState]]: 'pending', [[PromiseResult]]: undefined}
I've checked other similar questions on Stack, but I still don't know what I'm getting wrong here.
Is there no way to return the boolean in a new variable?
My code is like this. The allowedRoles
is a list of user levels that are passed in the route with RequireAuth.
const RequireAuth = ({ allowedRoles }) => {
const location = useLocation();
try {
var t = UseToken();
var l = UseLevel();
} catch (error) {
return <Navigate to="/" state={{ from: location }} replace />;
}
const confirmToken = async () => {
return await checkToken(t);
};
var v = confirmToken().then((result) => {
return result;
});
return v & (l in allowedRoles) ? (
<Outlet />
) : v ? (
<Navigate to="/noAuthorized" state={{ from: location }} replace />
) : (
<Navigate to="/" state={{ from: location }} replace />
);
};
Obs: I am probably doing something really wrong here.
I need it to return a boolean so that the ternary works in the return()
.