feat: add Flutter web frontend with authentication and recipe management features
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/api/api_providers.dart';
|
||||
import '../../../core/platform/platform_providers.dart';
|
||||
import 'auth_repository.dart';
|
||||
|
||||
final authRepositoryProvider = Provider<AuthRepository>((ref) {
|
||||
return AuthRepository(
|
||||
ref.watch(apiClientProvider),
|
||||
ref.watch(tokenStorageProvider),
|
||||
);
|
||||
});
|
||||
|
||||
final authStateProvider = AsyncNotifierProvider<AuthNotifier, String?>(() {
|
||||
return AuthNotifier();
|
||||
});
|
||||
|
||||
class AuthNotifier extends AsyncNotifier<String?> {
|
||||
@override
|
||||
Future<String?> build() async {
|
||||
return ref.watch(authRepositoryProvider).getToken();
|
||||
}
|
||||
|
||||
Future<void> login(String email, String password) async {
|
||||
state = const AsyncLoading();
|
||||
state = await AsyncValue.guard(
|
||||
() => ref.read(authRepositoryProvider).login(email, password),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await ref.read(authRepositoryProvider).logout();
|
||||
state = const AsyncData(null);
|
||||
}
|
||||
}
|
||||
@@ -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