appflowy_auth_service.dart 3.1 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 AppFlowyAuthService implements AuthService {
  15. @override
  16. Future<Either<FlowyError, UserProfilePB>> signIn({
  17. required String email,
  18. required String password,
  19. AuthTypePB authType = AuthTypePB.Local,
  20. Map<String, String> map = const {},
  21. }) async {
  22. final request = SignInPayloadPB.create()
  23. ..email = email
  24. ..password = password
  25. ..authType = authType
  26. ..deviceId = await getDeviceId();
  27. final response = UserEventSignIn(request).send();
  28. return response.then((value) => value.swap());
  29. }
  30. @override
  31. Future<Either<FlowyError, UserProfilePB>> signUp({
  32. required String name,
  33. required String email,
  34. required String password,
  35. AuthTypePB authType = AuthTypePB.Local,
  36. Map<String, String> map = 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. AuthTypePB authType = AuthTypePB.Local,
  52. Map<String, String> map = const {},
  53. }) async {
  54. await UserEventSignOut().send();
  55. return;
  56. }
  57. @override
  58. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  59. AuthTypePB authType = AuthTypePB.Local,
  60. Map<String, String> map = const {},
  61. }) {
  62. const password = "Guest!@123456";
  63. final uid = uuid();
  64. final userEmail = "[email protected]";
  65. return signUp(
  66. name: LocaleKeys.defaultUsername.tr(),
  67. password: password,
  68. email: userEmail,
  69. );
  70. }
  71. @override
  72. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  73. required String platform,
  74. AuthTypePB authType = AuthTypePB.Local,
  75. Map<String, String> map = const {},
  76. }) async {
  77. return left(
  78. FlowyError.create()
  79. ..code = ErrorCode.Internal
  80. ..msg = "Unsupported sign up action",
  81. );
  82. }
  83. @override
  84. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  85. return UserBackendService.getCurrentUserProfile();
  86. }
  87. @override
  88. Future<Either<FlowyError, UserProfilePB>> signInWithMagicLink({
  89. required String email,
  90. Map<String, String> map = const {},
  91. }) async {
  92. return left(
  93. FlowyError.create()
  94. ..code = ErrorCode.Internal
  95. ..msg = "Unsupported sign up action",
  96. );
  97. }
  98. }