Skip to content

Commit

Permalink
Use cached token instead of fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Feb 3, 2024
1 parent a0529af commit 726cdbe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/base/networking/api/handler/token_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,4 @@ class TokenHandler {
cachedToken = '';
}


}
4 changes: 3 additions & 1 deletion lib/view_models/user_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import 'package:logger/logger.dart';

class UserViewModel extends StateNotifier<UserState> {
final Logger _logger = Logger();
bool _isTokenChecked = false; // Flag to track token check

final GrpcHandler _grpcHandler;

UserViewModel(this._grpcHandler) : super(const UserState()){
// Check if the user is already logged in
_checkToken();
}

Expand Down Expand Up @@ -157,13 +157,15 @@ class UserViewModel extends StateNotifier<UserState> {
}

Future<void> _checkToken() async {
if (_isTokenChecked) return;
String token = await _getToken();
if(token.isNotEmpty && !Jwt.isExpired(token)) {
_logger.i('Token found, fetching user: $token');
fetchUser();
}else {
_logger.i('Token not found or expired');
}
_isTokenChecked = true;
}

Future<String> _getToken() async {
Expand Down

0 comments on commit 726cdbe

Please sign in to comment.