api integration
This commit is contained in:
28
lib/data/models/transaction.dart
Normal file
28
lib/data/models/transaction.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
class Transaction {
|
||||
final String? id;
|
||||
final String? name;
|
||||
final String? date;
|
||||
final int? amount;
|
||||
final String? type;
|
||||
|
||||
Transaction({this.id, this.name, this.date, this.amount, this.type});
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'date': date,
|
||||
'amount': amount,
|
||||
'type': type,
|
||||
};
|
||||
}
|
||||
|
||||
factory Transaction.fromJson(Map<String, dynamic> json) {
|
||||
return Transaction(
|
||||
id: json['id'] as String?,
|
||||
name: json['name'] as String?,
|
||||
date: json['date'] as String?,
|
||||
amount: json['amount'] as int?,
|
||||
type: json['type'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
@@ -1,25 +1,30 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:kmobile/data/models/transaction.dart';
|
||||
|
||||
class User extends Equatable {
|
||||
|
||||
final String accountNo;
|
||||
final String accountType;
|
||||
final String bookingNumber;
|
||||
final String branchId;
|
||||
final String currency;
|
||||
final String? accountNo;
|
||||
final String? accountType;
|
||||
final String? bookingNumber;
|
||||
final String? branchId;
|
||||
final String? currency;
|
||||
final String? productType;
|
||||
final String? approvedAmount;
|
||||
final String availableBalance;
|
||||
final String currentBalance;
|
||||
final String name;
|
||||
final String mobileNo;
|
||||
final String address;
|
||||
final String picode;
|
||||
|
||||
final String? availableBalance;
|
||||
final String? currentBalance;
|
||||
final String? name;
|
||||
final String? mobileNo;
|
||||
final String? address;
|
||||
final String? picode;
|
||||
final int? activeAccounts;
|
||||
final String? cifNumber;
|
||||
final String? primaryId;
|
||||
final String? dateOfBirth;
|
||||
final List<Transaction>? transactions;
|
||||
|
||||
const User({
|
||||
required this.accountNo,
|
||||
required this.accountType,
|
||||
required this.bookingNumber,
|
||||
this.bookingNumber,
|
||||
required this.branchId,
|
||||
required this.currency,
|
||||
this.productType,
|
||||
@@ -30,14 +35,42 @@ class User extends Equatable {
|
||||
required this.mobileNo,
|
||||
required this.address,
|
||||
required this.picode,
|
||||
this.transactions,
|
||||
this.activeAccounts,
|
||||
this.cifNumber,
|
||||
this.primaryId,
|
||||
this.dateOfBirth,
|
||||
});
|
||||
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'stAccountNo': accountNo,
|
||||
'stAccountType': accountType,
|
||||
'stBookingNumber': bookingNumber,
|
||||
'stBranchId': branchId,
|
||||
'stCurrency': currency,
|
||||
'stProductType': productType,
|
||||
'stApprovedAmount': approvedAmount,
|
||||
'stAvailableBalance': availableBalance,
|
||||
'stCurrentBalance': currentBalance,
|
||||
'custname': name,
|
||||
'mobileno': mobileNo,
|
||||
'custaddress': address,
|
||||
'picode': picode,
|
||||
'transactions': transactions?.map((tx) => tx.toJson()).toList(),
|
||||
'activeAccounts': activeAccounts,
|
||||
'cifNumber': cifNumber,
|
||||
'primaryId': primaryId,
|
||||
'dateOfBirth': dateOfBirth,
|
||||
};
|
||||
}
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
return User(
|
||||
accountNo: json['stAccountNo'],
|
||||
accountType: json['stAccountType'],
|
||||
bookingNumber: json['stBookingNumber'],
|
||||
branchId: json['stBranchId'],
|
||||
branchId: json['stBranchNo'],
|
||||
currency: json['stCurrency'],
|
||||
productType: json['stProductType'],
|
||||
approvedAmount: json['stApprovedAmount'],
|
||||
@@ -47,9 +80,34 @@ class User extends Equatable {
|
||||
mobileNo: json['mobileno'],
|
||||
address: json['custaddress'],
|
||||
picode: json['picode'],
|
||||
cifNumber: json['cifNumber'],
|
||||
primaryId: json['id'],
|
||||
activeAccounts: json['activeAccounts'] as int?,
|
||||
dateOfBirth: json['custdob'],
|
||||
transactions: (json['transactions'] as List<dynamic>?)
|
||||
?.map((tx) => Transaction.fromJson(tx))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
List<Object?> get props => [accountNo, accountType, bookingNumber, branchId, currency, productType, approvedAmount, availableBalance, currentBalance, name, mobileNo, address, picode];
|
||||
List<Object?> get props => [
|
||||
accountNo,
|
||||
accountType,
|
||||
bookingNumber,
|
||||
branchId,
|
||||
currency,
|
||||
productType,
|
||||
approvedAmount,
|
||||
availableBalance,
|
||||
currentBalance,
|
||||
name,
|
||||
mobileNo,
|
||||
address,
|
||||
picode,
|
||||
transactions,
|
||||
activeAccounts,
|
||||
cifNumber,
|
||||
primaryId,
|
||||
];
|
||||
}
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import 'package:kmobile/api/services/user_service.dart';
|
||||
|
||||
import '../../api/services/auth_service.dart';
|
||||
import '../../features/auth/models/auth_token.dart';
|
||||
import '../../features/auth/models/auth_credentials.dart';
|
||||
@@ -6,28 +8,25 @@ import '../../security/secure_storage.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final AuthService _authService;
|
||||
final UserService _userService;
|
||||
final SecureStorage _secureStorage;
|
||||
|
||||
static const _accessTokenKey = 'access_token';
|
||||
static const _refreshTokenKey = 'refresh_token';
|
||||
static const _tokenExpiryKey = 'token_expiry';
|
||||
static const _userKey = 'user_data';
|
||||
|
||||
AuthRepository(this._authService, this._secureStorage);
|
||||
|
||||
Future<User> login(String username, String password) async {
|
||||
|
||||
AuthRepository(this._authService, this._userService, this._secureStorage);
|
||||
|
||||
Future<List<User>> login(String customerNo, String password) async {
|
||||
// Create credentials and call service
|
||||
final credentials = AuthCredentials(username: username, password: password);
|
||||
final credentials = AuthCredentials(customerNo: customerNo, password: password);
|
||||
final authToken = await _authService.login(credentials);
|
||||
|
||||
// Save token securely
|
||||
await _saveAuthToken(authToken);
|
||||
|
||||
// Get and save user profile
|
||||
final user = await _authService.getUserProfile();
|
||||
await _saveUserData(user);
|
||||
|
||||
return user;
|
||||
final users = await _userService.getUserDetails();
|
||||
return users;
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
@@ -35,83 +34,29 @@ class AuthRepository {
|
||||
return token != null && !token.isExpired;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
final token = await _getAuthToken();
|
||||
if (token != null) {
|
||||
try {
|
||||
await _authService.logout(token.refreshToken);
|
||||
} finally {
|
||||
// Clear stored data regardless of logout API success
|
||||
await _clearAuthData();
|
||||
}
|
||||
}
|
||||
}
|
||||
Future<User?> getCurrentUser() async {
|
||||
final userJson = await _secureStorage.read(_userKey);
|
||||
if (userJson != null) {
|
||||
return User.fromJson(userJson);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String?> getAccessToken() async {
|
||||
final token = await _getAuthToken();
|
||||
if (token == null) return null;
|
||||
|
||||
// If token expired, try to refresh it
|
||||
if (token.isExpired) {
|
||||
final newToken = await _refreshToken(token.refreshToken);
|
||||
if (newToken != null) {
|
||||
return newToken.accessToken;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return token.accessToken;
|
||||
}
|
||||
|
||||
// Private helper methods
|
||||
Future<void> _saveAuthToken(AuthToken token) async {
|
||||
await _secureStorage.write(_accessTokenKey, token.accessToken);
|
||||
await _secureStorage.write(_refreshTokenKey, token.refreshToken);
|
||||
await _secureStorage.write(_tokenExpiryKey, token.expiresAt.toIso8601String());
|
||||
}
|
||||
|
||||
Future<AuthToken?> _getAuthToken() async {
|
||||
final accessToken = await _secureStorage.read(_accessTokenKey);
|
||||
final refreshToken = await _secureStorage.read(_refreshTokenKey);
|
||||
final expiryString = await _secureStorage.read(_tokenExpiryKey);
|
||||
|
||||
if (accessToken != null && refreshToken != null && expiryString != null) {
|
||||
|
||||
if (accessToken != null && expiryString != null) {
|
||||
return AuthToken(
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
expiresAt: DateTime.parse(expiryString),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> _saveUserData(User user) async {
|
||||
await _secureStorage.write(_userKey, user);
|
||||
}
|
||||
|
||||
Future<void> _clearAuthData() async {
|
||||
await _secureStorage.delete(_accessTokenKey);
|
||||
await _secureStorage.delete(_refreshTokenKey);
|
||||
await _secureStorage.delete(_tokenExpiryKey);
|
||||
await _secureStorage.delete(_userKey);
|
||||
}
|
||||
|
||||
Future<AuthToken?> _refreshToken(String refreshToken) async {
|
||||
try {
|
||||
final newToken = await _authService.refreshToken(refreshToken);
|
||||
await _saveAuthToken(newToken);
|
||||
return newToken;
|
||||
} catch (e) {
|
||||
// If refresh fails, clear auth data
|
||||
await _clearAuthData();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user