[GH-ISSUE #1001] How to use headers with fastify #8543

Closed
opened 2026-04-13 03:38:59 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @arthurgrigoletto on GitHub (Dec 23, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1001

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a backend with fastify
  2. Initiated Auth instance with drizzle ORM and organization plugin
  3. Use create organization API method

Current vs. Expected behavior

I would like to create an organization, get a session, but I am continually getting UNAUTHORIZED

What version of Better Auth are you using?

1.1.1

Provide environment information

- OS: Macbook Pro
- Framework: Fastify

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

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { organization } from 'better-auth/plugins'

import { db } from '@/db/connection'
import * as schema from '@/db/schema'
import { env } from '@/env'

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: 'pg',
    schema,
  }),
  plugins: [organization()],
})

Additional context

When I call createOrganization, or getSession or any api methods that needs the headers or some token the API returns UNAUTHORIZED

const organization = await auth.api.createOrganization({
        body: {
          name,
          slug: createSlug(name),
        },
        headers: new Headers(request.raw.headers as HeadersInit),
      })

LOG:

[APIError [BetterCallAPIError]: API Error: UNAUTHORIZED ] {
  status: 'UNAUTHORIZED',
  headers: Headers {
    'Set-Cookie': 'better-auth.session_token=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax, better-auth.session_data=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax, better-auth.dont_remember=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax'
  },
  body: { code: 'UNAUTHORIZED' },
  [cause]: undefined
}
Originally created by @arthurgrigoletto on GitHub (Dec 23, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1001 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. Create a backend with fastify 2. Initiated Auth instance with drizzle ORM and organization plugin 3. Use create organization API method ### Current vs. Expected behavior I would like to create an organization, get a session, but I am continually getting UNAUTHORIZED ### What version of Better Auth are you using? 1.1.1 ### Provide environment information ```bash - OS: Macbook Pro - Framework: Fastify ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth' import { drizzleAdapter } from 'better-auth/adapters/drizzle' import { organization } from 'better-auth/plugins' import { db } from '@/db/connection' import * as schema from '@/db/schema' import { env } from '@/env' export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg', schema, }), plugins: [organization()], }) ``` ### Additional context When I call createOrganization, or getSession or any api methods that needs the headers or some token the API returns UNAUTHORIZED ``` const organization = await auth.api.createOrganization({ body: { name, slug: createSlug(name), }, headers: new Headers(request.raw.headers as HeadersInit), }) ``` LOG: ``` [APIError [BetterCallAPIError]: API Error: UNAUTHORIZED ] { status: 'UNAUTHORIZED', headers: Headers { 'Set-Cookie': 'better-auth.session_token=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax, better-auth.session_data=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax, better-auth.dont_remember=; Max-Age=0; Path=/; HttpOnly; SameSite=Lax' }, body: { code: 'UNAUTHORIZED' }, [cause]: undefined } ```
GiteaMirror added the stalelockedbug labels 2026-04-13 03:38:59 -05:00
Author
Owner

@brunowego commented on GitHub (Feb 7, 2025):

Hi @arthurgrigoletto you can find an example of this implementation here https://www.boilit.dev/b/nextjs-with-fastify-and-better-auth.

Enjoy!

<!-- gh-comment-id:2642730913 --> @brunowego commented on GitHub (Feb 7, 2025): Hi @arthurgrigoletto you can find an example of this implementation here https://www.boilit.dev/b/nextjs-with-fastify-and-better-auth. Enjoy!
Author
Owner

@maumercado commented on GitHub (Mar 1, 2025):

In case someone is still looking for this -> https://github.com/flaviodelgrosso/fastify-better-auth

<!-- gh-comment-id:2691920912 --> @maumercado commented on GitHub (Mar 1, 2025): In case someone is still looking for this -> https://github.com/flaviodelgrosso/fastify-better-auth
Author
Owner

@dosubot[bot] commented on GitHub (Jun 15, 2025):

Hi, @arthurgrigoletto. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • The issue involves an "UNAUTHORIZED" error when using headers with Fastify to create an organization and get a session.
  • The problem seems related to session management, with session cookies being set with a Max-Age of 0.
  • @brunowego suggested an implementation example, which received mixed reactions.
  • @maumercado provided a link to a related GitHub repository, which was positively received.

Next Steps:

  • Please let us know if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If there is no further activity, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:2974147725 --> @dosubot[bot] commented on GitHub (Jun 15, 2025): Hi, @arthurgrigoletto. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - The issue involves an "UNAUTHORIZED" error when using headers with Fastify to create an organization and get a session. - The problem seems related to session management, with session cookies being set with a Max-Age of 0. - @brunowego suggested an implementation example, which received mixed reactions. - @maumercado provided a link to a related GitHub repository, which was positively received. **Next Steps:** - Please let us know if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If there is no further activity, this issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8543