[PR #5599] [MERGED] fix(settings): normalize API token timestamps #5922

Closed
opened 2026-11-02 04:05:03 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5599
Author: @Tomilla
Created: 6/27/2026
Status: ✅ Merged
Merged: 6/27/2026
Merged by: @MHSanaei

Base: main ← Head: fix/api-token-timestamps


📝 Commits (2)

  • e56e317 fix(settings): normalize API token timestamps
  • 98f4c12 refactor(api-token): share timestamp threshold

📊 Changes

7 files changed (+141 additions, -4 deletions)

View changed files

📝 frontend/src/pages/settings/SecurityTab.tsx (+8 -2)
➕ frontend/src/test/api-token-date.test.tsx (+36 -0)
➕ internal/database/api_token_timestamp_test.go (+49 -0)
📝 internal/database/db.go (+12 -0)
📝 internal/database/model/model.go (+5 -1)
📝 internal/web/service/panel/api_token.go (+8 -1)
➕ internal/web/service/panel/api_token_test.go (+23 -0)

📄 Description

Summary

Fix incorrect API-token creation dates in Panel Settings → Authentication → API Token. API-token timestamps now consistently use Unix seconds while remaining compatible with legacy millisecond values.

Why

The API contract and frontend expected ApiToken.createdAt in Unix seconds, but the database model used autoCreateTime:milli.

For example, the backend returned:

1782485394270

The frontend multiplied this value by 1,000 again, producing a date thousands of years in the future instead of:

06/26/2026, 22:49:54

This PR restores the documented seconds-based contract and safely handles data created by affected versions.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

Automated validation

The following commands passed locally:

  • go build ./...
  • go test ./...
  • npm run lint
  • npm run typecheck
  • npm run test — 40 test files and 589 tests passed
  • npm run build

Added regression coverage

  • Added a frontend test covering both:
    • Unix seconds: 1782485394
    • Legacy Unix milliseconds: 1782485394270
  • Added a backend unit test for API response normalization.
  • Added a database migration test confirming that:
    • Legacy millisecond values are converted to seconds.
    • Existing seconds values remain unchanged.
    • Running the migration multiple times is safe.

Real database test

Tested locally with a copy of an existing x-ui.db containing:

created_at = 1782485394270

After backend startup, the copied database contained:

created_at = 1782485394

The authenticated endpoint:

GET /panel/api/setting/apiTokens

returned:

{
  "success": true,
  "msg": "",
  "obj": [
    {
      "id": 2,
      "name": "central-panel",
      "enabled": true,
      "createdAt": 1782485394
    }
  ]
}

The frontend was tested through the Vite development server in a Chromium-based browser.

On Panel Settings → Authentication → API Token, the token rendered as:

central-panel — 06/26/2026, 22:49:54

The original database was not modified.

Screenshots / recordings

Screenshot of Authentication → API Token Datetime Bug:
image

image

Fixed:
image

Breaking changes

None expected.

  • Newly created API-token timestamps are stored in Unix seconds.
  • Existing millisecond values are automatically migrated to seconds during startup.
  • The migration is idempotent.
  • The backend also normalizes remaining legacy millisecond values before returning them.
  • The frontend accepts both seconds and milliseconds for compatibility with older panel versions.
  • The existing API documentation already defines createdAt using Unix seconds.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I verified the Wiki / README / API docs; the existing API contract already documents seconds.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5599 **Author:** [@Tomilla](https://github.com/Tomilla) **Created:** 6/27/2026 **Status:** ✅ Merged **Merged:** 6/27/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/api-token-timestamps` --- ### 📝 Commits (2) - [`e56e317`](https://github.com/MHSanaei/3x-ui/commit/e56e317f1c1c22847558c9b49627f4b01f3010f6) fix(settings): normalize API token timestamps - [`98f4c12`](https://github.com/MHSanaei/3x-ui/commit/98f4c12bbc1c5301f0922c1cfb5cd62f4b6fb1eb) refactor(api-token): share timestamp threshold ### 📊 Changes **7 files changed** (+141 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/pages/settings/SecurityTab.tsx` (+8 -2) ➕ `frontend/src/test/api-token-date.test.tsx` (+36 -0) ➕ `internal/database/api_token_timestamp_test.go` (+49 -0) 📝 `internal/database/db.go` (+12 -0) 📝 `internal/database/model/model.go` (+5 -1) 📝 `internal/web/service/panel/api_token.go` (+8 -1) ➕ `internal/web/service/panel/api_token_test.go` (+23 -0) </details> ### 📄 Description ## Summary Fix incorrect API-token creation dates in Panel Settings → Authentication → API Token. API-token timestamps now consistently use Unix seconds while remaining compatible with legacy millisecond values. ## Why The API contract and frontend expected `ApiToken.createdAt` in Unix seconds, but the database model used `autoCreateTime:milli`. For example, the backend returned: `1782485394270` The frontend multiplied this value by 1,000 again, producing a date thousands of years in the future instead of: `06/26/2026, 22:49:54` This PR restores the documented seconds-based contract and safely handles data created by affected versions. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? ### Automated validation The following commands passed locally: - `go build ./...` - `go test ./...` - `npm run lint` - `npm run typecheck` - `npm run test` — 40 test files and 589 tests passed - `npm run build` ### Added regression coverage - Added a frontend test covering both: - Unix seconds: `1782485394` - Legacy Unix milliseconds: `1782485394270` - Added a backend unit test for API response normalization. - Added a database migration test confirming that: - Legacy millisecond values are converted to seconds. - Existing seconds values remain unchanged. - Running the migration multiple times is safe. ### Real database test Tested locally with a copy of an existing `x-ui.db` containing: `created_at = 1782485394270` After backend startup, the copied database contained: `created_at = 1782485394` The authenticated endpoint: `GET /panel/api/setting/apiTokens` returned: ```json { "success": true, "msg": "", "obj": [ { "id": 2, "name": "central-panel", "enabled": true, "createdAt": 1782485394 } ] } ``` The frontend was tested through the Vite development server in a Chromium-based browser. On Panel Settings → Authentication → API Token, the token rendered as: `central-panel — 06/26/2026, 22:49:54` The original database was not modified. ## Screenshots / recordings Screenshot of Authentication → API Token Datetime Bug: <img width="2938" height="1668" alt="image" src="https://github.com/user-attachments/assets/e9873253-8d43-41e3-a567-800eeab7c4a5" /> <img width="2484" height="1282" alt="image" src="https://github.com/user-attachments/assets/3ceb7ac6-5a5f-498c-9ec5-3f52d23056c7" /> Fixed: <img width="2938" height="1742" alt="image" src="https://github.com/user-attachments/assets/dcfb80ab-f24b-48e5-a600-6be46882fc54" /> ## Breaking changes None expected. - Newly created API-token timestamps are stored in Unix seconds. - Existing millisecond values are automatically migrated to seconds during startup. - The migration is idempotent. - The backend also normalizes remaining legacy millisecond values before returning them. - The frontend accepts both seconds and milliseconds for compatibility with older panel versions. - The existing API documentation already defines `createdAt` using Unix seconds. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I verified the Wiki / README / API docs; the existing API contract already documents seconds. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:03 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5922
No description provided.