LoginLink not working in NextJS
I have been sitting with this issue for a while now in NextJS.
After following the docs to setup my application for NextJS and getting to the point where I added a
<LoginLink>Sign in</LoginLink>
and <RegisterLink>Sign up</RegisterLink>
Clicking on the Signup button redirects me to kinde's website where the user can register.
However, Clicking on the LoginLink takes me to my own site and does nothing.
The same thing happens when I use the route /api/auth/login
and /api/auth/register
Any help will be appreciated5 Replies
Hello, JackNytely.
Your description suggests that the <LoginLink> and <RegisterLink> components are rendering, but only the register flow is redirecting correctly, while login is not, and going directly to /api/auth/login also does nothing.
Here are the key things to check based on the official Kinde Next.js SDK documentation:
1. API Route Handler Exists
Make sure you have the required API route handler set up. For the Next.js Pages Router SDK, you must create /pages/api/auth/[...kindeAuth].js with the following code:
This file is required for the Kinde auth endpoints to work. Without it, the /api/auth/login and /api/auth/register routes will not function (https://docs.kinde.com/developer-tools/sdks/backend/nextjs-prev-sdk/#authentication).
2. Environment Variables
Double-check your .env.local file contains all the necessary Kinde variables, and that their values match what you have in your Kinde application settings:
If these are missing or incorrect, authentication won't work as expected (https://docs.kinde.com/developer-tools/sdks/backend/nextjs-prev-sdk/#authentication).
3. No Conflicting Routes
Ensure you do not have your own custom
/login
or /register
routes/pages that could intercept or conflict with Kinde’s /api/auth/login
and /api/auth/register
. If you do, remove or rename them, as this is a common cause of the behavior you described (https://github.com/kinde-oss/kinde-auth-nextjs/issues/86).
4. Correct Link Usage
For Next.js 13+ with the Pages Router, use the provided <LoginLink>
and <RegisterLink>
components. If you are using standard <a href="/api/auth/login">
or <a href="/api/auth/register">
, that’s also supported, but do not wrap these links with the Next.js <Link>
component, as prefetching can cause state errors (https://docs.kinde.com/developer-tools/sdks/backend/nextjs-prev-sdk/#authentication).
5. Dev Server Restart
After updating any routes or environment variables, restart your Next.js development server to ensure all changes are picked up.
If you follow all of these steps and the issue persists, please confirm:
- The framework version and SDK version you are using.
- That you do not see any errors in your server logs or browser console.
- That visiting /api/auth/login
directly in your browser triggers a redirect to Kinde’s login page.
Let me know if this helps or if you need more help troubleshooting.Kinde docs
Next.js Pages Router SDK
Legacy guide for Next.js Pages Router SDK including authentication setup, route handlers, and migration from previous versions.
I love you, I did have everything setup correctly, and after deleting my existing /api/login and /login pages it still did not work, however, I went to the middleware.ts file to check if I maybe have residuals there and found I have set the Login Page property to /login
Removing that fixed the issue:
Hello again, I'm glad to hear that worked! Thank you for taking the time to report back with your solution; it's incredibly helpful for us and will allow us to assist others with similar issues more quickly, many actually don't report back when they find the solution, so thanks again
No problem, Thanks again, and I am glad I could help in someway aswell ❤️