Skip to main content

Overview

Budgetron uses BetterAuth for authentication, providing a secure and flexible authentication system with support for multiple providers. All authentication is integrated with the oRPC API layer for type-safe auth operations.

Authentication Methods

Budgetron supports multiple authentication methods:
  1. Email & Password - Traditional username/password authentication
  2. Google OAuth - Sign in with Google (optional)
  3. Custom OAuth - Generic OAuth 2.0 provider support (optional)

Configuration

Authentication is configured in src/server/auth/config.ts:

Session Management

Getting the Session

The auth instance is a singleton that provides access to the current session:

RPC Context

Every RPC request includes the user’s session in the context:

Session Types

Authentication Flow

Sign Up

Procedure: api.auth.signUp
Implementation:
Post-Signup Flow:
  1. User account is created
  2. Welcome email is sent automatically
  3. Email verification link is sent
  4. User is redirected to dashboard (if autoSignIn: true)

Sign In (Email & Password)

Procedure: api.auth.signIn
Implementation:

Sign In (Social Provider)

Procedure: api.auth.signInWithSocial
Implementation:

Sign In (Custom OAuth)

Procedure: api.auth.signInWithOAuth

Get Current Session

Procedure: api.auth.session
Implementation:

Sign Out

Procedure: api.auth.signOut
Implementation:

Password Reset Flow

Request Password Reset

Procedure: api.auth.forgotPassword
This sends a password reset email with a token that expires in 15 minutes. Implementation:

Reset Password

Procedure: api.auth.resetPassword
Implementation:

Protected Procedures

Protected procedures automatically verify authentication and provide a typed session:

Authorization Middleware

The authorization middleware ensures only authenticated users can access protected procedures:

Client-Side Usage

React Component Example

Here’s a complete example of a password reset form:

Checking Authentication

On the server side, you can require authentication:

Email Verification

Automatic Email Sending

When a user signs up, BetterAuth automatically sends a verification email:

Database Schema

BetterAuth uses four main tables:

Users Table

Stores user account information:

Sessions Table

Stores active user sessions:

Accounts Table

Stores OAuth provider connections:

Verifications Table

Stores verification tokens:

Security Features

Token Expiration

  • Password Reset Tokens: 15 minutes
  • Email Verification Tokens: 15 minutes
  • Delete Account Tokens: 15 minutes

Account Linking

Users can link multiple authentication providers to the same account:

Disable Implicit Sign-Up

For OAuth providers, implicit sign-up can be disabled to require manual user creation:

Error Handling

Authentication errors are handled consistently across all procedures:
Common auth error codes:
  • UNAUTHORIZED - Invalid credentials or no session
  • FORBIDDEN - Feature disabled or email not verified
  • BAD_REQUEST - Invalid input (e.g., weak password)
  • CONFLICT - Email already exists

Next Steps