appflowy_auth_service.dart 3.0 KB

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