changeEmail method
Moves the account with id to email. Throws AuthException with
AuthFailure.accountExists when another account has that address and
AuthFailure.invalidEmail when it is not one.
Implementation
@override
Future<AuthUser> changeEmail(String id, String email) async {
final String key = _normalize(email);
if (!key.contains('@') || key.startsWith('@') || key.endsWith('@')) {
throw const AuthException(
AuthFailure.invalidEmail,
'That e-mail address is not valid.',
);
}
final MapEntry<String, _StoredCredential> current = _accounts.entries
.firstWhere((MapEntry<String, _StoredCredential> e) => e.value.user.id == id,
orElse: () => throw StateError('No account has that id.'));
if (current.key == key) return current.value.user;
if (_accounts.containsKey(key)) {
throw const AuthException(
AuthFailure.accountExists,
'An account already exists for that e-mail address.',
);
}
final AuthUser moved = AuthUser(
id: id,
email: key,
name: current.value.user.name,
metadata: current.value.user.metadata,
);
_accounts.remove(current.key);
_accounts[key] = _StoredCredential(moved, current.value.passwordHash);
if (_currentUser?.id == id) _currentUser = moved;
return moved;
}