Language Changes
This commit is contained in:
56
lib/api/interceptors/auth_interceptor.dart
Normal file
56
lib/api/interceptors/auth_interceptor.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../data/repositories/auth_repository.dart';
|
||||
|
||||
class AuthInterceptor extends Interceptor {
|
||||
final AuthRepository _authRepository;
|
||||
final Dio _dio;
|
||||
|
||||
AuthInterceptor(this._authRepository, this._dio);
|
||||
|
||||
@override
|
||||
Future<void> onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
// Skip auth header for login and refresh endpoints
|
||||
if (options.path.contains('/login') ||
|
||||
options.path.contains('/auth/refresh')) {
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
// Get token and add to request
|
||||
final token = await _authRepository.getAccessToken();
|
||||
if (token != null) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onError(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
// Handle 401 errors by refreshing token and retrying
|
||||
if (err.response?.statusCode == 401) {
|
||||
// On 401, try to get a new token
|
||||
final token = await _authRepository.getAccessToken();
|
||||
|
||||
if (token != null) {
|
||||
// If we have a new token, retry the request
|
||||
final opts = err.requestOptions;
|
||||
opts.headers['Authorization'] = 'Bearer $token';
|
||||
|
||||
try {
|
||||
final response = await _dio.fetch(opts);
|
||||
return handler.resolve(response);
|
||||
} on DioException catch (e) {
|
||||
return handler.next(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return handler.next(err);
|
||||
}
|
||||
}
|
93
lib/api/services/auth_service.dart
Normal file
93
lib/api/services/auth_service.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../features/auth/models/auth_token.dart';
|
||||
import '../../features/auth/models/auth_credentials.dart';
|
||||
import '../../core/errors/exceptions.dart';
|
||||
|
||||
|
||||
class AuthService {
|
||||
final Dio _dio;
|
||||
AuthService(this._dio);
|
||||
|
||||
Future<AuthToken> login(AuthCredentials credentials) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'/api/auth/login',
|
||||
data: credentials.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return AuthToken.fromJson(response.data);
|
||||
} else {
|
||||
throw AuthException('Login failed');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e.toString());
|
||||
}
|
||||
if (e.response?.statusCode == 401) {
|
||||
throw AuthException('Invalid credentials');
|
||||
}
|
||||
throw NetworkException('Network error during login');
|
||||
} catch (e) {
|
||||
throw UnexpectedException('Unexpected error during login: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<AuthToken> refreshToken(String refreshToken) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'/auth/refresh',
|
||||
data: {'refresh_token': refreshToken},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return AuthToken.fromJson(response.data);
|
||||
} else {
|
||||
throw AuthException('Token refresh failed');
|
||||
}
|
||||
} catch (e) {
|
||||
throw AuthException('Failed to refresh token: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> checkTpin() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/auth/tpin');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return response.data['tpinSet'] as bool;
|
||||
} else {
|
||||
throw AuthException('Failed to check TPIN status');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e.toString());
|
||||
}
|
||||
throw NetworkException('Network error during TPIN check');
|
||||
} catch (e) {
|
||||
throw UnexpectedException('Unexpected error during TPIN check: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setTpin(String tpin) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'/api/auth/tpin',
|
||||
data: {'tpin': tpin},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw AuthException('Failed to set TPIN');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e.toString());
|
||||
}
|
||||
throw NetworkException('Network error during TPIN setup');
|
||||
} catch (e) {
|
||||
throw UnexpectedException('Unexpected error during TPIN setup: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
43
lib/api/services/payment_service.dart
Normal file
43
lib/api/services/payment_service.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:kmobile/data/models/payment_response.dart';
|
||||
import 'package:kmobile/data/models/transfer.dart';
|
||||
|
||||
class PaymentService {
|
||||
final Dio _dio;
|
||||
PaymentService(this._dio);
|
||||
|
||||
Future<PaymentResponse> processQuickPayWithinBank(Transfer transfer) async {
|
||||
try {
|
||||
await Future.delayed(const Duration(seconds: 3)); // Simulate delay
|
||||
final response =
|
||||
await _dio.post('/api/payment/transfer', data: transfer.toJson());
|
||||
|
||||
return PaymentResponse(
|
||||
isSuccess: true,
|
||||
date: DateTime.now(),
|
||||
creditedAccount: transfer.toAccount,
|
||||
amount: transfer.amount,
|
||||
currency: "INR",
|
||||
errorMessage: response.data['errorMessage'],
|
||||
errorCode: response.data['errorCode'],
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
log('DioException: ${e.toString()}');
|
||||
if (e.response?.data != null) {
|
||||
return PaymentResponse(
|
||||
isSuccess: false,
|
||||
errorMessage: e.response?.data['error'] ?? 'Unknown error',
|
||||
errorCode: e.response?.data['status'] ?? 'UNKNOWN_ERROR',
|
||||
);
|
||||
}
|
||||
throw Exception(
|
||||
'Failed to process quick pay within bank: ${e.toString()}');
|
||||
} catch (e) {
|
||||
log('Unexpected error: ${e.toString()}');
|
||||
throw Exception(
|
||||
'Failed to process quick pay within bank: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
31
lib/api/services/user_service.dart
Normal file
31
lib/api/services/user_service.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:kmobile/data/models/user.dart';
|
||||
|
||||
class UserService {
|
||||
final Dio _dio;
|
||||
UserService(this._dio);
|
||||
|
||||
Future<List<User>> getUserDetails() async {
|
||||
try {
|
||||
final response = await _dio.get('/api/customer/details');
|
||||
if (response.statusCode == 200) {
|
||||
log('Response: ${response.data}');
|
||||
return (response.data as List)
|
||||
.map((user) => User.fromJson(user))
|
||||
.toList();
|
||||
} else {
|
||||
throw Exception('Failed to load customer details');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e.toString());
|
||||
}
|
||||
throw Exception('Network error: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Unexpected error: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user