login method Null safety
Gets the account with the given id
(Retrieved from auths
)
If auths
doesn't contain the id, then address
and password
are required
to load the account from api
if then, the account isn't retrieved and elseNew
is true a new account is created
or else, an exception is thrown.
Implementation
static FutureOr<GWAccount> login({
String? id,
String? address,
String? password,
bool elseNew: true,
}) async {
assert(
id != null || (address != null && password != null) || elseNew,
'Either id or address and password must be provided',
);
if (id != null && auths.containsKey(id)) {
return auths[id]!.account;
}
GWAccount account;
if (address != null && password != null) {
String token = await getToken(address, password);
account = await accountFromApi(address, password);
auths[account.id] = Auth(account, token);
return account;
}
if (elseNew) {
address ??= '';
account =
await register(username: address.split('@')[0], password: password);
String token = await getToken(account.address, account.password);
auths[account.id] = Auth(account, token);
return account;
} else {
throw MailException('Invalid arguments', -1);
}
}