mock_auth_service.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:async';
  2. import 'package:appflowy/user/application/auth/appflowy_auth_service.dart';
  3. import 'package:appflowy/user/application/auth/auth_service.dart';
  4. import 'package:appflowy/user/application/user_service.dart';
  5. import 'package:appflowy_backend/dispatch/dispatch.dart';
  6. import 'package:appflowy_backend/log.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  8. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  9. import 'package:dartz/dartz.dart';
  10. import 'package:nanoid/nanoid.dart';
  11. import 'package:supabase_flutter/supabase_flutter.dart';
  12. import 'auth_error.dart';
  13. /// Only used for testing.
  14. class MockAuthService implements AuthService {
  15. MockAuthService();
  16. SupabaseClient get _client => Supabase.instance.client;
  17. GoTrueClient get _auth => _client.auth;
  18. final AppFlowyAuthService _appFlowyAuthService = AppFlowyAuthService();
  19. @override
  20. Future<Either<FlowyError, UserProfilePB>> signUp({
  21. required String name,
  22. required String email,
  23. required String password,
  24. AuthTypePB authType = AuthTypePB.Supabase,
  25. Map<String, String> params = const {},
  26. }) async {
  27. throw UnimplementedError();
  28. }
  29. @override
  30. Future<Either<FlowyError, UserProfilePB>> signIn({
  31. required String email,
  32. required String password,
  33. AuthTypePB authType = AuthTypePB.Supabase,
  34. Map<String, String> params = const {},
  35. }) async {
  36. throw UnimplementedError();
  37. }
  38. @override
  39. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  40. required String platform,
  41. AuthTypePB authType = AuthTypePB.Supabase,
  42. Map<String, String> params = const {},
  43. }) async {
  44. try {
  45. final response = await _auth.signUp(
  46. email: "${nanoid(10)}@appflowy.io",
  47. password: "AppFlowyTest123!",
  48. );
  49. final uuid = response.user!.id;
  50. final email = response.user!.email!;
  51. final payload = ThirdPartyAuthPB(
  52. authType: AuthTypePB.Supabase,
  53. map: {
  54. AuthServiceMapKeys.uuid: uuid,
  55. AuthServiceMapKeys.email: email,
  56. AuthServiceMapKeys.deviceId: 'MockDeviceId'
  57. },
  58. );
  59. return UserEventThirdPartyAuth(payload)
  60. .send()
  61. .then((value) => value.swap());
  62. } on AuthException catch (e) {
  63. Log.error(e);
  64. return Left(AuthError.supabaseSignInError);
  65. }
  66. }
  67. @override
  68. Future<void> signOut({
  69. AuthTypePB authType = AuthTypePB.Supabase,
  70. }) async {
  71. await _auth.signOut();
  72. await _appFlowyAuthService.signOut(
  73. authType: authType,
  74. );
  75. }
  76. @override
  77. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  78. AuthTypePB authType = AuthTypePB.Supabase,
  79. Map<String, String> params = const {},
  80. }) async {
  81. // supabase don't support guest login.
  82. // so, just forward to our backend.
  83. return _appFlowyAuthService.signUpAsGuest();
  84. }
  85. @override
  86. Future<Either<FlowyError, UserProfilePB>> signInWithMagicLink({
  87. required String email,
  88. Map<String, String> params = const {},
  89. }) async {
  90. throw UnimplementedError();
  91. }
  92. @override
  93. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  94. return UserBackendService.getCurrentUserProfile();
  95. }
  96. }