appflowy_auth_service.dart 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. await UserEventSignOut().send();
  51. return;
  52. }
  53. @override
  54. Future<Either<FlowyError, UserProfilePB>> signUpAsGuest({
  55. AuthTypePB authType = AuthTypePB.Local,
  56. Map<String, String> map = const {},
  57. }) {
  58. const password = "Guest!@123456";
  59. final uid = uuid();
  60. final userEmail = "[email protected]";
  61. return signUp(
  62. name: LocaleKeys.defaultUsername.tr(),
  63. password: password,
  64. email: userEmail,
  65. );
  66. }
  67. @override
  68. Future<Either<FlowyError, UserProfilePB>> signUpWithOAuth({
  69. required String platform,
  70. AuthTypePB authType = AuthTypePB.Local,
  71. Map<String, String> map = const {},
  72. }) {
  73. throw UnimplementedError();
  74. }
  75. @override
  76. Future<Either<FlowyError, UserProfilePB>> getUser() async {
  77. return UserBackendService.getCurrentUserProfile();
  78. }
  79. }