supabase_task.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:appflowy/env/env.dart';
  4. import 'package:appflowy/user/application/supabase_realtime.dart';
  5. import 'package:appflowy/workspace/application/settings/application_data_storage.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:supabase_flutter/supabase_flutter.dart';
  8. import 'package:url_protocol/url_protocol.dart';
  9. import 'package:hive_flutter/hive_flutter.dart';
  10. import 'package:path/path.dart' as p;
  11. import '../startup.dart';
  12. // ONLY supports in macOS and Windows now.
  13. //
  14. // If you need to update the schema, please update the following files:
  15. // - appflowy_flutter/macos/Runner/Info.plist (macOS)
  16. // - the callback url in Supabase dashboard
  17. const appflowyDeepLinkSchema = 'appflowy-flutter';
  18. const supabaseLoginCallback = '$appflowyDeepLinkSchema://login-callback';
  19. const hiveBoxName = 'appflowy_supabase_authentication';
  20. // Used to store the session of the supabase in case of the user switch the different folder.
  21. Supabase? supabase;
  22. SupbaseRealtimeService? realtimeService;
  23. class InitSupabaseTask extends LaunchTask {
  24. @override
  25. Future<void> initialize(LaunchContext context) async {
  26. if (!isSupabaseEnabled) {
  27. return;
  28. }
  29. supabase?.dispose();
  30. supabase = null;
  31. final initializedSupabase = await Supabase.initialize(
  32. url: Env.supabaseUrl,
  33. anonKey: Env.supabaseAnonKey,
  34. debug: kDebugMode,
  35. localStorage: const SupabaseLocalStorage(),
  36. );
  37. if (realtimeService != null) {
  38. await realtimeService?.dispose();
  39. realtimeService = null;
  40. }
  41. realtimeService = SupbaseRealtimeService(supabase: initializedSupabase);
  42. supabase = initializedSupabase;
  43. if (Platform.isWindows) {
  44. // register deep link for Windows
  45. registerProtocolHandler(appflowyDeepLinkSchema);
  46. } else if (Platform.isLinux) {
  47. // register deep link for Linux
  48. await SupabaseAuth.instance.registerDBusService(
  49. // these values should be compatible with the values in the desktop file
  50. // dbus-interface.xml
  51. '/io/appflowy/AppFlowy/Object',
  52. 'io.appflowy.AppFlowy',
  53. );
  54. }
  55. }
  56. }
  57. /// customize the supabase auth storage
  58. ///
  59. /// We don't use the default one because it always save the session in the document directory.
  60. /// When we switch to the different folder, the session still exists.
  61. class SupabaseLocalStorage extends LocalStorage {
  62. const SupabaseLocalStorage()
  63. : super(
  64. initialize: _initialize,
  65. hasAccessToken: _hasAccessToken,
  66. accessToken: _accessToken,
  67. removePersistedSession: _removePersistedSession,
  68. persistSession: _persistSession,
  69. );
  70. static Future<void> _initialize() async {
  71. HiveCipher? encryptionCipher;
  72. // customize the path for Hive
  73. final path = await getIt<ApplicationDataStorage>().getPath();
  74. Hive.init(p.join(path, 'supabase_auth'));
  75. await Hive.openBox(
  76. hiveBoxName,
  77. encryptionCipher: encryptionCipher,
  78. );
  79. }
  80. static Future<bool> _hasAccessToken() {
  81. return Future.value(
  82. Hive.box(hiveBoxName).containsKey(
  83. supabasePersistSessionKey,
  84. ),
  85. );
  86. }
  87. static Future<String?> _accessToken() {
  88. return Future.value(
  89. Hive.box(hiveBoxName).get(supabasePersistSessionKey) as String?,
  90. );
  91. }
  92. static Future<void> _removePersistedSession() {
  93. return Hive.box(hiveBoxName).delete(supabasePersistSessionKey);
  94. }
  95. static Future<void> _persistSession(String persistSessionString) {
  96. return Hive.box(hiveBoxName).put(
  97. supabasePersistSessionKey,
  98. persistSessionString,
  99. );
  100. }
  101. }