supabase_task.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  47. }
  48. }
  49. /// customize the supabase auth storage
  50. ///
  51. /// We don't use the default one because it always save the session in the document directory.
  52. /// When we switch to the different folder, the session still exists.
  53. class SupabaseLocalStorage extends LocalStorage {
  54. const SupabaseLocalStorage()
  55. : super(
  56. initialize: _initialize,
  57. hasAccessToken: _hasAccessToken,
  58. accessToken: _accessToken,
  59. removePersistedSession: _removePersistedSession,
  60. persistSession: _persistSession,
  61. );
  62. static Future<void> _initialize() async {
  63. HiveCipher? encryptionCipher;
  64. // customize the path for Hive
  65. final path = await getIt<ApplicationDataStorage>().getPath();
  66. Hive.init(p.join(path, 'supabase_auth'));
  67. await Hive.openBox(
  68. hiveBoxName,
  69. encryptionCipher: encryptionCipher,
  70. );
  71. }
  72. static Future<bool> _hasAccessToken() {
  73. return Future.value(
  74. Hive.box(hiveBoxName).containsKey(
  75. supabasePersistSessionKey,
  76. ),
  77. );
  78. }
  79. static Future<String?> _accessToken() {
  80. return Future.value(
  81. Hive.box(hiveBoxName).get(supabasePersistSessionKey) as String?,
  82. );
  83. }
  84. static Future<void> _removePersistedSession() {
  85. return Hive.box(hiveBoxName).delete(supabasePersistSessionKey);
  86. }
  87. static Future<void> _persistSession(String persistSessionString) {
  88. return Hive.box(hiveBoxName).put(
  89. supabasePersistSessionKey,
  90. persistSessionString,
  91. );
  92. }
  93. }