changePassword method
Changes the user's NTUT Portal password.
Requires an active session. Updates stored credentials so auto-login continues to work, then re-logins to refresh the session and clear the password expiry warning.
Throws NotLoggedInException if no stored credentials are available.
Implementation
Future<void> changePassword(
String currentPassword,
String newPassword,
) async {
final user = await _database.select(_database.users).getSingleOrNull();
if (user == null) throw NotLoggedInException();
await withAuth(
() => _portalService.changePassword(currentPassword, newPassword),
);
// Update stored credentials so auto-login uses the new password
await _secureStorage.write(key: _passwordKey, value: newPassword);
// Best-effort re-login to refresh session and passwordExpiresInDays.
// The password is already changed at this point, so don't fail if
// the re-login hits a transient error.
try {
final userDto = await _portalService.login(user.studentId, newPassword);
await (_database.update(
_database.users,
)..where((u) => u.id.equals(user.id))).write(
UsersCompanion(
passwordExpiresInDays: Value(userDto.passwordExpiresInDays),
),
);
} catch (_) {}
}