login method
Authenticates with NTUT Portal and saves the user profile.
Throws LoginException if login is rejected (wrong credentials, account locked, password expired, etc.). Throws DioException on network failure. On success, credentials are stored securely for auto-login.
Implementation
Future<User> login(String username, String password) async {
final userDto = await _portalService.login(username, password);
// Save credentials for auto-login
await _secureStorage.write(key: _usernameKey, value: username);
await _secureStorage.write(key: _passwordKey, value: password);
_onAuthStatusChanged(.authenticated);
return _database.transaction(() async {
await _database.delete(_database.users).go();
return _database
.into(_database.users)
.insertReturning(
UsersCompanion.insert(
studentId: username,
nameZh: userDto.name ?? '',
avatarFilename: userDto.avatarFilename ?? '',
email: userDto.email ?? '',
passwordExpiresInDays: Value(userDto.passwordExpiresInDays),
),
);
});
}