signInByUsername method
Future<AuthResponse<T> >
signInByUsername(
- UsernameAuthenticator authenticator, {
- Object? args,
- String? id,
- SignByBiometricCallback<
T> ? onBiometric, - bool notifiable = true,
Implementation
Future<AuthResponse<T>> signInByUsername(
UsernameAuthenticator authenticator, {
Object? args,
String? id,
SignByBiometricCallback<T>? onBiometric,
bool notifiable = true,
}) async {
emit(
args: args,
id: id,
notifiable: notifiable,
const AuthResponse.loading(Provider.username, AuthType.login),
);
try {
final hasAnonymous = this.hasAnonymous;
final response = await delegate.signInWithUsernameNPassword(
authenticator.username,
authenticator.password,
);
if (!response.isSuccessful) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
response.error,
provider: Provider.username,
type: AuthType.login,
),
);
}
final result = response.data;
if (result == null) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
response.error,
provider: Provider.username,
type: AuthType.login,
),
);
}
final user = authenticator.update(
id: Modifier(result.uid),
anonymous: Modifier(result.isAnonymous),
email: Modifier(result.email),
name: Modifier(result.displayName),
phone: Modifier(result.phoneNumber),
photo: Modifier(result.photoURL),
provider: Modifier(Provider.username),
loggedIn: Modifier(true),
loggedInTime: Modifier(EntityHelper.generateTimeMills),
);
final value = await _update(
id: user.id,
hasAnonymous: hasAnonymous,
onBiometric: onBiometric,
initials: user.filtered,
updates: {
if (authenticator.extra != null) ...authenticator.extra!,
AuthKeys.i.loggedIn: true,
AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
AuthKeys.i.anonymous: result.isAnonymous,
AuthKeys.i.email: result.email,
AuthKeys.i.name: result.displayName,
AuthKeys.i.password: authenticator.password,
AuthKeys.i.phone: result.phoneNumber,
AuthKeys.i.photo: result.photoURL,
AuthKeys.i.provider: Provider.username.id,
AuthKeys.i.username: authenticator.username,
AuthKeys.i.verified: result.emailVerified,
},
);
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.authenticated(
value,
msg: msg.signInWithUsername.done,
provider: Provider.username,
type: AuthType.login,
),
);
} catch (error) {
return emit(
args: args,
id: id,
notifiable: notifiable,
AuthResponse.failure(
msg.signInWithUsername.failure ?? error,
provider: Provider.username,
type: AuthType.login,
),
);
}
}