Supabase Next.js Template

repository·main·Indexed 43 days ago

https://github.com/razikus/supabase-nextjs-template

A production-ready, full-stack SaaS template built with Next.js 15, Supabase, and Tailwind CSS. It features authentication with MFA/OAuth, user management, file storage, and task management demos. Includes a PricingService for subscription tier management, a GlobalProvider for application-wide state, and pre-built components for MFA setup and verification.

Tokens
11.3K
Snippets
43
Records
57
Agent score
55%

What's inside Supabase Next.js Template

  1. Project structure overview

    main

    The project follows the Expo Router file-based routing convention:

    • app/: Contains all screens.
      • (auth)/: Authentication stack (login, register, password reset, 2FA).
      • (app)/: Authenticated application screens (home, settings, storage, tasks).
      • _layout.tsx: Root layout and routing configuration.
    • components/: Reusable UI components (e.g., MFASetup.tsx, ui/button.tsx).
    • lib/: Core logic and utilities (Supabase client, i18n config, storage wrappers).
    • locales/: JSON files for internationalization.
    • constants/: Theme and configuration constants.
  2. How the password reset flow works

    main

    The password reset process relies on deep link interception:

    1. The user requests a reset via the app.
    2. An email is sent containing a link: yourappscheme://reset-password#access_token=...&refresh_token=....
    3. When the user clicks the link, the app intercepts it in app/_layout.tsx.
    4. The app extracts the tokens from the hash fragment.
    5. The session is established using supabase.auth.setSession().
    6. The user is redirected to the reset-password screen to set a new password.
  3. How password reset deep linking works

    main

    The template handles password resets via deep links. When a user clicks a reset link in their email, the app intercepts the URL in app/_layout.tsx.

    1. The link format is: yourappscheme://reset-password#access_token=...&refresh_token=....
    2. The app extracts the tokens from the hash fragment.
    3. It establishes a session using supabase.auth.setSession().
    4. The user is then redirected to the password reset form screen to set a new password.
  4. How MFA (Multi-Factor Authentication) registration works

    main

    The template supports TOTP-based MFA. The registration flow is:

    1. User navigates to Settings → MFA.
    2. User provides a device name.
    3. The app displays a QR code.
    4. User scans the QR code using an authenticator app (e.g., Google Authenticator, Authy).
    5. User enters the 6-digit code from the app.
    6. The app verifies and registers the factor. Multiple devices can be registered.
  5. How MFA (Multi-Factor Authentication) enrollment works

    main

    The template supports TOTP-based MFA:

    1. User navigates to Settings → MFA.
    2. User provides a device name.
    3. The app displays a QR code (using react-native-qrcode-svg).
    4. User scans the code with an authenticator app (e.g., Google Authenticator).
    5. User enters the 6-digit code from the app.
    6. The factor is verified and enrolled in Supabase.
  6. Setup the Supabase Expo Mobile Template

    main

    To get started with the mobile SaaS template, follow these steps:

    1. Backend Setup: Configure your Supabase backend following the main repository instructions. Ensure you have a Project URL, Anon Key, applied migrations, configured storage buckets, and enabled RLS policies.
    2. Install Dependencies:
      cd supabase-expo-template
      yarn install
    3. Configure Environment Variables: Create a .env file in the root directory:
      EXPO_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
      EXPO_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-here
    4. Configure App Settings: Edit app.json to set your app name, slug, scheme, and bundle identifiers/package names.
    5. Configure Supabase Redirect URLs: Add your deep link scheme to the Supabase dashboard or supabase/config.toml to handle password resets:
      [auth]
      additional_redirect_urls = ["yourappscheme://reset-password"]
    6. Run the App:
      # Start development server
      npx expo start
      
      # Run on iOS simulator
      npx expo run:ios
      
      # Run on Android simulator
      npx expo run:android
    cd supabase-expo-template
    yarn install
  7. Install and set up the Supabase Expo Mobile Template

    main

    To get started with the mobile template, follow these steps:

    1. Backend Setup: Ensure you have a Supabase project with a URL, Anon Key, migrations applied, storage buckets configured, and RLS policies enabled.
    2. Install Dependencies:
      cd supabase-expo-template
      yarn install
    3. Configure Environment Variables: Create a .env file in the root directory with your Supabase credentials:
      EXPO_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
      EXPO_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-here
    4. Configure App Settings: Update app.json with your app's name, slug, and unique schemes for iOS and Android.
    5. Configure Supabase Redirect URLs: Add your app's deep link scheme to the Supabase dashboard or supabase/config.toml to support password resets (e.g., yourappscheme://reset-password).
    6. Run the App:
      npx expo start
      # Or for native builds:
      npx expo run:ios
      npx expo run:android
    cd supabase-expo-template
    yarn install
  8. Customize the app theme and colors

    main

    To change the visual branding, edit constants/theme.ts. The template uses a light/dark mode object structure:

    export const Colors = {
      light: {
        text: '#11181C',
        background: '#fff',
        tint: '#0a7ea4', // Primary color
        icon: '#687076',
      },
      dark: {
        text: '#ECEDEE',
        background: '#151718',
        tint: '#fff',
        icon: '#9BA1A6',
      },
    };
    export const Colors = {
      light: {
        text: '#11181C',
        background: '#fff',
        tint: '#0a7ea4',
        icon: '#687076',
      },
      dark: {
        text: '#ECEDEE',
        background: '#151718',
        tint: '#fff',
        icon: '#9BA1A6',
      },
    };
  9. Set up local development environment

    main

    To run the template locally, follow these steps to link your local environment to a Supabase project and configure the Next.js frontend.

    1. Prerequisites

    Gather the following from your Supabase Project (Project Settings -> API and Database):

    • Project URL
    • Anon Key (anon public)
    • Service Key (service_role)
    • Database Password

    2. Initialize Supabase CLI

    Run these commands from the root of the repository:

    # Login to supabase
    npx supabase login
    
    # Link project to supabase (requires database password)
    npx supabase link
    
    # Send config to the server (may require confirmation 'y')
    npx supabase config push
    
    # Apply migrations to the linked project
    npx supabase migrations up --linked

    3. Configure Next.js

    Navigate to the nextjs folder and set up your environment variables:

    1. Run yarn to install dependencies.
    2. Copy .env.template to .env.local.
    3. Update .env.local with your Supabase credentials:
    NEXT_PUBLIC_SUPABASE_URL=https://APIURL
    NEXT_PUBLIC_SUPABASE_ANON_KEY=ANONKEY
    PRIVATE_SUPABASE_SERVICE_KEY=SERVICEROLEKEY
    1. Run yarn dev and visit http://localhost:3000.
    # Login to supabase
    npx supabase login
    
    # Link project to supabase (require database password) - you will get selector prompt
    npx supabase link
    
    # Send config to the server - may require confirmation (y)
    npx supabase config push
    
    # Up migrations
    npx supabase migrations up --linked
  10. Build the app for production using EAS

    main

    The template is configured for Expo Application Services (EAS) builds.

    iOS Production Build:

    eas build:configure
    eas build --platform ios --profile production
    eas submit --platform ios

    Android Production Build:

    eas build --platform android --profile production
    eas submit --platform android

    Build profiles are managed in eas.json. You can define development, preview, and production profiles to control distribution and development client settings.

    eas build --platform ios --profile production
  11. Customize colors and theme

    main

    Modify the application's color palette by editing constants/theme.ts:

    export const Colors = {
      light: {
        text: '#11181C',
        background: '#fff',
        tint: '#0a7ea4', // Primary color
        icon: '#687076',
      },
      dark: {
        text: '#ECEDEE',
        background: '#151718',
        tint: '#fff',
        icon: '#9BA1A6',
      },
    };
    export const Colors = {
      light: {
        text: '#11181C',
        background: '#fff',
        tint: '#0a7ea4',
        icon: '#687076',
      },
      dark: {
        text: '#ECEDEE',
        background: '#151718',
        tint: '#fff',
        icon: '#9BA1A6',
      },
    };