feat(auth): add reactive repo + extra data + router examples + tests
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
export 'builder/authentication_builder.dart';
|
||||
export 'cubit/authentication_cubit.dart';
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/features/authentication/cubit/authentication_cubit.dart';
|
||||
|
||||
class AuthenticationBuilder<Extra> extends StatelessWidget {
|
||||
const AuthenticationBuilder({
|
||||
required this.authenticated,
|
||||
required this.unauthenticated,
|
||||
required this.unknown,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final Widget Function(
|
||||
BuildContext context,
|
||||
User user,
|
||||
Extra? extra,
|
||||
) authenticated;
|
||||
final Widget Function(BuildContext context) unauthenticated;
|
||||
final Widget Function(BuildContext context) unknown;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
BlocBuilder<AuthenticationCubit<Extra>, AuthenticationState<Extra>>(
|
||||
builder: (context, state) {
|
||||
if (state.status == AuthenticationStatus.authenticated) {
|
||||
if (state.user != null) {
|
||||
return authenticated(context, state.user!, state.extra);
|
||||
} else {
|
||||
return unauthenticated(context);
|
||||
}
|
||||
} else if (state.status == AuthenticationStatus.unauthenticated) {
|
||||
return unauthenticated(context);
|
||||
} else {
|
||||
return unknown(context);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
|
||||
|
||||
part 'authentication_state.dart';
|
||||
|
||||
class AuthenticationCubit<Extra> extends Cubit<AuthenticationState<Extra>> {
|
||||
final AuthenticationRepository _authenticationRepository;
|
||||
late final StreamSubscription<AuthCubitStatus> _statusSubscription;
|
||||
|
||||
StreamSubscription<User>? _userSubscription;
|
||||
|
||||
final Future<Extra?> Function(User user)? _onAuthSuccess;
|
||||
|
||||
AuthenticationCubit({
|
||||
required AuthenticationRepository authenticationRepository,
|
||||
Future<Extra?> Function(User user)? onAuthSuccess,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
_onAuthSuccess = onAuthSuccess,
|
||||
super(const AuthenticationState.unknown()) {
|
||||
_subscribeStatus();
|
||||
}
|
||||
|
||||
Future<AuthCubitStatus> get status async =>
|
||||
_authenticationRepository.cubitStatus.last;
|
||||
|
||||
void _subscribeStatus() {
|
||||
_statusSubscription = _authenticationRepository.cubitStatus.listen(
|
||||
(status) {
|
||||
switch (status) {
|
||||
case AuthCubitStatus.started:
|
||||
start();
|
||||
break;
|
||||
case AuthCubitStatus.stoped:
|
||||
stop();
|
||||
break;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
final firstUser = await _authenticationRepository.user.first;
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
|
||||
return changeStatus(firstUser);
|
||||
}
|
||||
|
||||
bool start() {
|
||||
_userSubscription = _authenticationRepository.user.listen(changeStatus);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop() {
|
||||
_userSubscription?.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> changeStatus(User user) async {
|
||||
if (user.isNotEmpty) {
|
||||
final Extra? extra = await _onAuthSuccess?.call(user);
|
||||
emit(AuthenticationState.authenticated(user, extra));
|
||||
} else {
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.stoped);
|
||||
emit(const AuthenticationState.unauthenticated());
|
||||
}
|
||||
}
|
||||
|
||||
void logOut() {
|
||||
unawaited(_authenticationRepository.signOut());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_userSubscription?.cancel();
|
||||
_statusSubscription.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
part of 'authentication_cubit.dart';
|
||||
|
||||
enum AuthenticationStatus {
|
||||
unknown,
|
||||
authenticated,
|
||||
unauthenticated,
|
||||
}
|
||||
|
||||
class AuthenticationState<Extra> extends Equatable {
|
||||
final AuthenticationStatus status;
|
||||
final User? user;
|
||||
final Extra? extra;
|
||||
|
||||
const AuthenticationState._({
|
||||
required this.status,
|
||||
this.user,
|
||||
this.extra,
|
||||
});
|
||||
|
||||
const AuthenticationState.unknown()
|
||||
: this._(status: AuthenticationStatus.unknown);
|
||||
|
||||
const AuthenticationState.authenticated(
|
||||
User user,
|
||||
Extra? extra,
|
||||
) : this._(
|
||||
status: AuthenticationStatus.authenticated,
|
||||
user: user,
|
||||
extra: extra,
|
||||
);
|
||||
|
||||
const AuthenticationState.unauthenticated()
|
||||
: this._(status: AuthenticationStatus.unauthenticated);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, user, extra];
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'AuthenticationState(status: $status, user: $user, extra: $extra)';
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart' show FormStatus;
|
||||
|
||||
part 'email_verification_state.dart';
|
||||
|
||||
class EmailVerificationCubit extends Cubit<EmailVerificationState> {
|
||||
final AuthenticationRepository _authenticationRepository;
|
||||
|
||||
EmailVerificationCubit({
|
||||
required AuthenticationRepository authenticationRepository,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
super(const EmailVerificationState());
|
||||
|
||||
Future<void> sendEmailVerification() async {
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.sendEmailVerification();
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SendEmailVerificationFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkEmailVerification() async {
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.refresh();
|
||||
emit(
|
||||
state.copyWith(
|
||||
isVerified: _authenticationRepository.currentUser.emailVerified,
|
||||
status: FormStatus.submissionSuccess,
|
||||
),
|
||||
);
|
||||
} on RefreshFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
part of 'email_verification_cubit.dart';
|
||||
|
||||
class EmailVerificationState extends Equatable {
|
||||
final bool isVerified;
|
||||
final FormStatus status;
|
||||
final String? errorMessage;
|
||||
|
||||
const EmailVerificationState({
|
||||
this.isVerified = false,
|
||||
this.status = FormStatus.pure,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
EmailVerificationState copyWith({
|
||||
bool? isVerified,
|
||||
FormStatus? status,
|
||||
String? errorMessage,
|
||||
}) => EmailVerificationState(
|
||||
isVerified: isVerified ?? this.isVerified,
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object> get props => [isVerified, status];
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
export 'cubit/email_verification_cubit.dart';
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
part 'password_reset_state.dart';
|
||||
|
||||
class PasswordResetCubit extends Cubit<PasswordResetState> {
|
||||
final AuthenticationRepository _authenticationRepository;
|
||||
|
||||
final FormValidator _validationStrategy;
|
||||
|
||||
PasswordResetCubit({
|
||||
required AuthenticationRepository authenticationRepository,
|
||||
FormValidator validationStrategy = const EveryInputValidator(),
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
_validationStrategy = validationStrategy,
|
||||
super(const PasswordResetState());
|
||||
|
||||
void emailChanged(String value) {
|
||||
final Email email = Email.dirty(value);
|
||||
emit(
|
||||
state.copyWith(
|
||||
email: email,
|
||||
status: _validationStrategy.rawValidate([email]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> sendPasswordResetEmail() async {
|
||||
if (!state.status.isValidated) {
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.sendPasswordResetEmail(
|
||||
email: state.email.value,
|
||||
);
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SendPasswordResetEmailFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
part of 'password_reset_cubit.dart';
|
||||
|
||||
class PasswordResetState extends Equatable {
|
||||
final Email email;
|
||||
final FormStatus status;
|
||||
final String? errorMessage;
|
||||
|
||||
const PasswordResetState({
|
||||
this.email = const Email.pure(),
|
||||
this.status = FormStatus.pure,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
PasswordResetState copyWith({
|
||||
Email? email,
|
||||
FormStatus? status,
|
||||
String? errorMessage,
|
||||
}) => PasswordResetState(
|
||||
email: email ?? this.email,
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object> get props => [email, status];
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
export 'cubit/password_reset_cubit.dart';
|
||||
@@ -0,0 +1,125 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
part 'sign_in_state.dart';
|
||||
|
||||
class SignInCubit extends Cubit<SignInState> {
|
||||
final AuthenticationRepository _authenticationRepository;
|
||||
|
||||
final FormValidator _validationStrategy;
|
||||
|
||||
SignInCubit({
|
||||
required AuthenticationRepository authenticationRepository,
|
||||
FormValidator validationStrategy = const EveryInputValidator(),
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
_validationStrategy = validationStrategy,
|
||||
super(const SignInState());
|
||||
|
||||
void emailChanged(String value) {
|
||||
final Email email = Email.dirty(value);
|
||||
emit(
|
||||
state.copyWith(
|
||||
email: email,
|
||||
status: _validationStrategy.rawValidate([email, state.password]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final Password password = Password.dirty(value);
|
||||
emit(
|
||||
state.copyWith(
|
||||
password: password,
|
||||
status: _validationStrategy.rawValidate([state.email, password]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> signInAnonymously() async {
|
||||
if (state.status.isSubmissionInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.signInAnonymously();
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SignInAnonymouslyFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signInWithGoogle() async {
|
||||
if (state.status.isSubmissionInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.signInWithGoogle();
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SignInWithGoogleFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signInWithEmailAndPassword() async {
|
||||
if (!state.status.isValidated) {
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
await _authenticationRepository.signInWithEmailAndPassword(
|
||||
email: state.email.value,
|
||||
password: state.password.value,
|
||||
);
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SignInWithEmailAndPasswordFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
part of 'sign_in_cubit.dart';
|
||||
|
||||
class SignInState extends Equatable {
|
||||
final Email email;
|
||||
final Password password;
|
||||
final FormStatus status;
|
||||
final String? errorMessage;
|
||||
|
||||
const SignInState({
|
||||
this.email = const Email.pure(),
|
||||
this.password = const Password.pure(),
|
||||
this.status = FormStatus.pure,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
SignInState copyWith({
|
||||
Email? email,
|
||||
Password? password,
|
||||
FormStatus? status,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
SignInState(
|
||||
email: email ?? this.email,
|
||||
password: password ?? this.password,
|
||||
status: status ?? this.status,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object> get props => [email, password, status];
|
||||
|
||||
@override
|
||||
String toString() => '''
|
||||
email: $email,
|
||||
password: $password,
|
||||
status: $status,
|
||||
errorMessage: $errorMessage,
|
||||
''';
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
export 'cubit/sign_in_cubit.dart';
|
||||
@@ -0,0 +1,167 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/enum/auth_cubit_status.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
|
||||
import 'package:wyatt_authentication_bloc/src/domain/repositories/authentication_repository.dart';
|
||||
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
|
||||
|
||||
part 'sign_up_state.dart';
|
||||
|
||||
class SignUpCubit extends Cubit<SignUpState> {
|
||||
final AuthenticationRepository _authenticationRepository;
|
||||
|
||||
final Future<void> Function(SignUpState state, String? uid)? _onSignUpSuccess;
|
||||
final FormValidator _validationStrategy;
|
||||
|
||||
SignUpCubit({
|
||||
required AuthenticationRepository authenticationRepository,
|
||||
required FormData formData,
|
||||
FormValidator validationStrategy = const EveryInputValidator(),
|
||||
Future<void> Function(SignUpState state, String? uid)? onSignUpSuccess,
|
||||
}) : _authenticationRepository = authenticationRepository,
|
||||
_onSignUpSuccess = onSignUpSuccess,
|
||||
_validationStrategy = validationStrategy,
|
||||
super(SignUpState(data: formData));
|
||||
|
||||
void emailChanged(String value) {
|
||||
final Email email = Email.dirty(value);
|
||||
|
||||
final List<FormInputValidator<dynamic, ValidationError>> toValidate = [
|
||||
email,
|
||||
state.password,
|
||||
...state.data.validators<dynamic, ValidationError>(),
|
||||
];
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
email: email,
|
||||
status: _validationStrategy.rawValidate(toValidate),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void passwordChanged(String value) {
|
||||
final Password password = Password.dirty(value);
|
||||
|
||||
final List<FormInputValidator<dynamic, ValidationError>> toValidate = [
|
||||
state.email,
|
||||
password,
|
||||
...state.data.validators<dynamic, ValidationError>(),
|
||||
];
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
password: password,
|
||||
status: _validationStrategy.rawValidate(toValidate),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Take from wyatt_form_bloc/wyatt_form_bloc.dart
|
||||
void dataChanged<T>(
|
||||
String field,
|
||||
FormInputValidator<T, ValidationError> dirtyValue,
|
||||
) {
|
||||
final form = state.data.clone();
|
||||
|
||||
if (form.contains(field)) {
|
||||
form.updateValidator(field, dirtyValue);
|
||||
} else {
|
||||
throw Exception('Form field $field not found');
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
data: form,
|
||||
status: _validationStrategy.rawValidate(
|
||||
[
|
||||
state.email,
|
||||
state.password,
|
||||
...state.data.validators<dynamic, ValidationError>(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Take from wyatt_form_bloc/wyatt_form_bloc.dart
|
||||
void updateFormData(
|
||||
FormData data, {
|
||||
SetOperation operation = SetOperation.replace,
|
||||
}) {
|
||||
FormData form = data;
|
||||
|
||||
switch (operation) {
|
||||
case SetOperation.replace:
|
||||
form = data;
|
||||
break;
|
||||
case SetOperation.difference:
|
||||
form = state.data.difference(data);
|
||||
break;
|
||||
case SetOperation.intersection:
|
||||
form = state.data.intersection(data);
|
||||
break;
|
||||
case SetOperation.union:
|
||||
form = state.data.union(data);
|
||||
break;
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
data: form,
|
||||
status: _validationStrategy.rawValidate(
|
||||
[
|
||||
state.email,
|
||||
state.password,
|
||||
...state.data.validators<dynamic, ValidationError>(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> signUpFormSubmitted() async {
|
||||
if (!state.status.isValidated) {
|
||||
return;
|
||||
}
|
||||
emit(state.copyWith(status: FormStatus.submissionInProgress));
|
||||
try {
|
||||
final uid = await _authenticationRepository.signUp(
|
||||
email: state.email.value,
|
||||
password: state.password.value,
|
||||
);
|
||||
await _onSignUpSuccess?.call(state, uid);
|
||||
_authenticationRepository.changeCubitStatus(AuthCubitStatus.started);
|
||||
emit(state.copyWith(status: FormStatus.submissionSuccess));
|
||||
} on SignUpWithEmailAndPasswordFailureInterface catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
errorMessage: e.message,
|
||||
status: FormStatus.submissionFailure,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
emit(state.copyWith(status: FormStatus.submissionFailure));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
part of 'sign_up_cubit.dart';
|
||||
|
||||
class SignUpState extends Equatable {
|
||||
final Email email;
|
||||
final Password password;
|
||||
final FormStatus status;
|
||||
final FormData data;
|
||||
final String? errorMessage;
|
||||
|
||||
const SignUpState({
|
||||
required this.data,
|
||||
this.email = const Email.pure(),
|
||||
this.password = const Password.pure(),
|
||||
this.status = FormStatus.pure,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
SignUpState copyWith({
|
||||
Email? email,
|
||||
Password? password,
|
||||
FormStatus? status,
|
||||
FormData? data,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
SignUpState(
|
||||
email: email ?? this.email,
|
||||
password: password ?? this.password,
|
||||
status: status ?? this.status,
|
||||
data: data ?? this.data,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
|
||||
@override
|
||||
String toString() => 'SignUpState(email: $email, password: $password, '
|
||||
'status: $status, data: $data, errorMessage: $errorMessage)';
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password, status, data, errorMessage];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
export 'cubit/sign_up_cubit.dart';
|
||||
Reference in New Issue
Block a user