a83001de0d
- 新增系统语言/时区读取工具函数 - SessionStore 扩展支持时区存储 - 启动流程自动检测并保存系统语言/时区 - 注册时传递语言/时区到后端 - 登录后从服务器同步语言/时区
76 lines
2.0 KiB
Dart
76 lines
2.0 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../../../../core/network/api_problem.dart';
|
|
import '../../../../data/network/api_client.dart';
|
|
import '../models/session_response.dart';
|
|
|
|
class AuthApi {
|
|
AuthApi({required ApiClient apiClient}) : _apiClient = apiClient;
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
Future<void> sendOtp({required String email}) async {
|
|
await _apiClient.postNoContent(
|
|
'/api/v1/auth/otp/send',
|
|
data: {'email': email},
|
|
);
|
|
}
|
|
|
|
Future<SessionResponse> createEmailSession({
|
|
required String email,
|
|
required String token,
|
|
String? language,
|
|
String? timezone,
|
|
}) async {
|
|
final data = <String, dynamic>{
|
|
'email': email,
|
|
'token': token,
|
|
};
|
|
if (language != null) data['language'] = language;
|
|
if (timezone != null) data['timezone'] = timezone;
|
|
final json = await _apiClient.postJson(
|
|
'/api/v1/auth/email-session',
|
|
data: data,
|
|
);
|
|
return SessionResponse.fromJson(json);
|
|
}
|
|
|
|
Future<void> deleteSession({required String refreshToken}) async {
|
|
final response = await _apiClient.rawDio.delete<Map<String, dynamic>>(
|
|
'/api/v1/auth/sessions',
|
|
data: {'refresh_token': refreshToken},
|
|
options: Options(
|
|
validateStatus: (status) => status != null && status < 500,
|
|
),
|
|
);
|
|
final status = response.statusCode ?? 500;
|
|
if (status == 204 || status == 401) {
|
|
return;
|
|
}
|
|
|
|
final data = response.data;
|
|
if (data is Map<String, dynamic>) {
|
|
throw ApiProblem(
|
|
status: status,
|
|
title: (data['title'] as String?) ?? 'Request failed',
|
|
detail: (data['detail'] as String?) ?? '',
|
|
code: data['code'] as String?,
|
|
);
|
|
}
|
|
|
|
throw ApiProblem(
|
|
status: status,
|
|
title: 'Request failed',
|
|
detail: 'Failed to delete session',
|
|
);
|
|
}
|
|
|
|
Future<SessionResponse> refreshSession({required String refreshToken}) async {
|
|
final json = await _apiClient.postJson(
|
|
'/api/v1/auth/sessions/refresh',
|
|
data: {'refresh_token': refreshToken},
|
|
);
|
|
return SessionResponse.fromJson(json);
|
|
}
|
|
}
|