When building a mobile app, one of the most important (and sometimes scariest) parts is the login system. Fortunately, with Flutter and Supabase, setting up authentication becomes surprisingly easy and developer-friendly. In this article, we’ll explore the complete workflow of using Supabase Auth in a Flutter app from project setup to actual login logic. Let’s go!
What is Supabase?
Supabase is an open-source Firebase alternative. It offers a real-time Postgres database, authentication, storage, and edge functions. In other words: everything you need to build an app backend without reinventing the wheel.
Why Use Supabase for Authentication?
- Email/Password login out of the box
- Magic link support
- OAuth providers (Google, GitHub, etc.)
- Secure, scalable, and open source
- Works perfectly with Flutter using official SDK
Flutter + Supabase Auth Workflow Overview
Here’s the general flow of authentication using Flutter and Supabase:
- Create a Supabase project
- Install Flutter dependencies
- Initialize Supabase client
- Build login/register screens
- Call Supabase Auth API
- React to session changes (logged in or out)
Step 1: Create a Supabase Project
Go to supabase.com, create an account, and start a new project. You’ll get a project URL and anon key keep these safe, you'll use them in Flutter later.
Step 2: Install Flutter Dependencies
Add this to your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
supabase_flutter: ^1.10.17
Step 3: Initialize Supabase
In your main.dart
file:
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Supabase.initialize(
url: 'https://your-project.supabase.co',
anonKey: 'your-anon-key',
);
runApp(MyApp());
}
Step 4: Create a Login and Register Screen
Here’s a basic example for login:
Future signIn(String email, String password) async {
final response = await Supabase.instance.client.auth.signInWithPassword(
email: email,
password: password,
);
if (response.user != null) {
print('Login success: ${response.user!.email}');
} else {
print('Login failed');
}
}
And for registration:
Future signUp(String email, String password) async {
final response = await Supabase.instance.client.auth.signUp(
email: email,
password: password,
);
if (response.user != null) {
print('Registration successful');
} else {
print('Failed to register');
}
}
Step 5: Handling Auth State
Use this widget to check whether a user is logged in or not:
Widget build(BuildContext context) {
final session = Supabase.instance.client.auth.currentSession;
if (session != null) {
return HomeScreen();
} else {
return LoginScreen();
}
}
Tips
- Add magic link login via
signInWithOtp
- Use
auth.onAuthStateChange
for real-time state change - Secure routes based on auth state
Testing Your Flow
Test the login and register flow on both Android emulator and physical device. Supabase makes it easy to track users via their dashboard.
Flutter + Supabase is a powerful combo for building apps with authentication. The workflow is simple, clean, and scalable. You don’t need to manage tokens manually or worry about backend complexity Supabase handles that for you.
0 Comments:
Post a Comment