backend_auth_service.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:appflowy/user/application/auth/auth_service.dart';
  2. import 'package:appflowy/user/application/user_service.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-error/code.pbenum.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-user/auth.pb.dart';
  5. import 'package:dartz/dartz.dart';
  6. import 'package:easy_localization/easy_localization.dart';
  7. import 'package:flowy_infra/uuid.dart';
  8. import 'package:appflowy_backend/dispatch/dispatch.dart';
  9. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  10. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
  11. show SignInPayloadPB, SignUpPayloadPB, UserProfilePB;
  12. import '../../../generated/locale_keys.g.dart';
  13. import 'device_id.dart';
  14. class BackendAuthService implements AuthService {
  15. final AuthTypePB authType;
  16. BackendAuthService(this.authType);
  17. @override
  18. Future<Either<FlowyError, UserProfilePB>> signIn({
  19. required String email,
  20. required String password,
  21. Map<String, String> params = const {},
  22. }) async {
  23. final request = SignInPayloadPB.create()
  24. ..email = email
  25. ..password = password
  26. ..authType = authType
  27. ..deviceId = await getDeviceId();
  28. final response = UserEventSignIn(request).send();
  29. return response.then((value) => value.swap());
  30. }
  31. @override
  32. Future<Either<FlowyError, UserProfilePB>> signUp({
  33. required String name,
  34. required String email,
  35. required String password,
  36. Map<String, String> params = const {},
  37. }) async {
  38. final request = SignUpPayloadPB.create()
  39. ..name = name
  40. ..email = email
  41. ..password = password
  42. ..authType = authType
  43. ..deviceId = await getDeviceId();
  44. final response = await UserEventSignUp(request).send().then(
  45. (value) => value.swap(),
  46. );
  47. return response;
  48. }
  49. @override
  50. Future<void> signOut({
  51. Map<String, String> params = const {},
  52. }) async {
  53. await UserEventSignOut().send();
  54. return;
  55. }
  56. @override
  57. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  58. Map<String, String> params = const {},
  59. }) {
  60. const password = "Guest!@123456";
  61. final uid = uuid();
  62. final userEmail = "[email protected]";
  63. return signUp(
  64. name: LocaleKeys.defaultUsername.tr(),
  65. password: password,
  66. email: userEmail,
  67. );
  68. }
  69. @override
  70. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  71. required String platform,
  72. AuthTypePB authType = AuthTypePB.Local,
  73. Map<String, String> params = const {},
  74. }) async {
  75. return left(
  76. FlowyError.create()
  77. ..code = ErrorCode.Internal
  78. ..msg = "Unsupported sign up action",
  79. );
  80. }
  81. @override
  82. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  83. return UserBackendService.getCurrentUserProfile();
  84. }
  85. @override
  86. Future<Either<FlowyError, UserProfilePB>> signInWithMagicLink({
  87. required String email,
  88. Map<String, String> params = const {},
  89. }) async {
  90. return left(
  91. FlowyError.create()
  92. ..code = ErrorCode.Internal
  93. ..msg = "Unsupported sign up action",
  94. );
  95. }
  96. }