How to redirect when user ~is~ authenticated
So there's lots of documentation about protecting routes, and there's also ways within the useSession hook to perform an action if the user isn't authenticated. But is there a way to redirect if they ~are~ authenticated? I'd like to avoid doing something like:
const { data: sessionData, status } = useSession({ required: true })
const router = useRouter()
if (status === 'authenticated') {
void router.push('/quiz/results/success')
}
Because I get a react error about the component not finishing rendering. Is this the only way to do it or can i do something with next on the backend?5 Replies
middleware as well
or get server side props, redirect from there
@nyx (Rustular DevRel) any idea how to do it through middleware? I can only seem to get it to require authentication and not the other way around
the default next auth option just pushes directly from node modules
but in theory
matcher will be the nextjs part
protectedPaths and nonGuestPaths you would handle by code
Hello! i'm curious. If you already did auth in a node/express application and you're calling it from nextjs.
How do you achieve the same effect?
As long as the auth solution is using a
Request
and Response
type, should work regardless of the framework
Most of the time you can get away by checking if the cookie is in place