next-learn icon indicating copy to clipboard operation
next-learn copied to clipboard

Chapter 15: login redirect not working, protected route not protected

Open josephrmartinez opened this issue 2 years ago • 10 comments

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.

Related to: 477 and 252.

josephrmartinez avatar Dec 31 '23 20:12 josephrmartinez

Sign out also does not successfully result in a redirect.

josephrmartinez avatar Dec 31 '23 20:12 josephrmartinez

The same here

terciodejesus avatar Feb 14 '24 20:02 terciodejesus

I have the same issue. Any solution?

EdithOrt avatar Feb 21 '24 01:02 EdithOrt

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.

josephrmartinez avatar Feb 21 '24 01:02 josephrmartinez

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.

jsm-NK avatar Apr 09 '24 11:04 jsm-NK

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 🙏🏼

delbaoliveira avatar May 02 '24 13:05 delbaoliveira

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.

josephrmartinez avatar May 03 '24 16:05 josephrmartinez

@delbaoliveira auth chapter is still there

Lipus-a avatar Dec 17 '24 13:12 Lipus-a

@delbaoliveira the auth chapter is still there - it's pretty discouraging to go through all of that effort and it doesn't actually work

smokinjoe avatar Dec 17 '24 18:12 smokinjoe

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!

tilly63 avatar Feb 21 '26 12:02 tilly63