[GH-ISSUE #730] Sorry, this is probably not a bug but my error #25721

Closed
opened 2026-04-17 15:59:21 -05:00 by GiteaMirror · 11 comments
Owner

Originally created by @ghost on GitHub (Dec 2, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/730

Hi, I can log in and get a session returned. And I can see the session in my db.

try {
   const { error, data } = await authClient.signIn.emailOtp({
     email: validatedFormData.data.email,
     otp: validatedFormData.data.otp,
   });
   if (error) {
     return {
       success: false,
       message: 'Authentication failed! Please try again.',
     };
   }

   console.log('DATA: ', data);
   console.log('SESSION: ', data?.session);
   console.log('USER: ', data?.user);

But then I can't access the session and I don't see a cookie in my dev tools.

import { authClient } from '@/lib/auth-client';

export default async function ProfilePage() {
  const { data } = await authClient.getSession();

  console.log(data);
  console.log(data?.user);
  console.log(data?.session);
}
Originally created by @ghost on GitHub (Dec 2, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/730 Hi, I can log in and get a session returned. And I can see the session in my db. ``` try { const { error, data } = await authClient.signIn.emailOtp({ email: validatedFormData.data.email, otp: validatedFormData.data.otp, }); if (error) { return { success: false, message: 'Authentication failed! Please try again.', }; } console.log('DATA: ', data); console.log('SESSION: ', data?.session); console.log('USER: ', data?.user); ``` But then I can't access the session and I don't see a cookie in my dev tools. ``` import { authClient } from '@/lib/auth-client'; export default async function ProfilePage() { const { data } = await authClient.getSession(); console.log(data); console.log(data?.user); console.log(data?.session); } ```
GiteaMirror added the locked label 2026-04-17 15:59:21 -05:00
Author
Owner

@Bekacru commented on GitHub (Dec 2, 2024):

import { authClient } from '@/lib/auth-client';
import { headers } from "next/headers";

export default async function ProfilePage() {
  const { data } = await authClient.getSession({
    headers: await headers(),
  });
}

When you call authClient.getSession on the server, you need to pass the headers as a fetchOption. This will make an HTTP round trip from your server to your own server. Alternatively, you can use auth.api.getSession to avoid that. However, using getSession this way has the benefit of updating the cookie if the session has reached its update age.

<!-- gh-comment-id:2512191918 --> @Bekacru commented on GitHub (Dec 2, 2024): ```tsx import { authClient } from '@/lib/auth-client'; import { headers } from "next/headers"; export default async function ProfilePage() { const { data } = await authClient.getSession({ headers: await headers(), }); } ``` When you call `authClient.getSession` on the server, you need to pass the headers as a `fetchOption`. This will make an HTTP round trip from your server to your own server. Alternatively, you can use `auth.api.getSession` to avoid that. However, using `getSession` this way has the benefit of updating the cookie if the session has reached its update age.
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

Thank you so much for this!!

<!-- gh-comment-id:2512224718 --> @ghost commented on GitHub (Dec 2, 2024): Thank you so much for this!!
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

Do i need to set the headers somewhere?
I'm still getting null for the session with this.

const { data } = await authClient.getSession({
    fetchOptions: {
      headers: await headers(),
    },
  });
<!-- gh-comment-id:2512239268 --> @ghost commented on GitHub (Dec 2, 2024): Do i need to set the headers somewhere? I'm still getting null for the session with this. ``` const { data } = await authClient.getSession({ fetchOptions: { headers: await headers(), }, }); ```
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

I also don’t see any cookie in my chrome dev tools

<!-- gh-comment-id:2512249419 --> @ghost commented on GitHub (Dec 2, 2024): I also don’t see any cookie in my chrome dev tools
Author
Owner

@hossdar commented on GitHub (Dec 2, 2024):

I am at the same spot signIn.emailOtp successfully created the session in the database, but nothing in the client! session cookies not shown my dev tools !

Help please, and thank you guys for the amazing work 👍

<!-- gh-comment-id:2512269683 --> @hossdar commented on GitHub (Dec 2, 2024): I am at the same spot ```signIn.emailOtp``` successfully created the session in the database, but nothing in the client! session cookies not shown my dev tools ! Help please, and thank you guys for the amazing work 👍
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

Agreed. Love this project.

<!-- gh-comment-id:2512273347 --> @ghost commented on GitHub (Dec 2, 2024): Agreed. Love this project.
Author
Owner

@Bekacru commented on GitHub (Dec 2, 2024):

If you're trying to call authClient.signIn.emailOTP on the server, don't. authClient is meant to be called on the client, if it's not called on the client it can't set the cookie. If you need to use server actions you can use auth.api.signInEmailOTP instead and make sure to add nextCookies in your plugins list.

<!-- gh-comment-id:2512277933 --> @Bekacru commented on GitHub (Dec 2, 2024): If you're trying to call `authClient.signIn.emailOTP` on the server, don't. `authClient` is meant to be called on the client, if it's not called on the client it can't set the cookie. If you need to use server actions you can use `auth.api.signInEmailOTP` instead and make sure to add `nextCookies` in your plugins list.
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

do you mean this plugin:
import { bearer } from "better-auth/plugins";

<!-- gh-comment-id:2512316504 --> @ghost commented on GitHub (Dec 2, 2024): do you mean this plugin: import { bearer } from "better-auth/plugins";
Author
Owner

@Bekacru commented on GitHub (Dec 2, 2024):

no read the docs here https://www.better-auth.com/docs/integrations/next#server-action-cookies

<!-- gh-comment-id:2512320198 --> @Bekacru commented on GitHub (Dec 2, 2024): no read the docs here https://www.better-auth.com/docs/integrations/next#server-action-cookies
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

Thank you, appreciate this help.

<!-- gh-comment-id:2512322290 --> @ghost commented on GitHub (Dec 2, 2024): Thank you, appreciate this help.
Author
Owner

@ghost commented on GitHub (Dec 2, 2024):

Thanks again. It's working now.

<!-- gh-comment-id:2512333744 --> @ghost commented on GitHub (Dec 2, 2024): Thanks again. It's working now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25721