Skip to content

Commit a0be5d0

Browse files
committed
feat(auth): implement AuthGate using StreamBuilder for session tracking
1 parent d4fdfe6 commit a0be5d0

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:supabase_flutter/supabase_flutter.dart';
3+
import 'login_view.dart';
4+
import '../../../main/view/screens/main_screen.dart';
5+
6+
class AuthGate extends StatelessWidget {
7+
const AuthGate({super.key});
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
// StreamBuilder continously checks the auth state
12+
return StreamBuilder<AuthState>(
13+
stream: Supabase.instance.client.auth.onAuthStateChange,
14+
builder: (context, snapshot) {
15+
// Wait response from Supabase
16+
if (snapshot.connectionState == ConnectionState.waiting) {
17+
return const Scaffold(
18+
body: Center(child: CircularProgressIndicator()),
19+
);
20+
}
21+
22+
// Check if there is an active session ( user logged in )
23+
final session = snapshot.data?.session;
24+
25+
// if session exists -> Navigate to MainScreen
26+
if (session != null) {
27+
return const MainScreen();
28+
}
29+
30+
// if session not exists -> Navigate to LoginView
31+
return const LoginView();
32+
},
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)