Language Changes
This commit is contained in:
62
lib/data/repositories/auth_repository.dart
Normal file
62
lib/data/repositories/auth_repository.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
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';
|
||||
import '../../data/models/user.dart';
|
||||
import '../../security/secure_storage.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final AuthService _authService;
|
||||
final UserService _userService;
|
||||
final SecureStorage _secureStorage;
|
||||
|
||||
static const _accessTokenKey = 'access_token';
|
||||
static const _tokenExpiryKey = 'token_expiry';
|
||||
|
||||
AuthRepository(this._authService, this._userService, this._secureStorage);
|
||||
|
||||
Future<List<User>> login(String customerNo, String password) async {
|
||||
// Create credentials and call service
|
||||
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 users = await _userService.getUserDetails();
|
||||
return users;
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
final token = await _getAuthToken();
|
||||
return token != null && !token.isExpired;
|
||||
}
|
||||
|
||||
Future<String?> getAccessToken() async {
|
||||
final token = await _getAuthToken();
|
||||
if (token == null) return null;
|
||||
|
||||
return token.accessToken;
|
||||
}
|
||||
|
||||
// Private helper methods
|
||||
Future<void> _saveAuthToken(AuthToken token) async {
|
||||
await _secureStorage.write(_accessTokenKey, token.accessToken);
|
||||
await _secureStorage.write(_tokenExpiryKey, token.expiresAt.toIso8601String());
|
||||
}
|
||||
|
||||
Future<AuthToken?> _getAuthToken() async {
|
||||
final accessToken = await _secureStorage.read(_accessTokenKey);
|
||||
final expiryString = await _secureStorage.read(_tokenExpiryKey);
|
||||
|
||||
if (accessToken != null && expiryString != null) {
|
||||
return AuthToken(
|
||||
accessToken: accessToken,
|
||||
expiresAt: DateTime.parse(expiryString),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user