> ## Documentation Index
> Fetch the complete documentation index at: https://docs.turnkey.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Learn how to set up, log in, or sign up easily in your React app using @turnkey/react-wallet-kit.

## Overview

The Embedded Wallet Kit makes authentication incredibly simple. With just one function call, you can trigger a login or signup modal that automatically reflects the authentication methods you’ve configured in your Turnkey Dashboard. You can also call specific login and signup functions to create your own UI components and authentication flow.

## Quick authentication

The easiest way to handle authentication is using the `handleLogin` function from the `useTurnkey` hook. This will automatically show a modal with all the authentication methods you've enabled in your dashboard.

```tsx page.tsx theme={"system"}
"use client";
import { useTurnkey } from "@turnkey/react-wallet-kit";

function LoginButton() {
  const { handleLogin } = useTurnkey();
  return <button onClick={handleLogin}>Login / Sign Up</button>;
}
export default function Home() {
  return (
    <LoginButton /> 
  );
}
```

If you're following our quick start guide, we can now view our button.

<CodeGroup>
  ```bash npm theme={"system"}
  npm run dev
  ```

  ```bash pnpm theme={"system"}
  pnpm run dev
  ```

  ```bash yarn theme={"system"}
  yarn run dev
  ```
</CodeGroup>

When a user clicks this button, they'll see a modal with the authentication options you configured during the [organization setup](/sdks/react/getting-started#turnkey-organization-setup).

<img src="https://mintcdn.com/turnkey-0e7c1f5b/XOoCN4J8p4gXmFUJ/images/sdks/img/react/auth-component-1.png?fit=max&auto=format&n=XOoCN4J8p4gXmFUJ&q=85&s=6f80fb3608a56c95dbbb688817189d3e" alt="Login modal showing authentication options" width="876" height="688" data-path="images/sdks/img/react/auth-component-1.png" />

## UI customization

Want to customize the look and feel of the authentication modal? You can learn more about customizing the UI components in the [UI Customization](/sdks/react/ui-customization) section.

## Without a modal

If you need more control over the authentication flow, you can also call specific login and signup functions individually, without showing a modal.

```tsx theme={"system"}
import { useTurnkey } from "@turnkey/react-wallet-kit";

function CustomAuthButtons() {
  const { loginWithPasskey, signUpWithPasskey } = useTurnkey();

  return (
    <div>
      <button onClick={loginWithPasskey}>Log in with Passkey</button>
      <button onClick={signUpWithPasskey}>Sign Up with Passkey</button>
    </div>
  );
}
```

For a complete list of available authentication functions, check out the [SDK Reference](/generated-docs/react-wallet-kit/client-context-type-add-oauth-provider).

## Knowing when users are authenticated

To check if a user is authenticated, you can use the `authState` variable from the `useTurnkey` hook.

```tsx theme={"system"}
import { useTurnkey, AuthState } from "@turnkey/react-wallet-kit";

function AuthStatus() {
  const { authState, user, handleLogin } = useTurnkey();

  return (
    <div>
      {authState === AuthState.Authenticated ? (
        <p>Welcome back, {user?.userName}!</p>
      ) : (
        <button onClick={handleLogin}>Log in</button>
      )}
    </div>
  );
}
```

You can also set up an `onAuthenticationSuccess` callback passed in through the `TurnkeyProvider` to handle post-authentication logic, such as redirecting.

```tsx theme={"system"}
<TurnkeyProvider
  config={turnkeyConfig}
  callbacks={{
    onAuthenticationSuccess: ({ session }) => {
      console.log("User authenticated:", session);
      router.push("/dashboard");
    },
  }}
>
  {children}
</TurnkeyProvider>
```

## Configuring OAuth

Using OAuth requires additional configuration.

To start, ensure you enable OAuth and check the OAuth providers you want to use in the **Wallet Kit** section of the [Turnkey Dashboard](https://app.turnkey.com). See the [Getting Started](/sdks/react/getting-started) guide for more details.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/hs0edpJNaX3As9rj/images/sdks/img/react/oauth-panel.png?fit=max&auto=format&n=hs0edpJNaX3As9rj&q=85&s=3dc8f1ada3af0f3664c8c957d940c9d0" alt="OAuth providers configuration" width="1176" height="1200" data-path="images/sdks/img/react/oauth-panel.png" />

You can choose to enter your client IDs for each OAuth provider and the redirect url in the dashboard

<img src="https://mintcdn.com/turnkey-0e7c1f5b/hs0edpJNaX3As9rj/images/sdks/img/react/oauth-client-ids.png?fit=max&auto=format&n=hs0edpJNaX3As9rj&q=85&s=7eee5d396f0745a413c75195fd8edc19" alt="OAuth client IDs configuration" width="911" height="610" data-path="images/sdks/img/react/oauth-client-ids.png" />

Or, you can provide the client IDs and redirect URI through environment variables and pass them in the `TurnkeyProvider`'s config. This is useful if you want to use different OAuth client IDs or a different redirect URL for different environments (e.g., development, staging, production).

Here, you can also toggle `openOauthInPage` to `true` if you want OAuth to replace the current page instead of opening a new popup.

```tsx theme={"system"}
<TurnkeyProvider
  config={{
    authProxyConfigId: process.env.NEXT_PUBLIC_AUTH_PROXY_CONFIG_ID || "",
    organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID || "",

    auth: {
      oauthConfig: {
        openOauthInPage: true, // Set to true if you want OAuth to replace the current page instead of opening a new popup

        // You can also provide these values through the Turnkey dashboard:
        oauthRedirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI || "", // Eg: "https://your-app.com/home". This must match the redirect URI configured in your OAuth provider's dashboard.

        // You will typically get these from the OAuth provider's dashboard. Eg: Google developer console.
        googleClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
        appleClientId: process.env.NEXT_PUBLIC_APPLE_CLIENT_ID || "",
        facebookClientId: process.env.NEXT_PUBLIC_FACEBOOK_CLIENT_ID || "",
        xClientId: process.env.NEXT_PUBLIC_X_CLIENT_ID || "",
        discordClientId: process.env.NEXT_PUBLIC_DISCORD_CLIENT_ID || "",
      },
    },
  }}
>
  {children}
</TurnkeyProvider>
```

You can retrieve the client IDs from the OAuth provider's dashboard. Note that the redirect URI must match the one you configured in your `TurnkeyProvider` config.

For example, for Google, you can create a **web** client ID in the [Google developer console](https://console.developers.google.com/apis/credentials).

<img src="https://mintcdn.com/turnkey-0e7c1f5b/NNqJcRCxxsOsZobL/images/sdks/img/react/google-dev-console.png?fit=max&auto=format&n=NNqJcRCxxsOsZobL&q=85&s=f1426cd5c84bb0744628bb0f2faa0d38" alt="The Google developer console" width="2938" height="1648" data-path="images/sdks/img/react/google-dev-console.png" />

Your "Log in or sign up" modal should now include the OAuth options you configured.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/XOoCN4J8p4gXmFUJ/images/sdks/img/react/auth-component-2.png?fit=max&auto=format&n=XOoCN4J8p4gXmFUJ&q=85&s=f4a351a6a6b4b5e1c26aba449c30907d" alt="Authentication modal with OAuth options" width="858" height="862" data-path="images/sdks/img/react/auth-component-2.png" />

### OAuth2.0 providers (X, Discord, etc)

For OAuth providers that exclusively use OAuth2.0 (e.g., X, Discord), you will need to configure a few additional settings in your Turnkey Dashboard.

In the **Wallet Kit** section of the dashboard, head to the **Socials** tab and click **Add provider**.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/hs0edpJNaX3As9rj/images/sdks/img/react/socials-page.png?fit=max&auto=format&n=hs0edpJNaX3As9rj&q=85&s=5149ad2adea48de8f158cee90096b1e0" alt="OAuth2.0 providers configuration" width="1480" height="592" data-path="images/sdks/img/react/socials-page.png" />

Select the provider you want to add from the dropdown, and fill in the required fields. You can find these values in the provider's developer console. Any secrets will automatically be encrypted before uploading to Turnkey.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/b0fOk4Hn036S1cMU/images/sdks/img/react/add-social-login.png?fit=max&auto=format&n=b0fOk4Hn036S1cMU&q=85&s=c09a1f692fb56487c4ffab4e6952f60c" alt="Adding an OAuth2.0 provider" width="676" height="544" data-path="images/sdks/img/react/add-social-login.png" />

Once you've added the provider, head back to the **Authentication** tab, and enable the provider you just added under the **SDK Configuration** section.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/hs0edpJNaX3As9rj/images/sdks/img/react/twitter-toggled.png?fit=max&auto=format&n=hs0edpJNaX3As9rj&q=85&s=55000d6b8e2deda975818d7c054a20b1" alt="Enabling an OAuth2.0 provider" width="996" height="737" data-path="images/sdks/img/react/twitter-toggled.png" />

Click **Select** to choose your newly added client ID, then click **Save Settings**. You can also simply enter the client ID in the `TurnkeyProvider`'s config as shown above.

<img src="https://mintcdn.com/turnkey-0e7c1f5b/hs0edpJNaX3As9rj/images/sdks/img/react/select-id-twitter.png?fit=max&auto=format&n=hs0edpJNaX3As9rj&q=85&s=ebdbb0541729c073a5fca7a8aae01a13" alt="Selecting an OAuth2.0 provider" width="745" height="484" data-path="images/sdks/img/react/select-id-twitter.png" />

## Customize sub-organization creation

Need to configure default user names, passkey names, wallet creations or anything sub-org related? You can learn more about customizing the sub-orgs you create in the [Sub-Organization Customization](/sdks/react/sub-organization-customization) section.

## Next steps

Once your users are authenticated, you can start building embedded wallet functionality! Check out the [Using Embedded Wallets](/sdks/react/using-embedded-wallets) guide to learn how to create and manage wallets in your React app.
