@@ -9,7 +9,6 @@ import 'package:notredame/locator.dart';
99class AuthService {
1010 String ? _token;
1111 final int _maxRetry = 3 ;
12- int _retries = 0 ;
1312
1413 final _scopes = ['api://etsmobileapi/access_as_user' ];
1514
@@ -18,21 +17,21 @@ class AuthService {
1817 final Logger _logger = locator <Logger >();
1918
2019 Future <String > getToken () async {
21- if (_token == null ) {
20+ int attempt = 0 ;
21+
22+ while (attempt <= _maxRetry) {
23+ if (_token != null ) return _token! ;
24+
2225 final result = await acquireTokenSilent ();
2326 if (result.$1 != null ) {
24- _token = result.$1? .accessToken;
25- } else {
26- _retries++ ;
27- if (_retries > _maxRetry) {
28- _retries = 0 ;
29- throw Exception ('Max retries reached' );
30- }
31- getToken ();
27+ _token = result.$1! .accessToken;
28+ return _token! ;
3229 }
30+
31+ attempt++ ;
3332 }
34- _retries = 0 ;
35- return _token ! ;
33+
34+ throw Exception ( 'Max retries reached' ) ;
3635 }
3736
3837 Future <(bool , MsalException ?)> createPublicClientApplication ({
@@ -65,7 +64,11 @@ class AuthService {
6564
6665 Future <(AuthenticationResult ?, MsalException ?)> acquireToken ({String ? loginHint}) async {
6766 try {
68- final result = await singleAccountPca? .acquireToken (scopes: _scopes, loginHint: loginHint, prompt: Prompt .login);
67+ final result = await singleAccountPca? .acquireToken (
68+ scopes: _scopes,
69+ loginHint: loginHint,
70+ prompt: Prompt .selectAccount,
71+ );
6972 _token = result? .accessToken;
7073 _logger.d ('Acquire token => ${result ?.toJson ()}' );
7174 return (result, null );
@@ -84,14 +87,30 @@ class AuthService {
8487 } on MsalException catch (e) {
8588 _logger.e ('Acquire token silent failed => $e ' );
8689
87- // If it is a UI required exception, try to acquire token interactively.
8890 if (e is MsalUiRequiredException ) {
89- return acquireToken ();
91+ return await acquireTokenWithCacheReset ();
9092 }
9193 return (null , e);
9294 }
9395 }
9496
97+ Future <(AuthenticationResult ?, MsalException ?)> acquireTokenWithCacheReset () async {
98+ try {
99+ _token = null ;
100+
101+ await signOut ();
102+
103+ final result = await singleAccountPca? .acquireToken (scopes: _scopes, prompt: Prompt .login);
104+
105+ _token = result? .accessToken;
106+ _logger.d ('Token acquired with cache reset => ${result ?.toJson ()}' );
107+ return (result, null );
108+ } on MsalException catch (e) {
109+ _logger.e ('Token acquisition with cache reset failed => $e ' );
110+ return (null , e);
111+ }
112+ }
113+
95114 Future <(bool , MsalException ?)> signOut () async {
96115 try {
97116 _token = null ;
0 commit comments