login method
Future<LoginResponse>
login(
- String type, {
- AuthenticationIdentifier? identifier,
- String? password,
- String? token,
- String? deviceId,
- String? initialDeviceDisplayName,
- bool? refreshToken,
- @Deprecated('Deprecated in favour of identifier.') String? user,
- @Deprecated('Deprecated in favour of identifier.') String? medium,
- @Deprecated('Deprecated in favour of identifier.') String? address,
- void onInitStateChanged()?,
override
Handles the login and allows the client to call all APIs which require
authentication. Returns false if the login was not successful. Throws
MatrixException if login was not successful.
To just login with the username 'alice' you set identifier to:
AuthenticationUserIdentifier(user: 'alice')
Maybe you want to set user to the same String to stay compatible with
older server versions.
Implementation
@override
Future<LoginResponse> login(
String type, {
AuthenticationIdentifier? identifier,
String? password,
String? token,
String? deviceId,
String? initialDeviceDisplayName,
bool? refreshToken,
@Deprecated('Deprecated in favour of identifier.') String? user,
@Deprecated('Deprecated in favour of identifier.') String? medium,
@Deprecated('Deprecated in favour of identifier.') String? address,
void Function(InitState)? onInitStateChanged,
}) async {
if (homeserver == null) {
final domain = identifier is AuthenticationUserIdentifier
? identifier.user.domain
: null;
if (domain != null) {
await checkHomeserver(Uri.https(domain, ''));
} else {
throw Exception('No homeserver specified!');
}
}
final response = await super.login(
type,
identifier: identifier,
password: password,
token: token,
deviceId: deviceId ?? deviceID,
initialDeviceDisplayName: initialDeviceDisplayName,
// ignore: deprecated_member_use
user: user,
// ignore: deprecated_member_use
medium: medium,
// ignore: deprecated_member_use
address: address,
refreshToken: refreshToken ?? onSoftLogout != null,
);
// Connect if there is an access token in the response.
final accessToken = response.accessToken;
final deviceId_ = response.deviceId;
final userId = response.userId;
final homeserver_ = homeserver;
if (homeserver_ == null) {
throw Exception('Registered but homerserver is null.');
}
final expiresInMs = response.expiresInMs;
final tokenExpiresAt = expiresInMs == null
? null
: DateTime.now().add(Duration(milliseconds: expiresInMs));
await init(
newToken: accessToken,
newTokenExpiresAt: tokenExpiresAt,
newRefreshToken: response.refreshToken,
newUserID: userId,
newHomeserver: homeserver_,
newDeviceName: initialDeviceDisplayName ?? '',
newDeviceID: deviceId_,
onInitStateChanged: onInitStateChanged,
);
return response;
}