appflowy_auth_service.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class AppFlowyAuthService implements AuthService {
  13. @override
  14. Future<Either<FlowyError, UserProfilePB>> signIn({
  15. required String email,
  16. required String password,
  17. AuthTypePB authType = AuthTypePB.Local,
  18. Map<String, String> map = const {},
  19. }) async {
  20. final request = SignInPayloadPB.create()
  21. ..email = email
  22. ..password = password
  23. ..authType = authType;
  24. final response = UserEventSignIn(request).send();
  25. return response.then((value) => value.swap());
  26. }
  27. @override
  28. Future<Either<FlowyError, UserProfilePB>> signUp({
  29. required String name,
  30. required String email,
  31. required String password,
  32. AuthTypePB authType = AuthTypePB.Local,
  33. Map<String, String> map = const {},
  34. }) async {
  35. final request = SignUpPayloadPB.create()
  36. ..name = name
  37. ..email = email
  38. ..password = password
  39. ..authType = authType;
  40. final response = await UserEventSignUp(request).send().then(
  41. (value) => value.swap(),
  42. );
  43. return response;
  44. }
  45. @override
  46. Future<void> signOut({
  47. AuthTypePB authType = AuthTypePB.Local,
  48. Map<String, String> map = const {},
  49. }) async {
  50. final payload = SignOutPB()..authType = authType;
  51. await UserEventSignOut(payload).send();
  52. return;
  53. }
  54. @override
  55. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  56. AuthTypePB authType = AuthTypePB.Local,
  57. Map<String, String> map = const {},
  58. }) {
  59. const password = "AppFlowy123@";
  60. final uid = uuid();
  61. final userEmail = "[email protected]";
  62. return signUp(
  63. name: LocaleKeys.defaultUsername.tr(),
  64. password: password,
  65. email: userEmail,
  66. );
  67. }
  68. @override
  69. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  70. required String platform,
  71. AuthTypePB authType = AuthTypePB.Local,
  72. Map<String, String> map = const {},
  73. }) {
  74. throw UnimplementedError();
  75. }
  76. @override
  77. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  78. return UserBackendService.getCurrentUserProfile();
  79. }
  80. }