Next.js Discord

Discord Forum

Nextautth cakkbackUrl does not redirect to signin

Unanswered
bscdev#1145 posted this in #help-forum
Open in Discord
I am experiencing a problem related to nextauth callback url . Issue:- if non aurhenticated user visits a protected route it doesnot redirect to signin page instead it gives server error.

No issue for aithenticated users.

I am using middleware where i define a matcher array to list protected routes as per docs.

I did not mention NEXTAUTH_URL since i am working in localhost. Pls help.

26 Replies

Hello @Belgian Hare , Thanks for your reply. I have attached the code here
Oak rough bulletgall wasp
Have you wrapped your layout with SessionProvider?
No I am fetching session in server side via getServerSession
Like above
I did not convert my layout as client component thats why did not wrap the layout with SessionProvider
I m fetching session from server side
Northern Wheatear
You need to provide NEXTAUTH_URL even in localhost
So do I need to put that NEXAUTH_URL in .env variable or in any other file
Northern Wheatear
In .env itself
For me its just http://localhost:3000
Is that only issue that I have
So i will put that now and let you know if that works
Northern Wheatear
Sure
No it does not work
I have this error when i want to browse profile page , if I remove "/profile" from matcher it works but how can i protect profile page if i dont define the route in matcher
Issue lies in middleware itself I feel
Northern Wheatear
Is this using app router or pages router?
App router
Northern Wheatear
I haven't used middlewares that much to be honest. But if you are using app router you can directly handle in a server side component itself
Yes you are right. That I did. But issue is how to protect the route by middleware
The middleware code I wrote , I believe issue is there
Northern Wheatear
Can you post the code here... As text
Oak rough bulletgall wasp
Try wrapping your children in your layout in a SessionProvider. I personally also used middleware and made a signin page. In my signIn page I check if the status is authenticated, if so I route them to the callback, if not, I route them to SignIn()
You are taking to convert the layout as client component. Actually SessionProvider does not work in server side. I will try that at last. I know that will work. server side also should work.
Hello All, It worked after a hard work. I maintained server side rendering as it is and did not introduce SessionProvider at all. Now auth protection is working for defined pages.
However its not straight forward and not easy to implement and not even metioned in NextAuth docs.
In my application, I want everything to be worked using server rendering ability.

However Thanks to all for your interest.