Authentication

Set up authentication in your app to allow users to sign in, sign up, and manage accounts securely.

Tools

  • Better Auth: Provides secure authentication with email/password, OAuth, and magic link support.

Setup

  1. For Google OAuth, sign up at Google Cloud Console and copy the Client ID and Client Secret.
  2. Update your config:
    shared/config.ts
    auth: {
        enablePasswordLogin: true, // Allow email/password login and registration
        enableEmailVerification: false, // Require email verification after signup
        enableMagicLink: false, // Allow passwordless login via email link
    
        oauthProviders: ['google'], // Enable Google OAuth
    
        redirectAfterSignIn: '/', // Where to redirect after successful login
        password: {
            minLength: 8,
            maxLength: 128,
            pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/,
        },
    },
    
    • Note: Add more OAuth providers (e.g., github, twitter, apple) to oauthProviders and set corresponding environment variables (e.g., OAUTH_GITHUB_CLIENT_ID, OAUTH_GITHUB_CLIENT_SECRET).
  3. Set the following environment variables:
    .env
    BETTER_AUTH_SECRET="your-long-random-string"
    OAUTH_GOOGLE_CLIENT_ID="your-google-client-id"
    OAUTH_GOOGLE_CLIENT_SECRET="your-google-client-secret"
    

Usage

  • Email/Password: Sign in, sign up, reset password, sign out
  • Google OAuth: Sign in with Google account
  • Magic Link: Sign in via email link
  • Verify Setup: Use user and loggedIn from useAuth composable