Fix API key expiration label within 24 hours of expiry

Merged
BU
2d ago
+44-5

Problem

In Settings → Developers → API Keys, an API key whose expiry is less than 24 hours away, or less than 24 hours in the past, is shown without a duration:

  • the Expiration column reads In and nothing else;
  • the key's own page reads Expires in (next to Regenerate Key and in the Expiration field);
  • a key that has already expired is not shown as Expired (and is not red in the table) until a full day after its expiry.

formatExpiration and isExpired both go through beautifyDateDiff, which counts whole years and whole days. Inside the 24 hours on either side of the expiry it returns an empty string, so there is no duration to print and no - sign for the expired check to find.

Every key created with an expiry reaches this state on its last day. A key created through the API with a short expiresAt reaches it immediately.

I did not find an existing issue or pull request for this.

Fix

  • isExpired compares the expiry timestamp with the current time instead of looking for a - in the formatted text.
  • formatExpiration returns Expired whenever isExpired is true, and In less than a day / Expires in less than a day when the remaining time is under 24 hours.

beautifyDateDiff is unchanged. The change adds two strings (In less than a day, Expires in less than a day); following the repository guidance I left the translation catalogs out of the commit.

flowchart TD
    A[API key within 24 hours of its expiry] --> B{Expiration shown in Settings}
    B -->|Before| C[In followed by nothing even after expiry]
    B -->|After| D[In less than a day or Expired in red]

Narrated recording, before then after (sound on):

https://github.com/user-attachments/assets/5447e29b-b4b4-4d00-bbe3-56a61cb59f41

Edge cases and unchanged behaviour checked

Checked in the running app (front and server from main at 1dd1c249, then this branch, same seeded workspace and the same six API keys) and shown in the recording:

  • Before: a key expiring in about 8 hours and a key that expired 3 hours earlier both read In in the table; the expired key's page reads Expires in twice.
  • After: the same two rows read In less than a day and a red Expired; the expired key's page reads Expired in both places.
  • Edge case: the page of the key expiring in about 8 hours uses the long form, Expires in less than a day.
  • Edge case: a key about 34 hours from expiry, just past the boundary, still reads In 1 day.
  • Unchanged: Never, In 14 days, and a key that expired 10 days earlier (red Expired) look the same before and after.
  • Unchanged: the page of the key with two weeks left still reads Expires in 14 days.

Checked by the unit tests only:

  • isExpired is true 3 hours after the expiry and false 6 hours before it.
  • The long form with the "Expires" mention for a key that expired less than a day ago and for one that expired several days ago.

The recording was made in headless Chromium at 1280×720. The two keys inside the 24-hour window were created through the GraphQL createApiKey mutation with a chosen expiresAt, since the creation form only offers 15 days or more.

Tests run

$ npx jest src/modules/settings/developers/utils/__tests__/formatExpiration.test.ts --config=jest.config.mjs   (in packages/twenty-front)
Tests:       14 passed, 14 total

With the change to formatExpiration.ts reverted and the new tests kept, 3 of the 14 fail (Expected: "In less than a day" / Received: "In ", Expected: "Expired" / Received: "In ", and isExpired returning false for a key that expired 3 hours earlier).

oxfmt --check and oxlint pass on the two changed files.

Not run locally: the full front test suite, the type check, and the Storybook tests.

How to reproduce

  1. Create an API key whose expiresAt is a few hours from now (for example with the createApiKey mutation), and one whose expiresAt is a few hours in the past.
  2. Open Settings → Developers and look at the Expiration column, then open each key.

AI tools assisted with this change. I reproduced the bug and verified the fix manually in the running app.