# Source: https://www.better-auth.com/llms.txt/docs/plugins/one-tap.md

# One Tap

One Tap plugin for Better Auth



The One Tap plugin allows users to log in with a single tap using Google's One Tap API. The plugin
provides a simple way to integrate One Tap into your application, handling the client-side and server-side logic for you.

## Installation

### Add the Server Plugin

Add the One Tap plugin to your auth configuration:

```ts title="auth.ts"
import { betterAuth } from "better-auth";
import { oneTap } from "better-auth/plugins"; // [!code highlight]

export const auth = betterAuth({
    plugins: [ // [!code highlight]
        oneTap(), // Add the One Tap server plugin  // [!code highlight]
    ] // [!code highlight]
});
```

### Add the Client Plugin

Add the client plugin and specify where the user should be redirected after sign-in or if additional verification (like 2FA) is needed.

```ts
import { createAuthClient } from "better-auth/client";
import { oneTapClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
  plugins: [
    oneTapClient({
      clientId: "YOUR_CLIENT_ID",
      // Optional client configuration:
      autoSelect: false,
      cancelOnTapOutside: true,
      context: "signin",
      additionalOptions: {
        // Any extra options for the Google initialize method
      },
      // Configure prompt behavior and exponential backoff:
      promptOptions: {
        baseDelay: 1000,   // Base delay in ms (default: 1000)
        maxAttempts: 5     // Maximum number of attempts before triggering onPromptNotification (default: 5)
      }
    })
  ]
});
```

### Usage

#### Prompt Mode (Default)

To display the One Tap popup, simply call the oneTap method on your auth client:

```ts
await authClient.oneTap();
```

#### Button Mode

To render a "Sign In with Google" button instead of showing the automatic prompt, use the `button` option:

<Tabs items={["Vanilla JS", "React Ref"]}>
  <Tab value="Vanilla JS">
    ```tsx
    // In your component
    <div id="google-signin-button"></div>

    // Call oneTap with button configuration
    await authClient.oneTap({
      button: {
        container: "#google-signin-button", // CSS selector or HTMLElement
        config: {
          theme: "outline",
          size: "large",
          type: "standard",
          text: "signin_with"
        }
      }
    });
    ```
  </Tab>

  <Tab value="React Ref">
    ```tsx
    import { useEffect, useRef } from "react";

    function SignInButton() {
      const buttonRef = useRef<HTMLDivElement>(null);

      useEffect(() => {
        if (buttonRef.current) {
          authClient.oneTap({
            button: {
              container: buttonRef.current,
              config: {
                theme: "filled_blue",
                size: "large"
              }
            }
          });
        }
      }, []);

      return <div ref={buttonRef}></div>;
    }
    ```
  </Tab>
</Tabs>

### Customizing Redirect Behavior

By default, after a successful login the plugin will hard redirect the user to `/`. You can customize this behavior as follows:

#### Avoiding a Hard Redirect

Pass fetchOptions with an onSuccess callback to handle the login response without a page reload:

```ts
await authClient.oneTap({
  fetchOptions: {
    onSuccess: () => {
      // For example, use a router to navigate without a full reload:
      router.push("/dashboard");
    }
  }
});
```

#### Specifying a Custom Callback URL

To perform a hard redirect to a different page after login, use the callbackURL option:

```ts
await authClient.oneTap({
  callbackURL: "/dashboard"
});
```

#### Handling Prompt Dismissals with Exponential Backoff

If the user dismisses or skips the prompt, the plugin will retry showing the One Tap prompt using exponential backoff based on your configured promptOptions.

If the maximum number of attempts is reached without a successful sign-in, you can use the onPromptNotification callback to be notifiedâallowing you to render an alternative UI (e.g., a traditional Google Sign-In button) so users can restart the process manually:

```ts
await authClient.oneTap({
  onPromptNotification: (notification) => {
    console.warn("Prompt was dismissed or skipped. Consider displaying an alternative sign-in option.", notification);
    // Render your alternative UI here
  }
});
```

### Client Options

* `clientId`: The client ID for your Google One Tap API.
* `autoSelect`: Automatically select the account if the user is already signed in. Default is false.
* `cancelOnTapOutside`: Cancel the One Tap popup when the user taps outside it. To use this option, disable `promptOptions.fedCM`. Default is true.
* `uxMode`: The mode to use for the Google One Tap flow. Can be "popup" or "redirect". Default is "popup".
* `context`: The context in which the One Tap API should be used. Can be "signin", "signup", or "use". Default is "signin".
* `additionalOptions`: Extra options to pass to Google's initialize method as per the [Google Identity Services docs](https://developers.google.com/identity/gsi/web/reference/js-reference#google.accounts.id.prompt).
* `promptOptions`: Configuration for the prompt behavior and exponential backoff:
  * `baseDelay`: Base delay in milliseconds for retries. Default is 1000.
  * `maxAttempts`: Maximum number of prompt attempts before invoking the `onPromptNotification` callback. Default is 5.
  * `fedCM`: Whether to enable [Federated Credential Management](https://developer.mozilla.org/en-US/docs/Web/API/FedCM_API) (FedCM) support. Default is true.

### Button Mode Options

When using button mode, pass a `button` object to the `oneTap` action with the following properties:

* `container`: The HTML element or CSS selector where the button should be rendered (required)
* `config`: Button configuration options (optional):
  * `type`: Button type - `"standard"` (default) or `"icon"` (icon-only)
  * `theme`: Button theme - `"outline"` (default), `"filled_blue"`, or `"filled_black"`
  * `size`: Button size - `"large"` (default), `"medium"`, or `"small"`
  * `text`: Button text - `"signin_with"` (default), `"signup_with"`, `"continue_with"`, or `"signin"`
  * `shape`: Button shape - `"rectangular"` (default), `"pill"`, `"circle"`, or `"square"`
  * `logo_alignment`: Google logo alignment - `"left"` (default) or `"center"` (for standard button only)
  * `width`: Minimum button width in pixels (max 400)
  * `locale`: Display button using specified language code (e.g., `"zh_CN"`)

### Server Options

* `disableSignUp`: Disable the sign-up option, allowing only existing users to sign in. Default is false.
* `clientId`: Optionally, pass a client ID here if it is not provided in your social provider configuration.

### Authorized JavaScript origins

Ensure you have configured the Authorized JavaScript origins (e.g., [http://localhost:3000](http://localhost:3000), [https://example.com](https://example.com)) for your Client ID in the Google Cloud Console. This is a required step for the Google One Tap API, and it will not function correctly unless your origins are correctly set.