Skip to content

Commit

Permalink
breaking: removed all supabase classes and consolidate all auth relat…
Browse files Browse the repository at this point in the history
…ed logic to SupabaseAuth
  • Loading branch information
dshukertjr committed Jun 15, 2022
1 parent 5a25f2f commit bafed0d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 152 deletions.
16 changes: 7 additions & 9 deletions lib/src/supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ class Supabase {
!_instance._initialized,
'This instance is already initialized',
);
if (url != null && anonKey != null) {
_instance._init(url, anonKey);
_instance._debugEnable = debug ?? kDebugMode;
_instance.log('***** Supabase init completed $_instance');
_instance._init(url, anonKey);
_instance._debugEnable = debug ?? kDebugMode;
_instance.log('***** Supabase init completed $_instance');

await SupabaseAuth.initialize(
localStorage: localStorage ?? const HiveLocalStorage(),
authCallbackUrlHostname: authCallbackUrlHostname,
);
}
await SupabaseAuth.initialize(
localStorage: localStorage ?? const HiveLocalStorage(),
authCallbackUrlHostname: authCallbackUrlHostname,
);

return _instance;
}
Expand Down
88 changes: 88 additions & 0 deletions lib/src/supabase_auth.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:uni_links/uni_links.dart';

import 'package:url_launcher/url_launcher.dart';

Expand Down Expand Up @@ -29,6 +32,8 @@ class SupabaseAuth with WidgetsBindingObserver {
GotrueSubscription? _authSubscription;
final _listenerController = StreamController<AuthChangeEvent>.broadcast();

StreamSubscription<Uri?>? _deeplinkSubscription;

/// Listen to auth change events.
///
/// ```dart
Expand Down Expand Up @@ -98,6 +103,7 @@ class SupabaseAuth with WidgetsBindingObserver {
void dispose() {
_listenerController.close();
_authSubscription?.data?.unsubscribe();
stopDeeplinkObserver();
WidgetsBinding.instance.removeObserver(this);
}

Expand Down Expand Up @@ -194,6 +200,88 @@ class SupabaseAuth with WidgetsBindingObserver {
return _authCallbackUrlHostname == uri.host;
}
}

/// Enable deep link observer to handle deep links
void startDeeplinkObserver() {
Supabase.instance.log('***** SupabaseDeepLinkingMixin startAuthObserver');
_handleIncomingLinks();
_handleInitialUri();
}

/// Stop deep link observer
///
/// Automatically called on dispose().
void stopDeeplinkObserver() {
Supabase.instance.log('***** SupabaseDeepLinkingMixin stopAuthObserver');
if (_deeplinkSubscription != null) _deeplinkSubscription?.cancel();
}

/// Handle incoming links - the ones that the app will recieve from the OS
/// while already started.
void _handleIncomingLinks() {
if (!kIsWeb) {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
_deeplinkSubscription = uriLinkStream.listen(
(Uri? uri) {
if (uri != null) {
_handleDeeplink(uri);
}
},
onError: (Object err) {
_onErrorReceivingDeeplink(err.toString());
},
);
}
}

/// Handle the initial Uri - the one the app was started with
///
/// **ATTENTION**: `getInitialLink`/`getInitialUri` should be handled
/// ONLY ONCE in your app's lifetime, since it is not meant to change
/// throughout your app's life.
///
/// We handle all exceptions, since it is called from initState.
Future<void> _handleInitialUri() async {
if (!SupabaseAuth.instance.shouldHandleInitialDeeplink()) return;

try {
final uri = await getInitialUri();
if (uri != null) {
_handleDeeplink(uri);
}
} on PlatformException {
// Platform messages may fail but we ignore the exception
} on FormatException catch (err) {
_onErrorReceivingDeeplink(err.message);
}
}

/// Callback when deeplink receiving succeeds
Future<bool> _handleDeeplink(Uri uri) async {
if (!SupabaseAuth.instance.isAuthCallbackDeeplink(uri)) return false;

Supabase.instance.log('***** SupabaseAuthState handleDeeplink $uri');

// notify auth deeplink received
Supabase.instance.log('onReceivedAuthDeeplink uri: $uri');

return _recoverSessionFromUrl(uri);
}

Future<bool> _recoverSessionFromUrl(Uri uri) async {
// recover session from deeplink
final response = await Supabase.instance.client.auth.getSessionFromUrl(uri);
if (response.error != null) {
Supabase.instance.log(response.error!.message);
}
return true;
}

/// Callback when deeplink receiving throw error
void _onErrorReceivingDeeplink(String message) {
Supabase.instance.log('onErrorReceivingDeppLink message: $message');
}
}

extension GoTrueClientSignInProvider on GoTrueClient {
Expand Down
70 changes: 0 additions & 70 deletions lib/src/supabase_auth_state.dart

This file was deleted.

72 changes: 0 additions & 72 deletions lib/src/supabase_deep_linking_mixin.dart

This file was deleted.

1 change: 0 additions & 1 deletion lib/supabase_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export 'package:supabase/supabase.dart';
export 'src/local_storage.dart';
export 'src/supabase.dart';
export 'src/supabase_auth.dart';
export 'src/supabase_auth_state.dart';

0 comments on commit bafed0d

Please sign in to comment.