I have a conditional redirect from UserPage as shown below to an external authentication service. The condition is in such a way that if my UserContext does not contain the user object then the page will be redirected to https://urltoauthenticate:5000/user for user authentication. The condition is satisfied in the first run and goes to the authentication service
But the issue is happening after successful authentication. In the case of successful authentication even though the UserContext provides the user object, it will be null in the first render. Here how can I wait till the user object is available and ready so that the page will not be redirected to the authentication service again?
import React, { useEffect, useContext } from 'react';
import { UserContext } from '../context/UserContext';
export default function UserPage () {
const user = useContext(UserContext)
useEffect(() => {
if(user === null{
window.location.assign("https://urltoauthenticate:5000/user");
}
}, [users]);
return(
<div>Hello {user.name}</div>
)
}