Next.js Discord

Discord Forum

How do i redirect users to login page after signing out(supabase)?

Answered
Broken Nokia posted this in #help-forum
Open in Discord
Using
redirect("/login");
is giving me a runtime error as shown
Answered by B33fb0n3
As far as I know, you can't use the redirect function in client components. But you can still redirect using the clientside router via useRouter()-Hook. Then just do router.push("/login") and you are good to go.
View full answer

11 Replies

note: this is a client component
As far as I know, you can't use the redirect function in client components. But you can still redirect using the clientside router via useRouter()-Hook. Then just do router.push("/login") and you are good to go.
Answer
@Broken Nokia Using redirect("/login"); is giving me a runtime error as shown
Turkish Van
redirect function is only usable on Server Components, Server Actions and Route Handlers.

What You can do is to wrap that button inside of a form and attach signOut action to it. Following that, You might keep it a Server Component and also You would be able to use the redirect function.

On the other hand, if You want to keep it simple and stay on the Client side, You could just use useRouter Reack hook.
Serverside is nearly the same way:
if you are on a protected route, you can just refresh the browser using router.refresh and then the middleware should be executed if I am right and that will redirect the user
@B33fb0n3 As far as I know, you can't use the redirect function in client components. But you can still redirect using the clientside router via useRouter()-Hook. Then just do router.push("/login") and you are good to go.
i get this issue, the docs tells to migrate to new hooks imported from next/navigation


not sure what this means
yea, import it from next/navigation instead of next/router
that was a very silly mistake on my part, i didn't realise since it auto imported from /router
sometimes that happens the best 🙂
thank you very much @B33fb0n3 @Turkish Van 🫶
sure thing