mirror of
https://github.com/FoggedLens/deflock-app.git
synced 2026-02-12 16:52:51 +00:00
103 lines
2.4 KiB
Dart
103 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../services/auth_service.dart';
|
|
import 'settings_state.dart';
|
|
|
|
class AuthState extends ChangeNotifier {
|
|
final AuthService _auth = AuthService();
|
|
String? _username;
|
|
|
|
// Getters
|
|
bool get isLoggedIn => _username != null;
|
|
String get username => _username ?? '';
|
|
AuthService get authService => _auth;
|
|
|
|
// Initialize auth state and check existing login
|
|
Future<void> init(UploadMode uploadMode) async {
|
|
_auth.setUploadMode(uploadMode);
|
|
|
|
try {
|
|
if (await _auth.isLoggedIn()) {
|
|
_username = await _auth.restoreLogin();
|
|
}
|
|
} catch (e) {
|
|
print("AuthState: Error during auth initialization: $e");
|
|
}
|
|
}
|
|
|
|
Future<void> login() async {
|
|
try {
|
|
_username = await _auth.login();
|
|
} catch (e) {
|
|
print("AuthState: Login error: $e");
|
|
_username = null;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
await _auth.logout();
|
|
_username = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> refreshAuthState() async {
|
|
try {
|
|
if (await _auth.isLoggedIn()) {
|
|
_username = await _auth.restoreLogin();
|
|
} else {
|
|
_username = null;
|
|
}
|
|
} catch (e) {
|
|
print("AuthState: Auth refresh error: $e");
|
|
_username = null;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> forceLogin() async {
|
|
try {
|
|
_username = await _auth.forceLogin();
|
|
} catch (e) {
|
|
print("AuthState: Forced login error: $e");
|
|
_username = null;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> validateToken() async {
|
|
try {
|
|
return await _auth.isLoggedIn();
|
|
} catch (e) {
|
|
print("AuthState: Token validation error: $e");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Handle upload mode changes
|
|
Future<void> onUploadModeChanged(UploadMode mode) async {
|
|
_auth.setUploadMode(mode);
|
|
|
|
// Refresh user display for active mode, validating token
|
|
try {
|
|
if (await _auth.isLoggedIn()) {
|
|
final isValid = await validateToken();
|
|
if (isValid) {
|
|
_username = await _auth.restoreLogin();
|
|
} else {
|
|
await logout(); // This clears _username also.
|
|
}
|
|
} else {
|
|
_username = null;
|
|
}
|
|
} catch (e) {
|
|
_username = null;
|
|
print("AuthState: Mode change user restoration error: $e");
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<String?> getAccessToken() async {
|
|
return await _auth.getAccessToken();
|
|
}
|
|
} |