Documentation
Documentation
Introduction

Getting Started

Getting StartedInstallationQuick StartProject Structure

Architecture

Architecture OverviewTech StacktRPC MiddlewareDesign Principles

Patterns

Code Patterns & ConventionsFeature ModulesError HandlingType Safety

Database

DatabaseSchema DefinitionDatabase OperationsMigrationsCaching

API

tRPCProceduresRouterstRPC Proxy Setup
APIsOpenAPIREST Endpoints

Auth & Access

AuthenticationConfigurationOAuth ProvidersRolesSession Management
AuthorizationUser RolesPermissions

Routing & i18n

RoutingDeclarative RoutingNavigation
InternationalizationTranslationsLocale Routing

Components & UI

ComponentsButtonsFormsNavigationDialogs
StylesTailwind CSSThemingTypography

Storage

StorageConfigurationUsageBuckets

Configuration

ConfigurationEnvironment VariablesFeature Flags

Templates

Template GuidesCreate New FeatureCreate New PageCreate Database TableCreate tRPC RouterAdd Translations

Development

DevelopmentCommandsAI AgentsBest Practices

OAuth Providers

Configure OAuth providers like Google and GitHub

Overview

This template supports OAuth authentication with Google and GitHub providers through Better-Auth.

Supported Providers

ProviderStatusSetup Required
Google✅ ActiveEnvironment variables + OAuth app
GitHub✅ ActiveEnvironment variables + OAuth app

Google OAuth Setup

1. Create Google OAuth App

Go to Google Cloud Console

Visit Google Cloud Console

Create or Select Project

Create a new project or select an existing one

Enable Google+ API

Navigate to APIs & Services → Enable APIs and Services → Search for "Google+ API" → Enable

Create OAuth Credentials

  1. Go to Credentials → Create Credentials → OAuth 2.0 Client ID
  2. Configure OAuth consent screen if prompted
  3. Application type: Web application
  4. Add authorized redirect URI:
    https://your-app.com/api/auth/callback/google
  5. For development, also add:
    http://localhost:3000/api/auth/callback/google

Copy Credentials

Copy the Client ID and Client Secret

2. Configure Environment Variables

Add to .env.local:

GOOGLE_CLIENT_ID="your-google-client-id.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

3. Test Google Login

  1. Start your development server
  2. Navigate to the login page
  3. Click "Continue with Google"
  4. Authorize the application
  5. You should be redirected back and logged in

GitHub OAuth Setup

1. Create GitHub OAuth App

Go to GitHub Settings

Visit GitHub → Settings → Developer settings

Create OAuth App

  1. Navigate to OAuth Apps → New OAuth App
  2. Fill in the form:
    • Application name: Your App Name
    • Homepage URL: https://your-app.com
    • Authorization callback URL: https://your-app.com/api/auth/callback/github
  3. For development, use:
    • Homepage URL: http://localhost:3000
    • Authorization callback URL: http://localhost:3000/api/auth/callback/github

Copy Credentials

Copy the Client ID and generate a new Client Secret

2. Configure Environment Variables

Add to .env.local:

GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"

3. Test GitHub Login

  1. Start your development server
  2. Navigate to the login page
  3. Click "Continue with GitHub"
  4. Authorize the application
  5. You should be redirected back and logged in

Using OAuth in Components

Login Form with Social Providers

import { FormLogin } from "@/features/auth/components";

<FormLogin
  onSuccess={() => router.push(PageDashboard())}
  showSocialProviders={true}
/>

Manual OAuth Trigger

"use client";

import { authClient } from "@/lib/auth/client";
import { PageDashboard } from "@/routes";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";

export function SocialLogin() {
  const handleGoogleLogin = async () => {
    await authClient.signIn.social({
      provider: "google",
      callbackURL: PageDashboard(),
    });
  };

  const handleGitHubLogin = async () => {
    await authClient.signIn.social({
      provider: "github",
      callbackURL: PageDashboard(),
    });
  };

  return (
    <div className="space-y-2">
      <Button onClick={handleGoogleLogin} variant="outline" className="w-full">
        <Icon iconKey="BrandGoogle" className="mr-2" />
        Continue with Google
      </Button>
      
      <Button onClick={handleGitHubLogin} variant="outline" className="w-full">
        <Icon iconKey="BrandGithub" className="mr-2" />
        Continue with GitHub
      </Button>
    </div>
  );
}

OAuth Flow

Authentication Flow

  1. User clicks "Continue with [Provider]"
  2. User redirected to provider's authorization page
  3. User authorizes the application
  4. Provider redirects back with authorization code
  5. Server exchanges code for access token
  6. User profile fetched from provider
  7. User account created or linked
  8. Session created and user logged in
  9. Redirect to callback URL

Account Linking

If a user signs in with OAuth and an account with that email already exists:

  • Auto-link: Account is automatically linked if email is verified
  • Manual link: User prompted to link accounts

OAuth Data Stored

When a user signs in with OAuth, the following data is stored:

FieldDescription
providerIdProvider name (google, github)
accountIdProvider's user ID
emailUser's email
nameUser's full name
imageProfile picture URL

Manage OAuth Connections

Link/Unlink Providers

import { FormManageProviders } from "@/features/auth/components";

<FormManageProviders />

This component allows users to:

  • View linked OAuth accounts
  • Link new OAuth providers
  • Unlink OAuth providers (if password is set)

Security Considerations

1. Callback URL Validation

Always validate callback URLs match your domain:

// In OAuth callback handler
const allowedCallbacks = [
  "https://your-app.com",
  "http://localhost:3000", // Dev only
];

2. State Parameter

Better-Auth automatically handles CSRF protection via the state parameter.

3. Scope Limitations

Only request necessary scopes:

  • Google: email, profile
  • GitHub: user:email

4. Email Verification

OAuth emails are automatically marked as verified since they're verified by the provider.

Troubleshooting

Redirect URI Mismatch

Error: redirect_uri_mismatch

Solution:

  • Check the redirect URI in provider settings exactly matches
  • Include protocol (http/https)
  • No trailing slashes
  • Match port number in development

Invalid Client

Error: invalid_client

Solution:

  • Verify CLIENT_ID and CLIENT_SECRET are correct
  • Check environment variables are loaded
  • Restart development server after adding variables

OAuth Not Working in Production

Checklist:

  • Update BETTER_AUTH_URL to production domain
  • Add production callback URL to provider settings
  • Use HTTPS in production
  • Check environment variables are set in deployment

User Email Not Available

Some providers may not return email if:

  • Email is not verified on provider
  • Email permission not granted
  • Email scope not requested

Adding More Providers

Better-Auth supports many OAuth providers. To add more:

  1. Install provider package if needed
  2. Add provider configuration to src/lib/auth/server.ts
  3. Add environment variables
  4. Update UI components to include new provider

See Better-Auth documentation for more providers.

Next Steps

Session Management

User Roles

Configuration

On this page

Overview
Supported Providers
Google OAuth Setup
1. Create Google OAuth App
Go to Google Cloud Console
Create or Select Project
Enable Google+ API
Create OAuth Credentials
Copy Credentials
2. Configure Environment Variables
3. Test Google Login
GitHub OAuth Setup
1. Create GitHub OAuth App
Go to GitHub Settings
Create OAuth App
Copy Credentials
2. Configure Environment Variables
3. Test GitHub Login
Using OAuth in Components
Login Form with Social Providers
Manual OAuth Trigger
OAuth Flow
Authentication Flow
Account Linking
OAuth Data Stored
Manage OAuth Connections
Link/Unlink Providers
Security Considerations
1. Callback URL Validation
2. State Parameter
3. Scope Limitations
4. Email Verification
Troubleshooting
Redirect URI Mismatch
Invalid Client
OAuth Not Working in Production
User Email Not Available
Adding More Providers
Next Steps