Skip to content

Commit

Permalink
refactor: fix typo and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Nov 23, 2024
1 parent 0094ec0 commit d62da8f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ part 'gotrue_mfa_api.dart';
/// Required when using the pkce flow and persisting sessions.
///
/// [storageKey] key to store the session with in [asyncStorage].
/// The pkce code verifiers are suffixed with `-code-verifier`
/// The pkce code verifiers are suffixed with `-code-verifier`.
///
/// Set [flowType] to [AuthFlowType.implicit] to perform old implicit auth flow.
/// {@endtemplate}
Expand Down Expand Up @@ -73,7 +73,9 @@ class GoTrueClient {
final _onAuthStateChangeControllerSync =
BehaviorSubject<AuthState>(sync: true);

/// Local storage to store pkce code verifiers.
/// Local storage to store session and pkce code verifiers.
///
/// check [_initializedStorage] before usage.
final GotrueAsyncStorage? _asyncStorage;

/// Receive a notification every time an auth event happens.
Expand All @@ -95,12 +97,18 @@ class GoTrueClient {
Stream<AuthState> get onAuthStateChangeSync =>
_onAuthStateChangeControllerSync.stream;

final Completer<void> _initalizedStorage = Completer<void>();
/// Completes when the [_asyncStorage] is initialized.
///
/// Initialization is started in the constructor and should be awaited before
/// accessing the storage.
final Completer<void> _initializedStorage = Completer<void>();

final AuthFlowType _flowType;

final bool _persistSession;

/// Key to store the session with in [_asyncStorage].
/// The pkce code verifiers are suffixed with `-code-verifier`.
final String _storageKey;

final _log = Logger('supabase.auth');
Expand Down Expand Up @@ -149,7 +157,7 @@ class GoTrueClient {
assert(asyncStorage != null || !_persistSession,
'You need to provide asyncStorage to persist session.');
if (asyncStorage != null) {
_initalizedStorage.complete(
_initializedStorage.complete(
asyncStorage.initialize().catchError((e) => notifyException(e)));
}

Expand Down Expand Up @@ -179,7 +187,7 @@ class GoTrueClient {
Future<void> _initialize() async {
try {
if (_persistSession && _asyncStorage != null) {
await _initalizedStorage.future;
await _initializedStorage.future;
final jsonStr = await _asyncStorage!.getItem(key: _storageKey);
var shouldEmitInitialSession = true;
if (jsonStr != null) {
Expand Down Expand Up @@ -1204,8 +1212,8 @@ class GoTrueClient {
_currentUser = session.user;

if (_persistSession && _asyncStorage != null) {
if (!_initalizedStorage.isCompleted) {
await _initalizedStorage.future;
if (!_initializedStorage.isCompleted) {
await _initializedStorage.future;
}
_asyncStorage!.setItem(
key: _storageKey,
Expand All @@ -1220,12 +1228,10 @@ class GoTrueClient {
_currentUser = null;

if (_persistSession && _asyncStorage != null) {
if (!_initalizedStorage.isCompleted) {
await _initalizedStorage.future;
if (!_initializedStorage.isCompleted) {
await _initializedStorage.future;
}
_asyncStorage!.removeItem(
key: _storageKey,
);
_asyncStorage!.removeItem(key: _storageKey);
}
}

Expand Down

0 comments on commit d62da8f

Please sign in to comment.