From 726cdbe3a5930947c858e231750de90a729cddc1 Mon Sep 17 00:00:00 2001 From: Achraf Labidi Date: Sat, 3 Feb 2024 20:37:50 +0100 Subject: [PATCH] Use cached token instead of fetching --- lib/base/networking/api/handler/token_handler.dart | 1 - lib/view_models/user_view_model.dart | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/base/networking/api/handler/token_handler.dart b/lib/base/networking/api/handler/token_handler.dart index 1499e45..085735f 100644 --- a/lib/base/networking/api/handler/token_handler.dart +++ b/lib/base/networking/api/handler/token_handler.dart @@ -126,5 +126,4 @@ class TokenHandler { cachedToken = ''; } - } diff --git a/lib/view_models/user_view_model.dart b/lib/view_models/user_view_model.dart index dac2bb4..2fbe6f0 100644 --- a/lib/view_models/user_view_model.dart +++ b/lib/view_models/user_view_model.dart @@ -17,11 +17,11 @@ import 'package:logger/logger.dart'; class UserViewModel extends StateNotifier { 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(); } @@ -157,6 +157,7 @@ class UserViewModel extends StateNotifier { } Future _checkToken() async { + if (_isTokenChecked) return; String token = await _getToken(); if(token.isNotEmpty && !Jwt.isExpired(token)) { _logger.i('Token found, fetching user: $token'); @@ -164,6 +165,7 @@ class UserViewModel extends StateNotifier { }else { _logger.i('Token not found or expired'); } + _isTokenChecked = true; } Future _getToken() async {