feat: add Flutter web frontend with authentication and recipe management features
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import 'dart:convert';
|
||||
import '../../core/api/api_client.dart';
|
||||
import '../../core/platform/token_storage.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final ApiClient _api;
|
||||
final ITokenStorage _storage;
|
||||
|
||||
AuthRepository(this._api, this._storage);
|
||||
|
||||
Future<String> login(String email, String password) async {
|
||||
final response = await _api.post(
|
||||
'/api/auth/login',
|
||||
jsonEncode({'email': email, 'password': password}),
|
||||
);
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
throw Exception('Login failed: ${response.statusCode}');
|
||||
}
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final token = data['access_token'] as String;
|
||||
await _storage.saveToken(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
Future<void> logout() => _storage.deleteToken();
|
||||
|
||||
Future<String?> getToken() => _storage.getToken();
|
||||
}
|
||||
Reference in New Issue
Block a user