signInByUsername method
Future<AuthResponse<T> >
signInByUsername(
- UsernameAuthenticator authenticator, {
- SignByBiometricCallback? onBiometric,
- Object? args,
- String? id,
- bool notifiable = true,
override
Implementation
@override
Future<AuthResponse<T>> signInByUsername(
UsernameAuthenticator authenticator, {
SignByBiometricCallback? onBiometric,
Object? args,
String? id,
bool notifiable = true,
}) async {
emit(
args: args,
id: id,
notifiable: notifiable,
const AuthResponse.loading(AuthProviders.username, AuthType.login),
);
try {
final response = await authHandler.signInWithUsernameNPassword(
username: authenticator.username,
password: authenticator.password,
);
if (!response.isSuccessful) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
response.exception,
provider: AuthProviders.username,
type: AuthType.login,
),
);
}
final result = response.data?.user;
if (result == null) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
response.exception,
provider: AuthProviders.username,
type: AuthType.login,
),
);
}
final user = authenticator.copy(
id: result.uid,
email: result.email,
name: result.displayName,
phone: result.phoneNumber,
photo: result.photoURL,
provider: AuthProviders.username.name,
loggedIn: true,
loggedInTime: Entity.generateTimeMills,
);
BiometricStatus? biometric;
if (onBiometric != null) biometric = await onBiometric(user.mBiometric);
final value = await _update(
id: user.id,
initials:
(biometric != null ? user.copy(biometric: biometric.name) : user)
.source,
updates: {
...user.extra ?? {},
if (biometric != null) AuthKeys.i.biometric: biometric.name,
AuthKeys.i.loggedIn: true,
AuthKeys.i.loggedInTime: Entity.generateTimeMills,
},
);
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.authenticated(
value,
msg: msg.signInWithUsername.done,
provider: AuthProviders.username,
type: AuthType.login,
),
);
} catch (error) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
msg.signInWithUsername.failure ?? error,
provider: AuthProviders.username,
type: AuthType.login,
),
);
}
}