From d632850b50744260601bcd1fd126707452a13143 Mon Sep 17 00:00:00 2001 From: tommyxchow Date: Sun, 17 Oct 2021 16:26:02 -0400 Subject: [PATCH] Fix computed headers not updating, improve logout --- lib/stores/auth_store.dart | 28 ++++++++++++++++++++++------ lib/stores/auth_store.g.dart | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/stores/auth_store.dart b/lib/stores/auth_store.dart index 3d00cf60..37bce33a 100644 --- a/lib/stores/auth_store.dart +++ b/lib/stores/auth_store.dart @@ -14,13 +14,13 @@ abstract class _AuthBase with Store { /// Secure storage to store tokens. final _storage = const FlutterSecureStorage(); - /// The current token. - String? _token; - String? get token => _token; - /// Whether the token is valid or not. var _tokenIsValid = false; + /// The current token. + @readonly + String? _token; + /// The current user's info. @readonly UserTwitch? _user; @@ -115,8 +115,24 @@ abstract class _AuthBase with Store { // Set the login status to logged out. _isLoggedIn = false; - // If the default token already exists, set it. Otherwise, get a new default token. - _token = await _storage.read(key: 'DEFAULT_TOKEN') ?? await Twitch.getDefaultToken(); + // If the default token already exists, set it. + _token = await _storage.read(key: 'DEFAULT_TOKEN'); + + // If the default token does not already exist, get the new default token and store it. + // Else, validate the existing token. + if (_token == null) { + await Twitch.getDefaultToken(); + await _storage.write(key: 'DEFAULT_TOKEN', value: _token); + } else { + // Validate the stored token. + _tokenIsValid = await Twitch.validateToken(token: _token!); + + // If the stored token is invalid, get a new default token and store it. + if (!_tokenIsValid) { + await Twitch.getDefaultToken(); + await _storage.write(key: 'DEFAULT_TOKEN', value: _token); + } + } debugPrint('Successfully logged out'); } diff --git a/lib/stores/auth_store.g.dart b/lib/stores/auth_store.g.dart index ab7e92ff..d24d9477 100644 --- a/lib/stores/auth_store.g.dart +++ b/lib/stores/auth_store.g.dart @@ -17,6 +17,23 @@ mixin _$AuthStore on _AuthBase, Store { name: '_AuthBase.headersTwitch')) .value; + final _$_tokenAtom = Atom(name: '_AuthBase._token'); + + String? get token { + _$_tokenAtom.reportRead(); + return super._token; + } + + @override + String? get _token => token; + + @override + set _token(String? value) { + _$_tokenAtom.reportWrite(value, super._token, () { + super._token = value; + }); + } + final _$_userAtom = Atom(name: '_AuthBase._user'); UserTwitch? get user {