- Add languageProvider (StateProvider<String>, default 'ru') with supportedLanguages map matching backend locale.Supported - Wire Accept-Language header into AuthInterceptor via languageGetter callback; all API requests now carry the current language - Sync language from user profile preferences into languageProvider on every ProfileNotifier load/update - Add language field to UpdateProfileRequest, serialized as preferences.language in PUT /profile - Profile screen: НАСТРОЙКИ section displays current language; edit sheet adds DropdownButtonFormField for language selection Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
163 lines
4.3 KiB
Dart
163 lines
4.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart' as fb;
|
|
|
|
import '../../shared/models/user.dart';
|
|
import '../api/api_client.dart';
|
|
import '../config/app_config.dart';
|
|
import '../locale/language_provider.dart';
|
|
import 'auth_service.dart';
|
|
import 'secure_storage.dart';
|
|
|
|
enum AuthStatus { unknown, authenticated, unauthenticated }
|
|
|
|
class AuthState {
|
|
final AuthStatus status;
|
|
final User? user;
|
|
final bool isLoading;
|
|
final String? error;
|
|
|
|
const AuthState({
|
|
this.status = AuthStatus.unknown,
|
|
this.user,
|
|
this.isLoading = false,
|
|
this.error,
|
|
});
|
|
|
|
AuthState copyWith({
|
|
AuthStatus? status,
|
|
User? user,
|
|
bool? isLoading,
|
|
String? error,
|
|
}) {
|
|
return AuthState(
|
|
status: status ?? this.status,
|
|
user: user ?? this.user,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
error: error,
|
|
);
|
|
}
|
|
}
|
|
|
|
class AuthNotifier extends StateNotifier<AuthState> {
|
|
final AuthService? _authService;
|
|
|
|
AuthNotifier(AuthService authService) : _authService = authService, super(const AuthState()) {
|
|
_checkAuth();
|
|
}
|
|
|
|
@visibleForTesting
|
|
AuthNotifier.test(super.initialState)
|
|
: _authService = null;
|
|
|
|
Future<void> _checkAuth() async {
|
|
final isAuth = await _authService!.isAuthenticated();
|
|
state = state.copyWith(
|
|
status: isAuth ? AuthStatus.authenticated : AuthStatus.unauthenticated,
|
|
);
|
|
}
|
|
|
|
Future<void> signInWithEmail(String email, String password) async {
|
|
state = state.copyWith(isLoading: true, error: null);
|
|
try {
|
|
final user = await _authService!.signInWithEmail(email, password);
|
|
state = AuthState(
|
|
status: AuthStatus.authenticated,
|
|
user: user,
|
|
);
|
|
} catch (e) {
|
|
state = state.copyWith(
|
|
isLoading: false,
|
|
error: _mapError(e),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> signInWithGoogle() async {
|
|
state = state.copyWith(isLoading: true, error: null);
|
|
try {
|
|
final user = await _authService!.signInWithGoogle();
|
|
state = AuthState(
|
|
status: AuthStatus.authenticated,
|
|
user: user,
|
|
);
|
|
} catch (e) {
|
|
state = state.copyWith(
|
|
isLoading: false,
|
|
error: _mapError(e),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> register(String email, String password, String name) async {
|
|
state = state.copyWith(isLoading: true, error: null);
|
|
try {
|
|
final user =
|
|
await _authService!.registerWithEmail(email, password, name);
|
|
state = AuthState(
|
|
status: AuthStatus.authenticated,
|
|
user: user,
|
|
);
|
|
} catch (e) {
|
|
state = state.copyWith(
|
|
isLoading: false,
|
|
error: _mapError(e),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> signOut() async {
|
|
await _authService!.signOut();
|
|
state = const AuthState(status: AuthStatus.unauthenticated);
|
|
}
|
|
|
|
String _mapError(dynamic e) {
|
|
if (e is fb.FirebaseAuthException) {
|
|
switch (e.code) {
|
|
case 'wrong-password':
|
|
case 'user-not-found':
|
|
return 'Неверный email или пароль';
|
|
case 'email-already-in-use':
|
|
return 'Email уже зарегистрирован';
|
|
case 'weak-password':
|
|
return 'Пароль слишком простой';
|
|
case 'invalid-email':
|
|
return 'Некорректный email';
|
|
default:
|
|
return 'Ошибка авторизации: ${e.message}';
|
|
}
|
|
}
|
|
return 'Произошла ошибка. Попробуйте позже.';
|
|
}
|
|
}
|
|
|
|
final appConfigProvider = Provider<AppConfig>((ref) {
|
|
return AppConfig.development;
|
|
});
|
|
|
|
final secureStorageProvider = Provider<SecureStorageService>((ref) {
|
|
return SecureStorageService();
|
|
});
|
|
|
|
final apiClientProvider = Provider<ApiClient>((ref) {
|
|
final config = ref.read(appConfigProvider);
|
|
final storage = ref.read(secureStorageProvider);
|
|
return ApiClient(
|
|
baseUrl: config.apiBaseUrl,
|
|
storage: storage,
|
|
languageGetter: () => ref.read(languageProvider),
|
|
);
|
|
});
|
|
|
|
final authServiceProvider = Provider<AuthService>((ref) {
|
|
return AuthService(
|
|
firebaseAuth: fb.FirebaseAuth.instance,
|
|
apiClient: ref.read(apiClientProvider),
|
|
storage: ref.read(secureStorageProvider),
|
|
);
|
|
});
|
|
|
|
final authProvider = StateNotifierProvider<AuthNotifier, AuthState>((ref) {
|
|
return AuthNotifier(ref.read(authServiceProvider));
|
|
});
|