forked from eatvio/eatvio_app
149 lines
4.7 KiB
Dart
149 lines
4.7 KiB
Dart
import 'dart:developer';
|
||
import 'package:eatvio_app/features/auth/models/credentials_model.dart';
|
||
import 'package:eatvio_app/features/auth/models/oauth_model.dart';
|
||
import 'package:eatvio_app/features/auth/models/oauth_login_request.dart';
|
||
import 'package:eatvio_app/features/auth/models/oauth_verification_confirm_request.dart';
|
||
import 'package:eatvio_app/features/auth/models/oauth_verification_confirm_response.dart';
|
||
import 'package:eatvio_app/features/auth/models/oauth_verification_request_response.dart';
|
||
import 'package:eatvio_app/features/auth/models/token_model.dart';
|
||
import 'package:eatvio_app/features/auth/services/auth_service.dart';
|
||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||
|
||
class AuthRepository {
|
||
final AuthService authService;
|
||
|
||
AuthRepository({required this.authService});
|
||
|
||
Future<TokenModel> getTokenByCredentials(String email, String password) async {
|
||
try {
|
||
return await authService.getTokenByCredentials(
|
||
credentials: CredentialsModel(email: email, password: password),
|
||
);
|
||
} catch (e, s) {
|
||
await Sentry.captureException(e, stackTrace: s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<TokenModel> signInViaSocialMedia({
|
||
required String provider,
|
||
required String accessToken,
|
||
required String refreshToken,
|
||
required String clientId,
|
||
}) async {
|
||
try {
|
||
return await authService.signInViaSocialMedia(
|
||
body: OAuthLoginRequest(provider: provider, accessToken: accessToken, refreshToken: refreshToken, clientId: clientId),
|
||
);
|
||
} catch (e, s) {
|
||
await Sentry.captureException(e, stackTrace: s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<String> signUpViaSocialMedia({
|
||
required String provider,
|
||
required String accessToken,
|
||
required String id,
|
||
required String refreshToken,
|
||
required String firstName,
|
||
required String lastName,
|
||
required String email,
|
||
required String phone,
|
||
required String dateOfBirth,
|
||
required String gender,
|
||
required int weight,
|
||
required int height,
|
||
required String clientId,
|
||
}) async {
|
||
try {
|
||
return (await authService.signUpViaSocialMedia(
|
||
body: OAuthModel(
|
||
provider: provider,
|
||
accessToken: accessToken,
|
||
id: id,
|
||
refreshToken: refreshToken,
|
||
firstName: firstName,
|
||
lastName: lastName,
|
||
email: email,
|
||
phone: phone,
|
||
dateOfBirth: dateOfBirth,
|
||
gender: gender == 'М' ? 'male' : 'female',
|
||
weight: weight,
|
||
height: height,
|
||
clientId: clientId,
|
||
),
|
||
))['token']!
|
||
as String;
|
||
} catch (e, s) {
|
||
await Sentry.captureException(e, stackTrace: s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<OAuthVerificationRequestResponse?> requestOAuthVerification({String? email, String? phone}) async {
|
||
try {
|
||
Map<String, dynamic>? body;
|
||
if (email != null) {
|
||
body = {'email': email};
|
||
} else if (phone != null) {
|
||
body = {'phone': phone};
|
||
}
|
||
if (body != null) {
|
||
return await authService.requestOAuthVerification(body: body);
|
||
}
|
||
return null;
|
||
} catch (e, s) {
|
||
await Sentry.captureException(e, stackTrace: s);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<OAuthVerificationConfirmResponse> confirmOAuthVerification({
|
||
required String provider,
|
||
required String accessToken,
|
||
required String refreshToken,
|
||
required String clientId,
|
||
required String id,
|
||
String? email,
|
||
String? phone,
|
||
required String code,
|
||
required String verificationType,
|
||
required String firstName,
|
||
required String lastName,
|
||
required String dateOfBirth,
|
||
required String gender,
|
||
required int weight,
|
||
required int height,
|
||
}) async {
|
||
try {
|
||
final request = OAuthVerificationConfirmRequest(
|
||
provider: provider,
|
||
accessToken: accessToken,
|
||
refreshToken: refreshToken,
|
||
clientId: clientId,
|
||
id: id,
|
||
email: email,
|
||
phone: phone,
|
||
code: code,
|
||
verificationType: verificationType,
|
||
firstName: firstName,
|
||
lastName: lastName,
|
||
dateOfBirth: dateOfBirth,
|
||
gender: gender,
|
||
weight: weight,
|
||
height: height,
|
||
);
|
||
// Логируем запрос перед отправкой
|
||
log(
|
||
'AuthRepository: Sending OAuth verification confirm request: provider=$provider, verificationType=$verificationType, code=$code, email=$email, phone=$phone',
|
||
);
|
||
return await authService.confirmOAuthVerification(body: request);
|
||
} catch (e, s) {
|
||
log('AuthRepository: Error in confirmOAuthVerification: $e');
|
||
await Sentry.captureException(e, stackTrace: s);
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|