Next.js Discord

Discord Forum

SSR with NextAuth.JS not working.

Unanswered
AxenCroft posted this in #help-forum
Open in Discord
Hi.
I'm new to next and not sure what exactly is causing this specific issue where redirection is not working on home screen after authentication with next-auth. I've created a minimal example of this issue with just next-auth as the additional dependency.

I'm attaching a video reproducing the issue. I'm unable to create a Stackblitz for this due to it not supporting the latest NextJs version.
I've attached a few screenshots of the code and my project structure.

I will add more info as needed.

I'm using next-auth@beta.

13 Replies

Atlantic menhaden
You have not created an API route for next-auth.
Something like this: app/auth/[...nextauth]/route.ts
with the code in the image below .
Atlantic menhaden
The logout button should also be in the client component
Alright, let me try that. Thanks!
Oh. Just tried this. I do not have a module named "auth" and there is no "handlers" being exported from "next-auth". I have seen this API route in an example earlier but it was being used with the pages directory and not with the App router.
@Atlantic menhaden The logout button should also be in the client component
I cannot seem to have the logout button in a client component because it has 'use server' for the signOut() function.
Tried various ways to use this signOut function in onClick of the button and even in the form action. Still no difference.
Mentioned next-auth@beta in the question.
Atlantic menhaden
You also need to use it with the app router and import the sessionProvider into the layout.
The auth.ts should look something like this:
Make a server action for logout:
And import to the component:
Okay ... I did not think of that. Let me try this