No cookies being stored? #108

Closed
opened 2026-03-13 07:33:50 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @yashburshe on GitHub (Oct 20, 2024).

Cookies aren't being stored in the browser and session is always null even though I get POST /api/auth/sign-in/email 200 in 563ms after clicking on sign in.

Sign up works correctly and adds a user to the database.
Sessions are being added to the neon database through drizzle correctly, however, account table has accessToken, refreshToken and idToken as NULL. Is this expected?

I'm using safari/macos 18 and email/password to sign in with Next.js (latest).

/app/lib/auth.ts

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../db/drizzle";
import { user, session, account, verification } from "../db/schema";

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: "pg",
    schema: {
      user,
      session,
      account,
      verification,
    },
  }),
  emailAndPassword: {
    enabled: true,
  }
});

/app/lib/client.ts

import { createAuthClient } from "better-auth/react";
import { config } from "dotenv";

config({path: ".env"})

export const {signIn, signOut, signUp, useSession} = createAuthClient({
    baseURL: process.env.BETTER_AUTH_URL!
})

/app/(auth)/login/page.tsx

import { signIn } from "@/app/lib/client";

const userLogin = async (formData: FormData) => {
  "use server";
  const email = formData.get("email") as string;
  const password = formData.get("password") as string;
  signIn.email({
    email,
    password,
  });
};

export default function Login() {
  return (
    <div>
      <h1>Log in</h1>
      <form action={userLogin}>
        <label>
          <label>Username</label>
          <input name="email" type="email" required placeholder="Email"></input>
          <input
            name="password"
            type="password"
            required
            placeholder="Password"
          ></input>
        </label>
        <button>Sign in</button>
      </form>
    </div>
  );
}
Originally created by @yashburshe on GitHub (Oct 20, 2024). Cookies aren't being stored in the browser and session is always null even though I get `POST /api/auth/sign-in/email 200 in 563ms` after clicking on sign in. Sign up works correctly and adds a user to the database. Sessions are being added to the neon database through drizzle correctly, however, `account` table has `accessToken`, `refreshToken` and `idToken` as NULL. Is this expected? I'm using safari/macos 18 and email/password to sign in with Next.js (latest). /app/lib/auth.ts ``` import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { db } from "../db/drizzle"; import { user, session, account, verification } from "../db/schema"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", schema: { user, session, account, verification, }, }), emailAndPassword: { enabled: true, } }); ``` /app/lib/client.ts ``` import { createAuthClient } from "better-auth/react"; import { config } from "dotenv"; config({path: ".env"}) export const {signIn, signOut, signUp, useSession} = createAuthClient({ baseURL: process.env.BETTER_AUTH_URL! }) ``` /app/(auth)/login/page.tsx ``` import { signIn } from "@/app/lib/client"; const userLogin = async (formData: FormData) => { "use server"; const email = formData.get("email") as string; const password = formData.get("password") as string; signIn.email({ email, password, }); }; export default function Login() { return ( <div> <h1>Log in</h1> <form action={userLogin}> <label> <label>Username</label> <input name="email" type="email" required placeholder="Email"></input> <input name="password" type="password" required placeholder="Password" ></input> </label> <button>Sign in</button> </form> </div> ); } ```
Author
Owner

@Bekacru commented on GitHub (Oct 20, 2024):

You're trying to call the client from within a server action, which can't set cookies. To fix this, make sure to call it in a client component instead. If you absolutely need to keep it in a server action, use the server auth.api instance and manually handle the cookies with Next's cookie helper. But that's an unnecessary workaround. My suggestion is to move the call to a client component and it should work.

@Bekacru commented on GitHub (Oct 20, 2024): You're trying to call the client from within a server action, which can't set cookies. To fix this, make sure to call it in a client component instead. If you absolutely need to keep it in a server action, use the server `auth.api` instance and manually handle the cookies with Next's `cookie` helper. But that's an unnecessary workaround. My suggestion is to move the call to a client component and it should work.
Author
Owner

@yashburshe commented on GitHub (Oct 20, 2024):

That fixed the issue, thank you for bearing with me.

@yashburshe commented on GitHub (Oct 20, 2024): That fixed the issue, thank you for bearing with me.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#108