Skip to content

Bug: using getKindeServerSession doesn't trigger ssr on demand #261

@yyaskriloff

Description

@yyaskriloff

Prerequisites

Describe the issue

"@kinde-oss/kinde-auth-nextjs": "^2.4.6",
"next": "15.1.6",

In NextJS v15 the way a route is determined to be static or not is by awaiting either a header or params... (I originally thought it was just if the function was async) I ran into an issue where I had a page component getting kinde user and this was moved to another file because i'm using the kinde id as the id in my db except for the kp_ part of the string

// src/lib/kinde.ts
import 'server-only'

import { getKindeServerSession } from '@kinde-oss/kinde-auth-nextjs/server'
import { redirect } from 'next/navigation'

const { getUser } = getKindeServerSession()
export const getKindeUser = async () =>
  getUser().then(user => (user ? { ...user, id: user.id.replace('kp_', '') } : redirect('/api/auth/login')))

When deployed (not in dev mode) I would get the error to many redirects. the issue I found was that it was redirecting me to sign in from the function getKindeUser (as opposed to the middleware). This was because during the build I believe this expression was evaluated and user was null thus returning the redirect. when I looked at the build logs it said the pages using this function were static the only ones that were not static that were using this function were dynamic routes. I believe a fix can be to make getKindeServerSession an async function and just call await headers() to force it not to be evaluated at build.

Library URL

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

Library version

2.4.6

Operating system(s)

macOS

Operating system version(s)

macOS sequoia Version 15.1.1

Further environment details

No response

Reproducible test case URL

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions