Files
chat/client/flutter/lib/providers/auth_provider.dart
2026-04-25 16:36:34 +08:00

165 lines
4.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:sales_chat/models/user.dart';
import 'package:sales_chat/services/api_service.dart';
class AuthProvider with ChangeNotifier {
final ApiService _apiService;
User? _user;
bool _isLoading = false;
String? _error;
AuthProvider(this._apiService);
User? get user => _user;
bool get isLoading => _isLoading;
String? get error => _error;
bool get isLoggedIn => _user != null;
Future<bool> get isAuthenticated => _apiService.isAuthenticated;
/// 使用邮箱/用户名和密码登录。
/// 至少提供其中一个 [email] 或 [username]。
Future<bool> login({
String? email,
String? username,
required String password,
}) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final response = await _apiService.login(
email: email,
username: username,
password: password,
);
_user = response.user;
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_error = _parseError(e);
_isLoading = false;
notifyListeners();
return false;
}
}
/// 以访客身份登录(临时用户)。
Future<bool> loginAsGuest({required String nickname}) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final response = await _apiService.createTemporaryUser(nickname: nickname);
_user = response.user;
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_error = _parseError(e);
_isLoading = false;
notifyListeners();
return false;
}
}
/// 注册新用户。
Future<bool> register({
String? username,
String? email,
String? nickname,
required String password,
}) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final response = await _apiService.register(
username: username,
email: email,
nickname: nickname,
password: password,
);
_user = response.user;
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
_error = _parseRegisterError(e);
_isLoading = false;
notifyListeners();
return false;
}
}
Future<void> logout() async {
_isLoading = true;
notifyListeners();
try {
await _apiService.logout();
} finally {
_user = null;
_isLoading = false;
notifyListeners();
}
}
Future<void> loadUser() async {
if (!await _apiService.isAuthenticated) return;
_isLoading = true;
notifyListeners();
try {
_user = await _apiService.getCurrentUser();
} catch (e) {
_user = null;
} finally {
_isLoading = false;
notifyListeners();
}
}
void clearError() {
_error = null;
notifyListeners();
}
String _parseError(dynamic e) {
final errStr = e.toString();
if (errStr.contains('401')) {
return 'Email/username or password incorrect';
} else if (errStr.contains('422') || errStr.contains('password')) {
return 'Email/username or password incorrect';
} else if (errStr.contains('timeout')) {
return 'Network timeout, please retry';
} else if (errStr.contains('SocketException')) {
return 'Network connection failed';
} else if (errStr.contains('banned')) {
return 'Account has been banned';
}
return 'Login failed, please retry';
}
String _parseRegisterError(dynamic e) {
final errorStr = e.toString();
if (errorStr.contains('exist')) {
return 'Username or email already exists';
} else if (errorStr.contains('empty')) {
return 'Username or email is required';
} else if (errorStr.contains('timeout')) {
return 'Network timeout, please retry';
} else if (errorStr.contains('SocketException')) {
return 'Network connection failed';
} else if (errorStr.contains('not allowed')) {
return 'Server does not allow new user registration';
}
return 'Registration failed, please retry';
}
}