feat : login api call
This commit is contained in:
53
packages/auth/lib/data/common/dio_error_handler.dart
Normal file
53
packages/auth/lib/data/common/dio_error_handler.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
class DioErrorHandler {
|
||||
void handle(DioException error) {
|
||||
switch (error.response?.statusCode) {
|
||||
case 401:
|
||||
_handle401();
|
||||
break;
|
||||
case 403:
|
||||
_handle403();
|
||||
break;
|
||||
default:
|
||||
_handleGeneric(error);
|
||||
}
|
||||
}
|
||||
|
||||
//wrong password/user name => "detail": "No active account found with the given credentials" - 401
|
||||
void _handle401() {
|
||||
Get.showSnackbar(
|
||||
_errorSnackBar('نام کاربری یا رمز عبور اشتباه است'),
|
||||
);
|
||||
}
|
||||
|
||||
//wrong captcha => "detail": "Captcha code is incorrect" - 403
|
||||
void _handle403() {
|
||||
Get.showSnackbar(
|
||||
_errorSnackBar('کد امنیتی اشتباه است'),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleGeneric(DioException error) {
|
||||
// General error handling
|
||||
}
|
||||
|
||||
GetSnackBar _errorSnackBar(String message) {
|
||||
return GetSnackBar(
|
||||
titleText: Text(
|
||||
'خطا',
|
||||
style: AppFonts.yekan14.copyWith(color: Colors.white),
|
||||
),
|
||||
messageText: Text(
|
||||
message,
|
||||
style: AppFonts.yekan12.copyWith(color: Colors.white),
|
||||
),
|
||||
backgroundColor: AppColor.error,
|
||||
margin: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
borderRadius: 12,
|
||||
duration: Duration(milliseconds: 3500),
|
||||
snackPosition: SnackPosition.TOP,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:rasadyar_auth/data/common/constant.dart';
|
||||
import 'package:rasadyar_auth/data/common/dio_error_handler.dart';
|
||||
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:rasadyar_auth/data/services/auth_service.dart';
|
||||
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
|
||||
@@ -16,6 +17,7 @@ Future<void> setupAuthDI() async {
|
||||
diAuth.registerCachedFactory<AuthRepositoryImpl>(
|
||||
() => AuthRepositoryImpl(dioRemote),
|
||||
);
|
||||
diAuth.registerLazySingleton(() => AuthService());
|
||||
diAuth.registerLazySingleton(() => TokenStorageService());
|
||||
diAuth.registerLazySingleton<AuthService>(() => AuthService());
|
||||
diAuth.registerLazySingleton<TokenStorageService>(() => TokenStorageService());
|
||||
diAuth.registerLazySingleton<DioErrorHandler>(() => DioErrorHandler());
|
||||
}
|
||||
|
||||
@@ -12,10 +12,23 @@ abstract class LoginRequestModel with _$LoginRequestModel {
|
||||
String? captchaKey,
|
||||
}) = _LoginRequestModel;
|
||||
|
||||
factory LoginRequestModel.createWithCaptcha({
|
||||
required String username,
|
||||
required String password,
|
||||
required String captchaCode,
|
||||
required String captchaKey,
|
||||
}) {
|
||||
return LoginRequestModel(
|
||||
username: username,
|
||||
password: password,
|
||||
captchaCode: captchaCode,
|
||||
captchaKey: 'rest_captcha_$captchaKey.0',
|
||||
);
|
||||
}
|
||||
|
||||
factory LoginRequestModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$LoginRequestModelFromJson(json);
|
||||
|
||||
const LoginRequestModel._();
|
||||
|
||||
String get formattedCaptchaKey => 'rest_captcha_$captchaKey.0';
|
||||
}
|
||||
|
||||
@@ -14,64 +14,34 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
Future<AuthResponseModel?> login({
|
||||
required Map<String, dynamic> authRequest,
|
||||
}) async {
|
||||
final response = await safeCall<DioResponse<AuthResponseModel>>(
|
||||
call:
|
||||
() async => await _httpClient.post<AuthResponseModel>(
|
||||
'$_BASE_URL/login/',
|
||||
data: authRequest,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
),
|
||||
onSuccess: (response) {
|
||||
iLog(response);
|
||||
},
|
||||
onError: (error, trace) {
|
||||
throw Exception('Error during sign in: $error');
|
||||
},
|
||||
var res = await _httpClient.post<AuthResponseModel>(
|
||||
'$_BASE_URL/login/',
|
||||
data: authRequest,
|
||||
fromJson: AuthResponseModel.fromJson,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
|
||||
return response?.data;
|
||||
return res.data;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<CaptchaResponseModel?> captcha() async {
|
||||
final response = await safeCall<CaptchaResponseModel?>(
|
||||
call: () async {
|
||||
var res = await _httpClient.post<CaptchaResponseModel?>(
|
||||
'captcha/',
|
||||
fromJson: CaptchaResponseModel.fromJson,
|
||||
);
|
||||
return res.data;
|
||||
},
|
||||
onSuccess: (response) {
|
||||
return response;
|
||||
},
|
||||
onError: (error, trace) {
|
||||
throw Exception('Error during captcha : $error');
|
||||
},
|
||||
var res = await _httpClient.post<CaptchaResponseModel?>(
|
||||
'captcha/',
|
||||
fromJson: CaptchaResponseModel.fromJson,
|
||||
);
|
||||
return response;
|
||||
return res.data;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthResponseModel?> loginWithRefreshToken({
|
||||
required Map<String, dynamic> authRequest,
|
||||
}) async {
|
||||
final response = await safeCall<DioResponse<AuthResponseModel>>(
|
||||
call:
|
||||
() async => await _httpClient.post<AuthResponseModel>(
|
||||
'$_BASE_URL/login/',
|
||||
data: authRequest,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
),
|
||||
onSuccess: (response) {
|
||||
iLog(response);
|
||||
},
|
||||
onError: (error, trace) {
|
||||
throw Exception('Error during sign in: $error');
|
||||
},
|
||||
var res = await _httpClient.post<AuthResponseModel>(
|
||||
'$_BASE_URL/login/',
|
||||
data: authRequest,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
|
||||
return response?.data;
|
||||
return res.data;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -82,20 +52,11 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
|
||||
@override
|
||||
Future<bool> hasAuthenticated() async {
|
||||
final response = await safeCall<DioResponse<bool>>(
|
||||
call:
|
||||
() async => await _httpClient.get<bool>(
|
||||
'$_BASE_URL/login/',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
),
|
||||
onSuccess: (response) {
|
||||
iLog(response);
|
||||
},
|
||||
onError: (error, trace) {
|
||||
throw Exception('Error during sign in: $error');
|
||||
},
|
||||
final response = await _httpClient.get<bool>(
|
||||
'$_BASE_URL/login/',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
);
|
||||
|
||||
return response?.data ?? false;
|
||||
return response.data ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
import 'package:rasadyar_core/injection/di.dart';
|
||||
|
||||
|
||||
class TokenStorageService extends GetxService {
|
||||
static const String _boxName = 'secureBox';
|
||||
|
||||
Reference in New Issue
Block a user