Chapter 15: login redirect not working, protected route not protected
I've followed the tutorial as written, but I am unable to get the login to work. Page does not redirect to dashboard upon entering correct credentials. If I enter the incorrect credentials, I do see the 'Invalid credentials' error.
Additionally, I am able to access http://localhost:3000/dashboard without being logged in.
Would someone on the vercel team consider expanding this chapter of the tutorial to include creating new users? This basic functionality would be helpful to have working in the intro tutorial.
Sign out also does not successfully result in a redirect.
The same here
I have the same issue. Any solution?
I was not able to implement a solution using next-auth, so I ended up taking this as an opportunity to learn about Supabase auth. It was fairly straightforward and useful to learn about Supabase, but I am still a believer that there is an opportunity for the Vercel team to further develop auth implementation in this chapter.
Can confirm. Routes are not protected. Log in logs user successfully in - but then redirects to same path /login As others have mentioned as a fix in other threads; adding variations of AUTH_URL=http://localhost:3000 does not solve the issue. Issue is reproducable from : https://github.com/vercel/next-learn/tree/main/dashboard/final-example, as per mentioned here https://github.com/vercel/next-learn/issues/252
Needs attention from Vercel team.
Hey there 👋🏼
Sorry, you run into some issues with auth 😮💨
Would someone on the vercel team consider expanding this chapter of the tutorial to include creating new users? This basic functionality would be helpful to have working in the intro tutorial.
I've written a new guide that walks you through auth best practices, the examples used are for creating users: https://nextjs.org/docs/app/building-your-application/authentication
For now, I'll be removing the auth chapter from course as it doesn't recommend best practices (e.g. auth checks should be done close to your data source, not solely in Middleware), and we'll revisit again it in the future 🙏🏼
Thank you, @delbaoliveira the linked doc is very helpful. Removing the auth chapter from the intro course seems like a good move. The other parts of the course are excellent.
@delbaoliveira auth chapter is still there
@delbaoliveira the auth chapter is still there - it's pretty discouraging to go through all of that effort and it doesn't actually work
I solved the problem. First i found out, that the callback function authorized() in auth.config.ts hasn't been called. An alternative to that function is proxy() in proxy.ts and that function looks like this: ` export async function proxy(req: NextRequest) { const session = await NextAuth(authConfig).auth(); const path = req.nextUrl.pathname const isProtectedRoute = protectedRoutes.includes(path) const isPublicRoute = publicRoutes.includes(path)
console.log('Session:', session);
console.log(`Request URL: ${req.nextUrl}`);
// 4. Redirect to /login if the user is not authenticated
if (isProtectedRoute && !session?.user) {
const redirectUrl = new URL('/login', req.nextUrl);
console.log(`Redirecting to: ${redirectUrl}`);
return NextResponse.redirect(redirectUrl)
}
// 5. Redirect to /dashboard if the user is authenticated
if (
isPublicRoute &&
session?.user &&
!req.nextUrl.pathname.startsWith('/dashboard')
) {
return NextResponse.redirect(new URL('/dashboard', req.nextUrl))
}
return NextResponse.next()
}
` The code is mainly copied from the next guide for authorization and a little mofied. After introducing proxy() i had to update next to the version '^16.0.0'. That's all!