supabase_task.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. realtimeService = SupbaseRealtimeService(supabase: initializedSupabase);
  38. supabase = initializedSupabase;
  39. if (Platform.isWindows) {
  40. // register deep link for Windows
  41. registerProtocolHandler(appflowyDeepLinkSchema);
  42. } else if (Platform.isLinux) {
  43. // register deep link for Linux
  44. await SupabaseAuth.instance.registerDBusService(
  45. // these values should be compatible with the values in the desktop file
  46. // dbus-interface.xml
  47. '/io/appflowy/AppFlowy/Object',
  48. 'io.appflowy.AppFlowy',
  49. );
  50. }
  51. }
  52. }
  53. /// customize the supabase auth storage
  54. ///
  55. /// We don't use the default one because it always save the session in the document directory.
  56. /// When we switch to the different folder, the session still exists.
  57. class SupabaseLocalStorage extends LocalStorage {
  58. const SupabaseLocalStorage()
  59. : super(
  60. initialize: _initialize,
  61. hasAccessToken: _hasAccessToken,
  62. accessToken: _accessToken,
  63. removePersistedSession: _removePersistedSession,
  64. persistSession: _persistSession,
  65. );
  66. static Future<void> _initialize() async {
  67. HiveCipher? encryptionCipher;
  68. // customize the path for Hive
  69. final path = await getIt<ApplicationDataStorage>().getPath();
  70. Hive.init(p.join(path, 'supabase_auth'));
  71. await Hive.openBox(
  72. hiveBoxName,
  73. encryptionCipher: encryptionCipher,
  74. );
  75. }
  76. static Future<bool> _hasAccessToken() {
  77. return Future.value(
  78. Hive.box(hiveBoxName).containsKey(
  79. supabasePersistSessionKey,
  80. ),
  81. );
  82. }
  83. static Future<String?> _accessToken() {
  84. return Future.value(
  85. Hive.box(hiveBoxName).get(supabasePersistSessionKey) as String?,
  86. );
  87. }
  88. static Future<void> _removePersistedSession() {
  89. return Hive.box(hiveBoxName).delete(supabasePersistSessionKey);
  90. }
  91. static Future<void> _persistSession(String persistSessionString) {
  92. return Hive.box(hiveBoxName).put(
  93. supabasePersistSessionKey,
  94. persistSessionString,
  95. );
  96. }
  97. }