Initial commit: family safety frontend project setup
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class AuthProvider with ChangeNotifier {
|
||||
final AuthService _authService;
|
||||
|
||||
String _token = '';
|
||||
bool _isLoading = false;
|
||||
String _error = '';
|
||||
|
||||
AuthProvider({AuthService? authService})
|
||||
: _authService = authService ?? AuthService();
|
||||
|
||||
String get token => _token;
|
||||
bool get isLoggedIn => _token.isNotEmpty;
|
||||
bool get isLoading => _isLoading;
|
||||
String get error => _error;
|
||||
|
||||
Future<void> login(String login, String password) async {
|
||||
_isLoading = true;
|
||||
_error = '';
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
var response = await _authService.login(login, password);
|
||||
var data = jsonDecode(response);
|
||||
_token = data['token'];
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
notifyListeners();
|
||||
rethrow;
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> register(String login, String password) async {
|
||||
_isLoading = true;
|
||||
_error = '';
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _authService.register(login, password);
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
notifyListeners();
|
||||
rethrow;
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void logout() {
|
||||
_token = '';
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user