getAccessToken not implemented? #1207

Closed
opened 2026-03-13 08:28:13 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @wiktor-jurek on GitHub (May 15, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Setup better-auth:

    • Configure better-auth with an OAuth provider (e.g., Google) and a database (e.g., PostgreSQL) as per the documentation.
    • Ensure the necessary environment variables (client ID, client secret, database URL, NEXTAUTH_URL, NEXTAUTH_SECRET) are set.
  2. Implement Authentication Flow:

    • Create a sign-in mechanism using better-auth (e.g., authClient.signIn.social({ provider: "google" }) on the client-side or auth.api.signInSocial on the server-side).
  3. Authenticate User:

    • Run the application.
    • Initiate the sign-in process.
    • Successfully authenticate with the OAuth provider and get redirected back to the application. A session is established by better-auth.
  4. Attempt to Retrieve Provider's Access Token Server-Side:

    • In a server-side Next.js API route or a server component, after a user is authenticated:
      • Obtain the current session using await auth.api.getSession({ headers }).
      • Attempt to retrieve the OAuth provider's (e.g., Google's) raw access token from the session or via an auth.api method.
        • Attempt 1 (Incorrect): Trying to use a potentially internal session token like session.session.token (this is not the provider's OAuth token).
        • Attempt 2 (Incorrect): Trying to call a non-existent or incorrectly used method like await auth.api.getAccessToken({...}).
  5. Use Token for Provider API Call:

    • Attempt to use the retrieved (or attempted-to-be-retrieved) token to make an authenticated request to the OAuth provider's API (e.g., listing Google BigQuery datasets).

Current vs. Expected behavior

Expected Behavior:

  • There should be a clear and documented server-side mechanism within better-auth to reliably access the OAuth provider's raw access token (and potentially refresh token if applicable) associated with the authenticated user's session.
  • This token should be usable for making authenticated API calls to the provider's services.

Actual Behavior:

  • If using an internal better-auth session.token: The provider's API rejects the token as invalid.
  • If attempting to use a method like auth.api.getAccessToken: A TypeError occurs (e.g., auth.api.getAccessToken is not a function), indicating the method does not exist or is being used incorrectly.

What version of Better Auth are you using?

1.2.7

Provide environment information

**Environment:**

* `better-auth` version: `^1.2.7`
* Next.js version: `15.3.2` 
* Node.js version:  v20.2
* Database: PostgreSQL
* MacOS

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { Pool } from "pg";

 
export const auth = betterAuth({
    database: new Pool({
        connectionString: process.env.DATABASE_URL,
    }),

    socialProviders: {
        google: { 
            clientId: process.env.GOOGLE_CLIENT_ID as string, 
            clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
            
            scope: [
                "https://www.googleapis.com/auth/bigquery",
                "https://www.googleapis.com/auth/bigquery.readonly",
                "https://www.googleapis.com/auth/cloud-platform.read-only",
                "email",
                "profile"
            ]
        },
        
    },
})

Additional context

No response

Originally created by @wiktor-jurek on GitHub (May 15, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. **Setup `better-auth`:** * Configure `better-auth` with an OAuth provider (e.g., Google) and a database (e.g., PostgreSQL) as per the documentation. * Ensure the necessary environment variables (client ID, client secret, database URL, `NEXTAUTH_URL`, `NEXTAUTH_SECRET`) are set. 2. **Implement Authentication Flow:** * Create a sign-in mechanism using `better-auth` (e.g., `authClient.signIn.social({ provider: "google" })` on the client-side or `auth.api.signInSocial` on the server-side). 3. **Authenticate User:** * Run the application. * Initiate the sign-in process. * Successfully authenticate with the OAuth provider and get redirected back to the application. A session is established by `better-auth`. 4. **Attempt to Retrieve Provider's Access Token Server-Side:** * In a server-side Next.js API route or a server component, after a user is authenticated: * Obtain the current session using `await auth.api.getSession({ headers })`. * Attempt to retrieve the OAuth provider's (e.g., Google's) raw access token from the session or via an `auth.api` method. * **Attempt 1 (Incorrect):** Trying to use a potentially internal session token like `session.session.token` (this is not the provider's OAuth token). * **Attempt 2 (Incorrect):** Trying to call a non-existent or incorrectly used method like `await auth.api.getAccessToken({...})`. 5. **Use Token for Provider API Call:** * Attempt to use the retrieved (or attempted-to-be-retrieved) token to make an authenticated request to the OAuth provider's API (e.g., listing Google BigQuery datasets). ### Current vs. Expected behavior **Expected Behavior:** * There should be a clear and documented server-side mechanism within `better-auth` to reliably access the OAuth provider's raw access token (and potentially refresh token if applicable) associated with the authenticated user's session. * This token should be usable for making authenticated API calls to the provider's services. **Actual Behavior:** * **If using an internal `better-auth` session.token:** The provider's API rejects the token as invalid. * **If attempting to use a method like `auth.api.getAccessToken`:** A `TypeError` occurs (e.g., `auth.api.getAccessToken is not a function`), indicating the method does not exist or is being used incorrectly. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash **Environment:** * `better-auth` version: `^1.2.7` * Next.js version: `15.3.2` * Node.js version: v20.2 * Database: PostgreSQL * MacOS ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { Pool } from "pg"; export const auth = betterAuth({ database: new Pool({ connectionString: process.env.DATABASE_URL, }), socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, scope: [ "https://www.googleapis.com/auth/bigquery", "https://www.googleapis.com/auth/bigquery.readonly", "https://www.googleapis.com/auth/cloud-platform.read-only", "email", "profile" ] }, }, }) ``` ### Additional context _No response_
Author
Owner

@wiktor-jurek commented on GitHub (May 15, 2025):

Update: I was on version 1.2.7. Upgraded to the 1.2.8 alpha and we're gucci.

@wiktor-jurek commented on GitHub (May 15, 2025): Update: I was on version 1.2.7. Upgraded to the 1.2.8 alpha and we're gucci.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1207