this repo has no description

feat: implement token management and loading in ApiService and main app flow

+39 -1
+14 -1
lib/api.dart
··· 2 2 import 'dart:io'; 3 3 4 4 import 'package:at_uri/at_uri.dart'; 5 + import 'package:flutter_secure_storage/flutter_secure_storage.dart'; 5 6 import 'package:grain/app_logger.dart'; 6 7 import 'package:grain/dpop_client.dart'; 7 8 import 'package:grain/main.dart'; ··· 17 18 import 'models/profile.dart'; 18 19 19 20 class ApiService { 21 + static const _storage = FlutterSecureStorage(); 20 22 String? _accessToken; 21 23 Profile? currentUser; 22 24 Profile? loadedProfile; ··· 24 26 25 27 String get _apiUrl => AppConfig.apiUrl; 26 28 27 - setToken(String? token) { 29 + Future<void> loadToken() async { 30 + _accessToken = await _storage.read(key: 'access_token'); 31 + } 32 + 33 + Future<void> setToken(String? token) async { 28 34 _accessToken = token; 35 + if (token != null) { 36 + await _storage.write(key: 'access_token', value: token); 37 + } else { 38 + await _storage.delete(key: 'access_token'); 39 + } 29 40 } 41 + 42 + bool get hasToken => _accessToken != null && _accessToken!.isNotEmpty; 30 43 31 44 Future<AtprotoSession?> fetchSession() async { 32 45 if (_accessToken == null) return null;
+25
lib/main.dart
··· 23 23 24 24 Future<void> main() async { 25 25 await AppConfig.init(); 26 + await apiService.loadToken(); // Restore access token before app starts 26 27 appLogger.i('🚀 App started'); 27 28 runApp(const ProviderScope(child: MyApp())); 28 29 } ··· 36 37 37 38 class _MyAppState extends State<MyApp> { 38 39 bool isSignedIn = false; 40 + bool _loading = true; 39 41 String? displayName; 40 42 43 + @override 44 + void initState() { 45 + super.initState(); 46 + _checkToken(); 47 + } 48 + 49 + Future<void> _checkToken() async { 50 + await apiService.loadToken(); 51 + if (apiService.hasToken) { 52 + await apiService.fetchSession(); 53 + await apiService.fetchCurrentUser(); 54 + } 55 + setState(() { 56 + isSignedIn = apiService.hasToken; 57 + _loading = false; 58 + }); 59 + } 60 + 41 61 void handleSignIn() async { 42 62 setState(() { 43 63 isSignedIn = true; ··· 55 75 56 76 @override 57 77 Widget build(BuildContext context) { 78 + if (_loading) { 79 + return const MaterialApp( 80 + home: Scaffold(body: Center(child: CircularProgressIndicator())), 81 + ); 82 + } 58 83 return MaterialApp( 59 84 title: 'Grain', 60 85 theme: AppTheme.lightTheme,