authenticate method
Authenticates (from the cache when it's still valid, verifying in the background). Throws when it fails.
Implementation
@override
Future<ProjectAccess> authenticate() async {
// On the web Firebase restores the signed-in user (an app user, or the
// guest's or SDK's anonymous one) after the app starts: wait for it,
// or every page load signs in a new anonymous user (a guest loses their
// session, and Firebase soon blocks the device: too-many-requests).
if (auth.currentUser == null) {
try {
await auth.userChanges().first.timeout(restoreTimeout, onTimeout: () => null);
} on StateError {
// No event at all (a closed stream): nobody to restore.
}
}
if (auth.currentUser == null) await auth.signInAnonymously();
_idToken = await auth.currentUser!.getIdTokenResult(true);
final cached = await readCached(cache, cacheKey, ProjectAccess.fromJson);
if (cached != null && cached.authToken == config.authToken && hasProjectClaim(_idToken, cached.projectId)) {
_set(cached);
unawaited(_verify().then<void>((_) {}, onError: (Object e, StackTrace s) => onError?.call(e, s)));
return cached;
}
if (cached != null) await cache.delete(cacheKey);
final access = await _verify();
if (access == null) throw StateError('Failed to authenticate the project token.');
return access;
}