I have a Next.js project (client side only) where users can sign up via the Universal Login screen, but they’re apparently not being automatically signed in.
After the sign in/sign up flow, users are redirected to the /auth_callback
page, that redirects them to an onboarding flow if they still don’t exist in our database or to a to one of the platform pages if they already are registered.
The problem is that, when signing in, it all works properly; but when users sign up, instead of redirecting them to one of these pages, they are logged out and are required to sign in again, even though they should have been signed in automatically after a successful sign up.
The following code block is a context provider, where the redirect logic is placed:
const AuthProvider = ({ children }: PropsWithChildren) => {
const router = useRouter();
const { isAuthenticated, getAccessTokenSilently } = useAuth0();
useEffect(() => {
if (!isAuthenticated) return;
handleSignIn();
}, [isAuthenticated]);
const handleSignIn = async () => {
const accessToken = await getAccessTokenSilently();
setAuthToken(accessToken);
try {
await getLoggedUser();
if (router.pathname === "/" || router.pathname === "/auth_callback") {
router.push("/payments");
return;
}
} catch (error) {
// The sign up scenario will always end up here
if (error?.response.status === 404) {
router.push("/onboarding/about-you");
return;
}
}
};
return <AuthContext.Provider value={{}}>{children}</AuthContext.Provider>;
};
export default withAuthenticationRequired(AuthProvider);