Files
front-hokm/lib/features/auth/auth_repository.dart
T
2026-06-17 11:32:18 +03:30

46 lines
1.6 KiB
Dart

import '../../core/network/api_client.dart';
import '../../core/storage/token_storage.dart';
/// دسترسی به endpointهای احراز هویت (login-otp / check-otp).
class AuthRepository {
final ApiClient _api;
final TokenStorage _storage;
AuthRepository(this._api, this._storage);
/// درخواست ارسال کد یک‌بارمصرف.
Future<void> requestOtp(String mobile) async {
await _api.dio.post('/auth/login-otp', data: {'mobile': mobile});
}
/// اعتبارسنجی کد، ذخیره‌ی توکن JWT و بازگرداندنِ اینکه کاربر نام دارد یا نه.
/// اگر نام نداشته باشد، فرانت کاربر را به صفحه‌ی انتخاب نام/آواتار می‌برد.
Future<bool> verifyOtp(String mobile, String code) async {
final res = await _api.dio.post(
'/auth/check-otp',
data: {'mobile': mobile, 'token': code},
);
final token = res.data['token'] as String?;
if (token == null || token.isEmpty) {
throw Exception('no token in response');
}
await _storage.write(token);
final user = res.data['user'];
final name = (user is Map) ? user['first_name'] : null;
return name is String && name.trim().isNotEmpty;
}
/// تنظیم نام نمایشی و آواتار.
Future<void> updateProfile(String firstName, String avatar) async {
await _api.dio.post('/profile',
data: {'first_name': firstName, 'avatar': avatar});
}
Future<bool> isLoggedIn() async {
final t = await _storage.read();
return t != null && t.isNotEmpty;
}
Future<void> logout() => _storage.clear();
}