supabase_auth_service.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import 'dart:async';
  2. import 'package:appflowy/env/env.dart';
  3. import 'package:appflowy/user/application/auth/appflowy_auth_service.dart';
  4. import 'package:appflowy/user/application/auth/auth_service.dart';
  5. import 'package:appflowy/user/application/user_service.dart';
  6. import 'package:appflowy_backend/dispatch/dispatch.dart';
  7. import 'package:appflowy_backend/log.dart';
  8. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  9. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  10. import 'package:dartz/dartz.dart';
  11. import 'package:flutter/foundation.dart';
  12. import 'package:supabase_flutter/supabase_flutter.dart';
  13. import 'auth_error.dart';
  14. // can't use underscore here.
  15. const loginCallback = 'io.appflowy.appflowy-flutter://login-callback';
  16. class SupabaseAuthService implements AuthService {
  17. SupabaseAuthService();
  18. SupabaseClient get _client => Supabase.instance.client;
  19. GoTrueClient get _auth => _client.auth;
  20. final AppFlowyAuthService _appFlowyAuthService = AppFlowyAuthService();
  21. @override
  22. Future<Either<FlowyError, UserProfilePB>> signUp({
  23. required String name,
  24. required String email,
  25. required String password,
  26. AuthTypePB authType = AuthTypePB.Supabase,
  27. Map<String, String> map = const {},
  28. }) async {
  29. if (!isSupabaseEnabled) {
  30. return _appFlowyAuthService.signUp(
  31. name: name,
  32. email: email,
  33. password: password,
  34. );
  35. }
  36. // fetch the uuid from supabase.
  37. final response = await _auth.signUp(
  38. email: email,
  39. password: password,
  40. );
  41. final uuid = response.user?.id;
  42. if (uuid == null) {
  43. return left(AuthError.supabaseSignUpError);
  44. }
  45. // assign the uuid to our backend service.
  46. // and will transfer this logic to backend later.
  47. return _appFlowyAuthService.signUp(
  48. name: name,
  49. email: email,
  50. password: password,
  51. authType: authType,
  52. map: {
  53. AuthServiceMapKeys.uuid: uuid,
  54. },
  55. );
  56. }
  57. @override
  58. Future<Either<FlowyError, UserProfilePB>> signIn({
  59. required String email,
  60. required String password,
  61. AuthTypePB authType = AuthTypePB.Supabase,
  62. Map<String, String> map = const {},
  63. }) async {
  64. if (!isSupabaseEnabled) {
  65. return _appFlowyAuthService.signIn(
  66. email: email,
  67. password: password,
  68. );
  69. }
  70. try {
  71. final response = await _auth.signInWithPassword(
  72. email: email,
  73. password: password,
  74. );
  75. final uuid = response.user?.id;
  76. if (uuid == null) {
  77. return Left(AuthError.supabaseSignInError);
  78. }
  79. return _appFlowyAuthService.signIn(
  80. email: email,
  81. password: password,
  82. authType: authType,
  83. map: {
  84. AuthServiceMapKeys.uuid: uuid,
  85. },
  86. );
  87. } on AuthException catch (e) {
  88. Log.error(e);
  89. return Left(AuthError.supabaseSignInError);
  90. }
  91. }
  92. @override
  93. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  94. required String platform,
  95. AuthTypePB authType = AuthTypePB.Supabase,
  96. Map<String, String> map = const {},
  97. }) async {
  98. if (!isSupabaseEnabled) {
  99. return _appFlowyAuthService.signUpWithOAuth(platform: platform);
  100. }
  101. final provider = platform.toProvider();
  102. final completer = supabaseLoginCompleter(
  103. onSuccess: (userId, userEmail) async {
  104. return await setupAuth(
  105. map: {
  106. AuthServiceMapKeys.uuid: userId,
  107. AuthServiceMapKeys.email: userEmail
  108. },
  109. );
  110. },
  111. );
  112. final response = await _auth.signInWithOAuth(
  113. provider,
  114. queryParams: queryParamsForProvider(provider),
  115. redirectTo: loginCallback,
  116. );
  117. if (!response) {
  118. completer.complete(left(AuthError.supabaseSignInWithOauthError));
  119. }
  120. return completer.future;
  121. }
  122. @override
  123. Future<void> signOut({
  124. AuthTypePB authType = AuthTypePB.Supabase,
  125. }) async {
  126. if (isSupabaseEnabled) {
  127. await _auth.signOut();
  128. }
  129. await _appFlowyAuthService.signOut(
  130. authType: authType,
  131. );
  132. }
  133. @override
  134. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  135. AuthTypePB authType = AuthTypePB.Supabase,
  136. Map<String, String> map = const {},
  137. }) async {
  138. // supabase don't support guest login.
  139. // so, just forward to our backend.
  140. return _appFlowyAuthService.signUpAsGuest();
  141. }
  142. @override
  143. Future<Either<FlowyError, UserProfilePB>> signInWithMagicLink({
  144. required String email,
  145. Map<String, String> map = const {},
  146. }) async {
  147. final completer = supabaseLoginCompleter(
  148. onSuccess: (userId, userEmail) async {
  149. return await setupAuth(
  150. map: {
  151. AuthServiceMapKeys.uuid: userId,
  152. AuthServiceMapKeys.email: userEmail
  153. },
  154. );
  155. },
  156. );
  157. await _auth.signInWithOtp(
  158. email: email,
  159. emailRedirectTo: kIsWeb ? null : loginCallback,
  160. );
  161. return completer.future;
  162. }
  163. @override
  164. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  165. return UserBackendService.getCurrentUserProfile();
  166. }
  167. Future<Either<FlowyError, User>> getSupabaseUser() async {
  168. final user = _auth.currentUser;
  169. if (user == null) {
  170. return left(AuthError.supabaseGetUserError);
  171. }
  172. return Right(user);
  173. }
  174. Future<Either<FlowyError, UserProfilePB>> setupAuth({
  175. required Map<String, String> map,
  176. }) async {
  177. final payload = ThirdPartyAuthPB(
  178. authType: AuthTypePB.Supabase,
  179. map: map,
  180. );
  181. return UserEventThirdPartyAuth(payload)
  182. .send()
  183. .then((value) => value.swap());
  184. }
  185. }
  186. extension on String {
  187. Provider toProvider() {
  188. switch (this) {
  189. case 'github':
  190. return Provider.github;
  191. case 'google':
  192. return Provider.google;
  193. case 'discord':
  194. return Provider.discord;
  195. default:
  196. throw UnimplementedError();
  197. }
  198. }
  199. }
  200. Completer<Either<FlowyError, UserProfilePB>> supabaseLoginCompleter({
  201. required Future<Either<FlowyError, UserProfilePB>> Function(
  202. String userId,
  203. String userEmail,
  204. ) onSuccess,
  205. }) {
  206. final completer = Completer<Either<FlowyError, UserProfilePB>>();
  207. late final StreamSubscription<AuthState> subscription;
  208. final auth = Supabase.instance.client.auth;
  209. subscription = auth.onAuthStateChange.listen((event) async {
  210. final user = event.session?.user;
  211. if (event.event != AuthChangeEvent.signedIn || user == null) {
  212. completer.complete(left(AuthError.supabaseSignInWithOauthError));
  213. } else {
  214. final response = await onSuccess(
  215. user.id,
  216. user.email ?? user.newEmail ?? '',
  217. );
  218. completer.complete(response);
  219. }
  220. subscription.cancel();
  221. });
  222. return completer;
  223. }
  224. Map<String, String> queryParamsForProvider(Provider provider) {
  225. switch (provider) {
  226. case Provider.github:
  227. return {};
  228. case Provider.google:
  229. return {
  230. 'access_type': 'offline',
  231. 'prompt': 'consent',
  232. };
  233. case Provider.discord:
  234. return {};
  235. default:
  236. return {};
  237. }
  238. }