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
Neto
Neto2y ago
middleware as well or get server side props, redirect from there
Dylz
DylzOP2y ago
@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
Neto
Neto2y ago
the default next auth option just pushes directly from node modules but in theory
matcher // all paths that that middleware should cache
protectedPaths // paths to check if is authenticated or send to auth page
nonGuestPaths // paths to check if is authenticated or send to other page
matcher // all paths that that middleware should cache
protectedPaths // paths to check if is authenticated or send to auth page
nonGuestPaths // paths to check if is authenticated or send to other page
matcher will be the nextjs part protectedPaths and nonGuestPaths you would handle by code
Complexlity
Complexlity2y ago
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?
Neto
Neto2y ago
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
Want results from more Discord servers?
Add your server