212 lines
5.6 KiB
Dart
Raw Normal View History

import 'package:appflowy/user/application/auth/auth_service.dart';
2021-07-13 13:14:49 +08:00
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
show UserProfilePB;
2021-07-13 13:14:49 +08:00
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
part 'sign_in_bloc.freezed.dart';
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final AuthService authService;
SignInBloc(this.authService) : super(SignInState.initial()) {
2022-01-04 22:44:03 +08:00
on<SignInEvent>((event, emit) async {
await event.map(
signedInWithUserEmailAndPassword: (e) async {
await _performActionOnSignIn(
state,
emit,
);
},
signedInWithOAuth: (value) async =>
await _performActionOnSignInWithOAuth(
state,
emit,
value.platform,
),
signedInAsGuest: (value) async => await _performActionOnSignInAsGuest(
state,
emit,
),
2022-01-04 22:44:03 +08:00
emailChanged: (EmailChanged value) async {
emit(
state.copyWith(
email: value.email,
emailError: none(),
successOrFail: none(),
),
);
2022-01-04 22:44:03 +08:00
},
passwordChanged: (PasswordChanged value) async {
emit(
state.copyWith(
password: value.password,
passwordError: none(),
successOrFail: none(),
),
);
2022-01-04 22:44:03 +08:00
},
signedWithMagicLink: (SignedWithMagicLink value) async {
await _performActionOnSignInWithMagicLink(state, emit, value.email);
},
2022-01-04 22:44:03 +08:00
);
});
2021-07-13 13:14:49 +08:00
}
Future<void> _performActionOnSignIn(
SignInState state,
Emitter<SignInState> emit,
) async {
final result = await authService.signIn(
email: state.email ?? '',
password: state.password ?? '',
);
emit(
result.fold(
(error) => stateFromCode(error),
(userProfile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(userProfile)),
),
),
);
}
Future<void> _performActionOnSignInWithOAuth(
SignInState state,
Emitter<SignInState> emit,
String platform,
) async {
emit(
state.copyWith(
isSubmitting: true,
emailError: none(),
passwordError: none(),
successOrFail: none(),
),
);
2021-07-13 13:14:49 +08:00
final result = await authService.signUpWithOAuth(
platform: platform,
2022-01-31 08:15:49 +08:00
);
emit(
result.fold(
(error) => stateFromCode(error),
(userProfile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(userProfile)),
),
),
);
}
Future<void> _performActionOnSignInWithMagicLink(
SignInState state,
Emitter<SignInState> emit,
String email,
) async {
emit(
state.copyWith(
isSubmitting: true,
emailError: none(),
passwordError: none(),
successOrFail: none(),
),
);
final result = await authService.signInWithMagicLink(
email: email,
);
emit(
result.fold(
(error) => stateFromCode(error),
(userProfile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(userProfile)),
),
),
);
}
Future<void> _performActionOnSignInAsGuest(
SignInState state,
Emitter<SignInState> emit,
) async {
emit(
state.copyWith(
isSubmitting: true,
emailError: none(),
passwordError: none(),
successOrFail: none(),
),
);
final result = await authService.signUpAsGuest();
emit(
result.fold(
(error) => stateFromCode(error),
(userProfile) => state.copyWith(
isSubmitting: false,
successOrFail: some(left(userProfile)),
),
),
);
2021-07-13 13:14:49 +08:00
}
2021-12-14 18:04:51 +08:00
SignInState stateFromCode(FlowyError error) {
feat: migrate user data to cloud (#3078) * refactor: weak passed-in params in handler * refactor: rename struct * chore: update tables * chore: update schema * chore: add permission * chore: update tables * chore: support transaction mode * chore: workspace database id * chore: add user workspace * feat: return list of workspaces * chore: add user to workspace * feat: separate database row table * refactor: update schema * chore: partition table * chore: use transaction * refactor: dir * refactor: collab db ref * fix: collab db lock * chore: rename files * chore: add tables descriptions * chore: update readme * docs: update documentation * chore: rename crate * chore: update ref * chore: update tests * chore: update tests * refactor: crate deps * chore: update crate ref * chore: remove unused deps * chore: remove unused deps * chore: update collab crate refs * chore: replace client with transaction in pooler * refactor: return error type * refactor: use anyhow error in deps * feat: supabase postgrest user signin (wip) * fix: Cargo.toml source git deps, changed Error to anyhow::Error * fix: uuid serialization * chore: fix conflict * chore: extend the response * feat: add implementation place holders * feat: impl get_user_workspaces * feat: impl get_user_profile * test: create workspace * fix: postgrest: field names and alias * chore: implement folder restful api * chore: implement collab storate with restful api * feat: added placeholders for impl: update_user_profile, check_user * feat: impl: update_user_profile * feat: impl: check_user * fix: use UidResponse, add more debug info for serde serialization error * fix: get_user_profile: use Optional<UserProfileResponse> * chore: imple init sync * chore: support soft delete * feat: postgresql: add migration test * feat: postgresql migration test: added UID display and colored output * feat: postgresql migration test: workspace role * feat: postgresql migration test: create shared common utils * feat: postgresql migration test: fixed shebang * chore: add flush_collab_update pg function * chore: implement datbaase and document restful api * chore: migrate to use restful api * chore: update table schema * chore: fix tests * chore: remove unused code * chore: format code * chore: remove unused env * fix: tauri build * fix: tauri build --------- Co-authored-by: Fu Zi Xiang <speed2exe@live.com.sg>
2023-07-29 09:46:24 +08:00
switch (ErrorCode.valueOf(error.code)) {
case ErrorCode.EmailFormatInvalid:
return state.copyWith(
isSubmitting: false,
emailError: some(error.msg),
passwordError: none(),
);
case ErrorCode.PasswordFormatInvalid:
return state.copyWith(
isSubmitting: false,
passwordError: some(error.msg),
emailError: none(),
);
default:
return state.copyWith(
isSubmitting: false,
successOrFail: some(right(error)),
);
}
}
2021-07-13 13:14:49 +08:00
}
@freezed
2022-03-25 15:02:43 +08:00
class SignInEvent with _$SignInEvent {
const factory SignInEvent.signedInWithUserEmailAndPassword() =
SignedInWithUserEmailAndPassword;
const factory SignInEvent.signedInWithOAuth(String platform) =
SignedInWithOAuth;
const factory SignInEvent.signedInAsGuest() = SignedInAsGuest;
const factory SignInEvent.signedWithMagicLink(String email) =
SignedWithMagicLink;
const factory SignInEvent.emailChanged(String email) = EmailChanged;
const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
}
@freezed
2022-03-25 15:02:43 +08:00
class SignInState with _$SignInState {
const factory SignInState({
String? email,
String? password,
required bool isSubmitting,
required Option<String> passwordError,
required Option<String> emailError,
2022-07-19 14:40:56 +08:00
required Option<Either<UserProfilePB, FlowyError>> successOrFail,
}) = _SignInState;
factory SignInState.initial() => SignInState(
isSubmitting: false,
passwordError: none(),
emailError: none(),
successOrFail: none(),
);
}