Files
supabase/examples/auth/flutter-mfa/lib/main.dart
Chris Chinchilla d8bd6b047c docs: Examples Key changes (#45170)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Documentation**
* Updated examples and guides to use Supabase publishable (client) keys
instead of anon keys for client-side usage across frameworks and
platforms.
* Renamed environment variable examples and .env templates to reflect
publishable key naming.
* Adjusted sample requests and client-init examples to send/use the
publishable key via the apikey header where applicable.
* Updated references from service_role to secret for server-side
credential guidance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: fadymak <fady@fadymak.com>
2026-05-04 12:58:16 +02:00

103 lines
3.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:mfa_app/pages/auth/login_page.dart';
import 'package:mfa_app/pages/auth/register_page.dart';
import 'package:mfa_app/pages/home_page.dart';
import 'package:mfa_app/pages/list_mfa_page.dart';
import 'package:mfa_app/pages/mfa/verify_page.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:mfa_app/pages/mfa/enroll_page.dart';
void main() async {
await Supabase.initialize(
url: 'YOUR_SUPABASE_URL',
publishableKey: 'YOUR_PUBLISHABLE_KEY',
);
runApp(const MyApp());
}
/// Extract SupabaseClient instance in a handy variable
final supabase = Supabase.instance.client;
final _router = GoRouter(
routes: [
GoRoute(
path: HomePage.route,
builder: (context, state) => const HomePage(),
),
GoRoute(
path: ListMFAPage.route,
builder: (context, state) => ListMFAPage(),
),
GoRoute(
path: LoginPage.route,
builder: (context, state) => const LoginPage(),
),
GoRoute(
path: RegisterPage.route,
builder: (context, state) => const RegisterPage(),
),
GoRoute(
path: MFAEnrollPage.route,
builder: (context, state) => const MFAEnrollPage(),
),
GoRoute(
path: MFAVerifyPage.route,
builder: (context, state) => const MFAVerifyPage(),
),
],
redirect: (context, state) async {
// Any users can visit the /auth route
if (state.location.contains('/auth') == true) {
return null;
}
final session = supabase.auth.currentSession;
// A user without a session should be redirected to the register page
if (session == null) {
return RegisterPage.route;
}
final assuranceLevelData =
supabase.auth.mfa.getAuthenticatorAssuranceLevel();
// The user has not setup MFA yet, so send them to enroll MFA page.
if (assuranceLevelData.currentLevel == AuthenticatorAssuranceLevels.aal1) {
await supabase.auth.refreshSession();
final nextLevel =
supabase.auth.mfa.getAuthenticatorAssuranceLevel().nextLevel;
if (nextLevel == AuthenticatorAssuranceLevels.aal2) {
// The user has already setup MFA, but haven't login via MFA
// Redirect them to the verify page
return MFAVerifyPage.route;
} else {
// The user has not yet setup MFA
// Redirect them to the enrollment page
return MFAEnrollPage.route;
}
}
// The user has signed invia MFA, and is allowed to view any page.
return null;
},
);
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'MFA App',
debugShowCheckedModeBanner: false,
theme: ThemeData.light().copyWith(
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
),
),
routerConfig: _router,
);
}
}