Module 4
Topic 2

Authentication

Implementing email/password login, signup, session management, and OAuth providers with Supabase.

Supabase Auth Overview Flutter Auth Quickstart supabase_flutter package
Supabase Auth Overview

Supabase provides a complete authentication system that supports:

  • Email/Password – Traditional email and password login
  • Magic Links – Passwordless login via email
  • OAuth Providers – Google, GitHub, Apple, Facebook, and more
  • Session Management – Automatic session handling with refresh tokens
  • Password Reset – Secure password reset flow
  • User Profiles – Store user data in PostgreSQL tables

🔐 Supabase Auth vs Firebase Auth

  • Same features – Email, OAuth, magic links, password reset
  • PostgreSQL integration – User data stored in auth.users table
  • RLS integration – Row Level Security policies work with auth
  • Simpler pricing – 50,000 monthly active users on free tier
  • Open source – Can self-host if needed
Setting Up Authentication

To use authentication in your Flutter app, you need to configure your Supabase project and initialize the client.

💡 Auth Flow Types

  • pkce – Proof Key for Code Exchange (recommended for mobile apps)
  • implicit – Implicit flow (simpler but less secure)
Sign Up

To create a new user account, use the signUp() method with email and password.

Sign In

To log in an existing user, use the signInWithPassword() method.

Magic Links (Passwordless Login)

Magic links allow users to log in without a password by sending a one-time link to their email.

OAuth Providers

Supabase supports multiple OAuth providers for social login:

🔵
Google
GitHub
🍎
Apple
🔷
Facebook
🐦
Twitter
🔗
LinkedIn

⚠️ OAuth Setup Required

  • Configure OAuth providers in Supabase Dashboard → Authentication → Providers
  • Add redirect URIs for your app (e.g., com.example.app://auth-callback )
  • For Android: Add intent-filter in AndroidManifest.xml
  • For iOS: Add URL Types in Info.plist
  • For web: Add the redirect URL in your Supabase project settings
Password Reset

To allow users to reset their password, use the resetPasswordForEmail() method.

Session Management

Supabase handles session management automatically with refresh tokens. Here's how to check the auth state.

Step-by-Step Explanation
1.
Initialize Supabase – Add the supabase_flutter package and initialize the client with your project credentials.
2.
Create Auth Service – Build a service class that wraps Supabase auth methods.
3.
Sign Up – Use signUp() with email and password, storing additional user data in the data parameter.
4.
Sign In – Use signInWithPassword() to log in existing users.
5.
Magic Links – Use signInWithOtp() for passwordless login via email.
6.
OAuth – Use signInWithOAuth() for social login with Google, GitHub, Apple, etc.
7.
Password Reset – Use resetPasswordForEmail() and updateUser() for password changes.
8.
Session Management – Listen to auth state changes with onAuthStateChange .
Common Mistakes
❌ Mistake 1: Not handling email verification

By default, Supabase requires email verification. Users must verify their email before they can log in.

✅ Correct: Handle verification flow

Show a verification message after sign up. Use the confirmEmail() method to handle the verification link.

❌ Mistake 2: Using the wrong redirect URL

OAuth and magic link redirects must match exactly what's configured in your Supabase dashboard.

✅ Correct: Configure redirect URLs correctly

For mobile apps, use a custom URL scheme like com.example.app://auth-callback .

❌ Mistake 3: Not handling session expiration

Sessions expire after a certain time. If you don't handle this, users will see errors.

✅ Correct: Use onAuthStateChange

Listen to auth state changes to react to session expiration and update your UI accordingly.

🎯 Key Takeaway

Supabase provides a complete authentication system with email/password, magic links, and OAuth providers. The session management is automatic with refresh tokens. Always handle verification flows and use Row Level Security to protect user data.