Initial commit: Captain Claude Mobile App
- Flutter app with chat and terminal screens - WebSocket integration for real-time chat - xterm integration for screen sessions - Markdown rendering with code blocks - JWT authentication Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
63
lib/providers/auth_provider.dart
Normal file
63
lib/providers/auth_provider.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../models/user.dart';
|
||||
import '../services/api_service.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthProvider with ChangeNotifier {
|
||||
final ApiService _apiService = ApiService();
|
||||
late final AuthService _authService;
|
||||
|
||||
User? _user;
|
||||
bool _isLoading = true;
|
||||
String? _error;
|
||||
|
||||
AuthProvider() {
|
||||
_authService = AuthService(_apiService);
|
||||
_loadStoredUser();
|
||||
}
|
||||
|
||||
User? get user => _user;
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isAuthenticated => _user != null && !_user!.isExpired;
|
||||
String? get error => _error;
|
||||
String? get token => _user?.token;
|
||||
ApiService get apiService => _apiService;
|
||||
|
||||
Future<void> _loadStoredUser() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
_user = await _authService.getStoredUser();
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<bool> login(String username, String password) async {
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_user = await _authService.login(username, password);
|
||||
_isLoading = false;
|
||||
|
||||
if (_user == null) {
|
||||
_error = 'Invalid credentials';
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return _user != null;
|
||||
} catch (e) {
|
||||
_error = 'Connection error';
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _authService.logout();
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user