Skip to content

Bug: getKindeServerSession isAuthenticated triggers a new login and a redirect if token expired #337

@antonioGlavocevic

Description

@antonioGlavocevic

Prerequisites

Describe the issue

Summary

I upgraded my @kinde-oss/kinde-auth-nextjs and noticed on my web app that I was being redirected to the kinde login page when my token expired. Prior to my upgrade, the above scenario would have just resulted in my web app rendering my pages with a "not logged in" state. I have narrowed it down to where I call await isAuthenticated (example further below).

Additionally, this is causing further problems in that I have KINDE_POST_LOGIN_REDIRECT_URL set to go to the users profile. This means when I paste a link somewhere in my app and my token has expired it now redirects to login and then redirects to my profile, rather than where my link specified. I could add some extra logic to track where the user was trying to go and redirect them there instead, but that triggers multiple page loads.

It is quite possible I am miss understanding how isAuthenticated should be used. My understanding is that isAuthenticated should check the auth token and return true if it is valid and false otherwise, which is how it was behaving in 2.4.6. Now it seems to return true when the token is valid, otherwise trigger a login redirect (performing an authentication).

Versions

Previous @kinde-oss/kinde-auth-nextjs version: 2.4.6
Issue occurs on @kinde-oss/kinde-auth-nextjs version: 2.5.0 or higher

I believe it is happening due to

redirectOnExpiredToken(token);

In change batch v2.4.6...v2.5.0#diff-0f5d49c1a149da5ca6fe2bd8f3e6456b90e5eb13dfa48052c3d77cc72407383eR19

Setup

my-app/app/page.tsx

import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

import LoginComponent from "./logincomponent";

export default async function HomePage() {
  const { isAuthenticated, getUser } = getKindeServerSession();
  console.log("checking is authenticated...");
  const check = await isAuthenticated();
  console.log("isAuthenticated: ", check);
  let userID = "Not Logged In";
  if (check) {
    const user = await getUser();
    if (user) {
      userID = user.id;
    }
  }
  return (
    <div>
      <LoginComponent />
      {userID}
    </div>
  );
}

LoginComponent is just a small client component that uses useKindeBrowserClient to render import { LoginLink, LogoutLink, RegisterLink } from "@kinde-oss/kinde-auth-nextjs/components";

Result v2.4.6

If you render the page with an expired token you get the following logs:

 ○ Compiling / ...
 ✓ Compiled / in 1214ms (954 modules)
checking is authenticated...
isAuthenticated:  false
 GET / 200 in 1491ms
 ✓ Compiled /api/auth/[kindeAuth] in 150ms (961 modules)
 GET /api/auth/setup 500 in 995ms

And it will render the Not Logged In.

Result v2.5.0 and higher

If you render the page with an expired token you get the following logs:

 ○ Compiling / ...
 ✓ Compiled / in 1387ms (1326 modules)
checking is authenticated...
 GET / 307 in 1780ms
 ✓ Compiled /api/auth/[kindeAuth] in 170ms (1333 modules)
 ⨯ [Error: No response is returned from route handler '<PATH_TO_APP>/app/api/auth/[kindeAuth]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.]
 ⨯ [Error: No response is returned from route handler '<PATH_TO_APP>/app/api/auth/[kindeAuth]/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.]
 GET /api/auth/login 500 in 734ms
checking is authenticated...
 GET / 307 in 83ms
 GET /api/auth/login 307 in 8ms
 GET /api/auth/kinde_callback?<ARGS> 307 in 129ms
checking is authenticated...
isAuthenticated:  true
 GET / 200 in 24ms
 GET /api/auth/setup 200 in 11ms

And it will render the user id.

Note the number of times "checking is authenticated..." is triggered and that isAuthenticated is never false.

Library URL

https://github.com/kinde-oss/kinde-auth-nextjs

Library version

2.5.0 and higher

Operating system(s)

macOS

Operating system version(s)

15.4.1

Further environment details

No response

Reproducible test case URL

No response

Additional information

I think the function is not doing what is described in the docs: https://docs.kinde.com/developer-tools/sdks/backend/nextjs-sdk/#isauthenticated

I believe this function should simply return true or false regarding the validity of the token. This is what it did in 2.4.6 (https://github.com/kinde-oss/kinde-auth-nextjs/blob/v2.4.6/src/session/isAuthenticated.js)

It seems like now the function performs authentication. Would it be more appropriate for this to be on a different function named something like authenticate?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions