Skip to content

Commit

Permalink
style(features): Format code
Browse files Browse the repository at this point in the history
* 1279 issues left.
  • Loading branch information
realth000 committed Feb 1, 2024
1 parent f0e1e6b commit 29d6aec
Show file tree
Hide file tree
Showing 43 changed files with 481 additions and 145 deletions.
38 changes: 28 additions & 10 deletions lib/features/authentication/bloc/authentication_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import 'package:tsdm_client/utils/debug.dart';
part 'authentication_event.dart';
part 'authentication_state.dart';

/// Emitter
typedef AuthenticationEmitter = Emitter<AuthenticationState>;

/// Bloc the authentication, including login and logout.
///
/// This bloc should be used as a global long-live bloc.
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
AuthenticationBloc(
{required AuthenticationRepository authenticationRepository})
: _authenticationRepository = authenticationRepository,
/// Constructor
AuthenticationBloc({
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
super(const AuthenticationState()) {
on<AuthenticationFetchLoginHashRequested>(
_onAuthenticationFetchLoginHashRequested);
_onAuthenticationFetchLoginHashRequested,
);
on<AuthenticationLoginRequested>(_onAuthenticationLoginRequested);
}

Expand All @@ -32,15 +38,23 @@ class AuthenticationBloc
emit(state.copyWith(status: AuthenticationStatus.fetchingHash));
try {
final loginHash = await _authenticationRepository.fetchHash();
emit(state.copyWith(
status: AuthenticationStatus.gotHash, loginHash: loginHash));
emit(
state.copyWith(
status: AuthenticationStatus.gotHash,
loginHash: loginHash,
),
);
} on HttpRequestFailedException catch (e) {
debug('failed to fetch login hash: $e');
emit(state.copyWith(status: AuthenticationStatus.failed));
} on LoginException catch (e) {
debug('failed to fetch login hash: $e');
emit(state.copyWith(
status: AuthenticationStatus.failed, loginException: e));
emit(
state.copyWith(
status: AuthenticationStatus.failed,
loginException: e,
),
);
}
}

Expand All @@ -57,8 +71,12 @@ class AuthenticationBloc
emit(state.copyWith(status: AuthenticationStatus.failed));
} on LoginException catch (e) {
debug('failed to login: $e');
emit(state.copyWith(
status: AuthenticationStatus.failed, loginException: e));
emit(
state.copyWith(
status: AuthenticationStatus.failed,
loginException: e,
),
);
}
}
}
5 changes: 5 additions & 0 deletions lib/features/authentication/bloc/authentication_event.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
part of 'authentication_bloc.dart';

/// Event of authentication.
sealed class AuthenticationEvent extends Equatable {
const AuthenticationEvent();

Expand All @@ -10,7 +11,11 @@ sealed class AuthenticationEvent extends Equatable {
/// Call this event to fetch hash data required in login process before login.
final class AuthenticationFetchLoginHashRequested extends AuthenticationEvent {}

/// User request to login with user credential.
final class AuthenticationLoginRequested extends AuthenticationEvent {
/// Constructor.
const AuthenticationLoginRequested(this.userCredential) : super();

/// User credential.
final UserCredential userCredential;
}
14 changes: 13 additions & 1 deletion lib/features/authentication/bloc/authentication_state.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
part of 'authentication_bloc.dart';

/// Status of authentication.
enum AuthenticationStatus {
/// Initial state
initial,

/// Fetching hash data that need to use in login process.
fetchingHash,

//
/// After got the form hash.
gotHash,

/// Polling login request.
Expand All @@ -19,19 +21,29 @@ enum AuthenticationStatus {
failed,
}

/// State of authentication.
///
/// Carrying all current logged user info and login status.
final class AuthenticationState extends Equatable {
/// Constructor.
const AuthenticationState({
this.status = AuthenticationStatus.initial,
this.loginHash,
this.loginException,
});

/// Status of authentication.
final AuthenticationStatus status;

/// The login hash used to login.
///
/// Useless unless is going to login.
final LoginHash? loginHash;

/// Exception happened in login.
final LoginException? loginException;

/// Copy with.
AuthenticationState copyWith({
AuthenticationStatus? status,
LoginHash? loginHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ import 'package:universal_html/parsing.dart';

/// Status of authentication.
enum AuthenticationStatus {
/// Unknown state.
///
/// Same with [unauthenticated].
unknown,

/// Have user logged.
authenticated,

/// No one logged.
unauthenticated,
}

Expand All @@ -49,6 +56,7 @@ enum AuthenticationStatus {
///
/// **Need to call dispose.**
class AuthenticationRepository {
/// Constructor.
AuthenticationRepository({User? user}) : _authedUser = user;

static const _checkAuthUrl = '$baseUrl/home.php?mod=spacecp';
Expand All @@ -74,10 +82,13 @@ class AuthenticationRepository {

User? _authedUser;

/// The current logged user.
User? get currentUser => _authedUser;

/// Authentication status stream.
Stream<AuthenticationStatus> get status => _controller.asBroadcastStream();

/// Dispose the resources.
void dispose() {
_controller.close();
}
Expand All @@ -88,7 +99,8 @@ class AuthenticationRepository {
///
/// * **HttpRequestFailedException** when http request failed.
/// * **LoginFormHashNotFoundException** when form hash not found.
/// * **LoginInvalidFormHashException** when form hash found but not in the expected format.
/// * **LoginInvalidFormHashException** when form hash found but not in the
/// expected format.
Future<LoginHash> fetchHash() async {
// TODO: Parse CDATA.
// 返回的data是xml:
Expand Down Expand Up @@ -120,10 +132,6 @@ class AuthenticationRepository {
return LoginHash(formHash: formHash, loginHash: loginHash);
}

Future<void> loginWithCookie(Map<String, dynamic> cookieMap) async {
throw UnimplementedError();
}

/// Login with password and other parameters in [credential].
///
/// Will not change authentication status if failed to login.
Expand Down
23 changes: 23 additions & 0 deletions lib/features/authentication/repository/exceptions/exceptions.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
/// Basic exception class of login
sealed class LoginException implements Exception {}

/// The form hash used in login progress is not found.
final class LoginFormHashNotFoundException implements LoginException {}

/// Found form hash, but it's not in the expect format.
final class LoginInvalidFormHashException implements LoginException {}

/// The login result message of login progress is not found.
///
/// Indicating that we do not know whether we logged in successful or not.
final class LoginMessageNotFoundException implements LoginException {}

/// The captcha user texted is incorrect.
final class LoginIncorrectCaptchaException implements LoginException {}

/// Incorrect password or account.
final class LoginInvalidCredentialException implements LoginException {}

/// Security question or its answer is incorrect.
final class LoginIncorrectSecurityQuestionException implements LoginException {}

/// Reached the limit of login attempt.
///
/// Maybe locked in 20 minutes.
final class LoginAttemptLimitException implements LoginException {}

/// User info not found when try to login after login seems success.
///
/// Now we should update the logged user info but this exception means we can
/// not found the logged user info.
final class LoginUserInfoNotFoundException implements LoginException {}

/// Some other exception that not recognized.
final class LoginOtherErrorException implements LoginException {
/// Constructor.
LoginOtherErrorException(this.message);

/// Message to describe the error.
final String message;
}

/// Basic exception class of logout.
sealed class LogoutException implements Exception {}

/// The form hash used to logout is not found.
final class LogoutFormHashNotFoundException implements LogoutException {}

/// Failed to logout.
///
/// Nearly impossible to happen.
final class LogoutFailedException implements LogoutException {}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ enum LoginResult {
return LoginResult.success;
}

// Impossible unless server response page updated and changed these messages.
// Impossible unless server response page updated and changed these
// messages.
debug(
'login result check passed but message check maybe outdated: $message',
);
Expand All @@ -65,7 +66,8 @@ enum LoginResult {

// Other unrecognized error.
debug(
'login result check not passed: alert_info class with unknown message: $message',
'login result check not passed: '
'alert_info class with unknown message: $message',
);
return LoginResult.otherError;
}
Expand All @@ -85,7 +87,8 @@ enum LoginResult {

// Other unrecognized error.
debug(
'login result check not passed: alert_error with unknown message: $message',
'login result check not passed: '
'alert_error with unknown message: $message',
);
return LoginResult.otherError;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/// User info used in repo.
class UserInfo {
/// Constructor.
const UserInfo({required this.uid, required this.username});

/// User id.
final String uid;

/// User name.
final String username;

@override
Expand Down
7 changes: 7 additions & 0 deletions lib/features/authentication/repository/models/hash.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import 'package:equatable/equatable.dart';

/// A group of login hash used in login or logout progress.
class LoginHash extends Equatable {
/// Constructor.
const LoginHash({
required this.formHash,
required this.loginHash,
});

/// Form hash.
final String formHash;

/// Login hash.
///
/// Seems not used.
final String loginHash;

@override
Expand Down
17 changes: 17 additions & 0 deletions lib/features/authentication/repository/models/user.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import 'package:equatable/equatable.dart';

/// Authenticated user.
///
/// [username], [uid] and [email] should have the same priority in identifying
/// the user.
///
/// * Though we may not know the all info above when trying to login.
/// * All the info above MUST be provided before save logged info info local
/// storage.
class User extends Equatable {
/// Constructor.
const User({
this.username,
this.uid,
this.password,
this.email,
});

/// Username.
final String? username;

/// Uid.
final String? uid;

/// Password.
///
/// Never save this to local store.
final String? password;

/// Email address.
final String? email;

@override
Expand Down
11 changes: 11 additions & 0 deletions lib/features/authentication/repository/models/user_credential.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import 'package:tsdm_client/constants/url.dart';

/// User name field type, pair with password.
enum LoginField {
/// Username
username,

/// Email address.
email,

/// Uid
uid;

@override
Expand All @@ -18,17 +23,22 @@ enum LoginField {

/// Additional security question.
class SecurityQuestion {
/// Constructor.
const SecurityQuestion({
required this.questionId,
required this.answer,
});

/// The question id of security question chose by user.
final String questionId;

/// The answer text that user texted.
final String answer;
}

/// Login credential.
class UserCredential {
/// Constructor.
const UserCredential({
required this.loginField,
required this.loginFieldValue,
Expand Down Expand Up @@ -76,6 +86,7 @@ class UserCredential {
/// Can be null.
final SecurityQuestion? securityQuestion;

/// Method to convert to json.
Map<String, dynamic> toJson() {
final m = {
'loginfield': loginField.toString(),
Expand Down
Loading

0 comments on commit 29d6aec

Please sign in to comment.