Skip to content

Commit

Permalink
fix: make sure the initialSessionCompleter completes
Browse files Browse the repository at this point in the history
  • Loading branch information
dshukertjr committed Jun 18, 2022
1 parent 7fa0572 commit da06da7
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions lib/src/supabase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,51 @@ class SupabaseAuth with WidgetsBindingObserver {
LocalStorage localStorage = const HiveLocalStorage(),
String? authCallbackUrlHostname,
}) async {
_instance._initialized = true;
_instance._localStorage = localStorage;
_instance._authCallbackUrlHostname = authCallbackUrlHostname;

_instance._authSubscription =
Supabase.instance.client.auth.onAuthStateChange((event, session) {
_instance._onAuthStateChange(event, session);
if (!_instance._listenerController.isClosed) {
_instance._listenerController.add(event);
}
});
try {
_instance._initialized = true;
_instance._localStorage = localStorage;
_instance._authCallbackUrlHostname = authCallbackUrlHostname;

_instance._authSubscription =
Supabase.instance.client.auth.onAuthStateChange((event, session) {
_instance._onAuthStateChange(event, session);
if (!_instance._listenerController.isClosed) {
_instance._listenerController.add(event);
}
});

await _instance._localStorage.initialize();
await _instance._localStorage.initialize();

final hasPersistedSession = await _instance._localStorage.hasAccessToken();
if (hasPersistedSession) {
final persistedSession = await _instance._localStorage.accessToken();
if (persistedSession != null) {
final response = await Supabase.instance.client.auth
.recoverSession(persistedSession);
final hasPersistedSession =
await _instance._localStorage.hasAccessToken();
if (hasPersistedSession) {
final persistedSession = await _instance._localStorage.accessToken();
if (persistedSession != null) {
final response = await Supabase.instance.client.auth
.recoverSession(persistedSession);

if (response.error != null) {
Supabase.instance.log(response.error!.message);
}
if (!_instance._initialSessionCompleter.isCompleted) {
_instance._initialSessionCompleter.complete(response.data);
if (response.error != null) {
Supabase.instance.log(response.error!.message);
}
if (!_instance._initialSessionCompleter.isCompleted) {
_instance._initialSessionCompleter.complete(response.data);
}
}
}
}
WidgetsBinding.instance?.addObserver(_instance);
_instance._startDeeplinkObserver();
WidgetsBinding.instance?.addObserver(_instance);
_instance._startDeeplinkObserver();

if (!_instance._initialSessionCompleter.isCompleted) {
_instance._initialSessionCompleter.complete(null);
if (!_instance._initialSessionCompleter.isCompleted) {
_instance._initialSessionCompleter.complete(null);
}
return _instance;
} catch (_) {
// completer will complete with null if an error occurs
if (!_instance._initialSessionCompleter.isCompleted) {
_instance._initialSessionCompleter.complete(null);
}
rethrow;
}
return _instance;
}

/// Dispose the instance to free up resources
Expand Down

0 comments on commit da06da7

Please sign in to comment.