[GH-ISSUE #1798] unable to access session information in API routes(route.ts) file in Nextjs 15 #26244

Closed
opened 2026-04-17 16:43:50 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @mrinmay7875 on GitHub (Mar 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1798

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I need to access session information for the currenty logged in user in route.ts so that I can extract userId from session and fetch relevant information for the current user from database.

But when I am trying to access session related information using auth.api.getSession then it is returning me NULL values.

When I try to access session information using useSession hook in client side components, then it works fine. But when I try to access session information in route.ts files using auth.api.getSession then it does not work.

I checked the documentation but couldn't find the solution for the same.

// auth.ts

import { betterAuth } from 'better-auth';

export const auth = betterAuth({
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID || '',
      clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
    },
  },
});
// auth-client.ts

import { createAuthClient } from 'better-auth/react';
export const authClient = createAuthClient({
  baseURL: process.env.BETTER_AUTH_URL,
});

export const { signIn, signOut, useSession } = authClient;

Current vs. Expected behavior

I guess this is not a bug and there is definitely a way in better-auth does it already.

What version of Better Auth are you using?

^1.1.14

Provide environment information

- OS: MacOS 15.3.1
- Browser: Chrome v134.0.6998.89

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth';

export const auth = betterAuth({
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID || '',
      clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
    },
  },
});

Additional context

I want to know how can I access session information in API routes in nextjs 15.

Originally created by @mrinmay7875 on GitHub (Mar 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1798 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I need to access session information for the currenty logged in user in route.ts so that I can extract userId from session and fetch relevant information for the current user from database. But when I am trying to access session related information using `auth.api.getSession` then it is returning me `NULL` values. When I try to access session information using `useSession` hook in client side components, then it works fine. But when I try to access session information in `route.ts` files using `auth.api.getSession` then it does not work. I checked the documentation but couldn't find the solution for the same. ``` // auth.ts import { betterAuth } from 'better-auth'; export const auth = betterAuth({ socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID || '', clientSecret: process.env.GOOGLE_CLIENT_SECRET || '', }, }, }); ``` ``` // auth-client.ts import { createAuthClient } from 'better-auth/react'; export const authClient = createAuthClient({ baseURL: process.env.BETTER_AUTH_URL, }); export const { signIn, signOut, useSession } = authClient; ``` ### Current vs. Expected behavior I guess this is not a bug and there is definitely a way in better-auth does it already. ### What version of Better Auth are you using? ^1.1.14 ### Provide environment information ```bash - OS: MacOS 15.3.1 - Browser: Chrome v134.0.6998.89 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; export const auth = betterAuth({ socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID || '', clientSecret: process.env.GOOGLE_CLIENT_SECRET || '', }, }, }); ``` ### Additional context I want to know how can I access session information in API routes in nextjs 15.
GiteaMirror added the lockedbug labels 2026-04-17 16:43:50 -05:00
Author
Owner

@matcastaneda commented on GitHub (Mar 13, 2025):

When you use auth.api.getSession you must pass the next/headers headers, are you passing them to the function?

import { headers } from 'next/headers';

const session = await auth.api.getSession({
  headers: await headers(),
});

You can also make a function to get the session from the server.

'use server';

import { headers } from 'next/headers';
import { auth } from '@/lib/auth';

export async function getSession(): Promise<Session | null> {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  return session;
}

I hope this helps you.

<!-- gh-comment-id:2721207369 --> @matcastaneda commented on GitHub (Mar 13, 2025): When you use `auth.api.getSession` you must pass the next/headers headers, are you passing them to the function? ```ts import { headers } from 'next/headers'; const session = await auth.api.getSession({ headers: await headers(), }); ``` You can also make a function to get the session from the server. ```ts 'use server'; import { headers } from 'next/headers'; import { auth } from '@/lib/auth'; export async function getSession(): Promise<Session | null> { const session = await auth.api.getSession({ headers: await headers(), }); return session; } ``` I hope this helps you.
Author
Owner

@mrinmay7875 commented on GitHub (Mar 13, 2025):

Hey @matcastaneda thanks for the response. I am using passing next/headers in getSession function, but still getting NULL as session information in server components.

Also, I tried the above function snippet that you gave me, this also returns me null as session.

<!-- gh-comment-id:2721271949 --> @mrinmay7875 commented on GitHub (Mar 13, 2025): Hey @matcastaneda thanks for the response. I am using passing next/headers in getSession function, but still getting NULL as session information in server components. Also, I tried the above function snippet that you gave me, this also returns me null as session.
Author
Owner

@JNLei commented on GitHub (Mar 19, 2025):

It took me a while to find the issue but I believe it's because the cookies are missing. In your route.ts, try

const allCookies = request.cookies.getAll();
console.log("allCookies", allCookies);

and you will see allCookies is an empty array.
To make it work, you need to pass cookies in your server component/page where it does the fetch call to the api.

const cookieStore = await cookies();
const test = await fetch("http://localhost:3000/api/test", {
	headers: {
		Cookie: cookieStore.toString(),
	},
});

then in your route.ts, the session should have user's information.

<!-- gh-comment-id:2735148403 --> @JNLei commented on GitHub (Mar 19, 2025): It took me a while to find the issue but I believe it's because the cookies are missing. In your route.ts, try ```typescript const allCookies = request.cookies.getAll(); console.log("allCookies", allCookies); ``` and you will see `allCookies` is an empty array. To make it work, you need to pass cookies in your server component/page where it does the fetch call to the api. ```typescript const cookieStore = await cookies(); const test = await fetch("http://localhost:3000/api/test", { headers: { Cookie: cookieStore.toString(), }, }); ``` then in your route.ts, the `session` should have user's information.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26244