From 7c3c1bbd6a228a2f1e99a35d27f060f8e66fe04d Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 3 Nov 2025 23:05:44 +0800 Subject: [PATCH] show example after api token generated --- pkg/api/tokens.go | 3 +- pkg/models/token_record.go | 3 +- src/locales/de.json | 1 + src/locales/en.json | 1 + src/locales/es.json | 1 + src/locales/fr.json | 1 + src/locales/it.json | 1 + src/locales/ja.json | 1 + src/locales/ko.json | 1 + src/locales/nl.json | 1 + src/locales/pt_BR.json | 1 + src/locales/ru.json | 1 + src/locales/th.json | 1 + src/locales/uk.json | 1 + src/locales/vi.json | 1 + src/locales/zh_Hans.json | 1 + src/locales/zh_Hant.json | 1 + src/models/token.ts | 1 + .../dialogs/UserGenerateTokenDialog.vue | 34 ++++++++++++++++--- 19 files changed, 49 insertions(+), 7 deletions(-) diff --git a/pkg/api/tokens.go b/pkg/api/tokens.go index e5a03a28..596a527a 100644 --- a/pkg/api/tokens.go +++ b/pkg/api/tokens.go @@ -123,7 +123,8 @@ func (a *TokensApi) TokenGenerateAPIHandler(c *core.WebContext) (any, *errs.Erro log.Infof(c, "[tokens.TokenGenerateAPIHandler] user \"uid:%d\" has generated api token, new token will be expired at %d", user.Uid, claims.ExpiresAt) generateAPITokenResp := &models.TokenGenerateAPIResponse{ - Token: token, + Token: token, + APIBaseUrl: a.CurrentConfig().RootUrl + "api", } return generateAPITokenResp, nil diff --git a/pkg/models/token_record.go b/pkg/models/token_record.go index f138f9f9..39da4e9a 100644 --- a/pkg/models/token_record.go +++ b/pkg/models/token_record.go @@ -44,7 +44,8 @@ type TokenRevokeRequest struct { // TokenGenerateAPIResponse represents all response parameters of generated api token type TokenGenerateAPIResponse struct { - Token string `json:"token"` + Token string `json:"token"` + APIBaseUrl string `json:"apiBaseUrl"` } // TokenGenerateMCPResponse represents all response parameters of generated mcp token diff --git a/src/locales/de.json b/src/locales/de.json index 12319f7c..bdb4733f 100644 --- a/src/locales/de.json +++ b/src/locales/de.json @@ -1453,6 +1453,7 @@ "Disable": "Deaktivieren", "Disabled": "Deaktiviert", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Kopieren", "Visible": "Sichtbar", diff --git a/src/locales/en.json b/src/locales/en.json index 0cdac330..8b9a3a98 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -1453,6 +1453,7 @@ "Disable": "Disable", "Disabled": "Disabled", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Copy", "Visible": "Visible", diff --git a/src/locales/es.json b/src/locales/es.json index 60298f87..a224279e 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -1453,6 +1453,7 @@ "Disable": "Desactivar", "Disabled": "Desactivado", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Copiar", "Visible": "Visible", diff --git a/src/locales/fr.json b/src/locales/fr.json index 079838f5..668554de 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -1453,6 +1453,7 @@ "Disable": "Désactiver", "Disabled": "Désactivé", "Configuration": "Configuration", + "Example": "Example", "Token": "Jeton", "Copy": "Copier", "Visible": "Visible", diff --git a/src/locales/it.json b/src/locales/it.json index 433bfc28..fa74dbaf 100644 --- a/src/locales/it.json +++ b/src/locales/it.json @@ -1453,6 +1453,7 @@ "Disable": "Disabilita", "Disabled": "Disabilitato", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Copia", "Visible": "Visibile", diff --git a/src/locales/ja.json b/src/locales/ja.json index 9f9b8e5c..7b7fc823 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -1453,6 +1453,7 @@ "Disable": "無効", "Disabled": "無効になっています", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "コピー", "Visible": "見える", diff --git a/src/locales/ko.json b/src/locales/ko.json index 87556979..28cbdc5b 100644 --- a/src/locales/ko.json +++ b/src/locales/ko.json @@ -1453,6 +1453,7 @@ "Disable": "비활성화", "Disabled": "비활성화됨", "Configuration": "설정", + "Example": "Example", "Token": "토큰", "Copy": "복사", "Visible": "표시", diff --git a/src/locales/nl.json b/src/locales/nl.json index 0ee5d3a1..91cd343c 100644 --- a/src/locales/nl.json +++ b/src/locales/nl.json @@ -1453,6 +1453,7 @@ "Disable": "Uitschakelen", "Disabled": "Uitgeschakeld", "Configuration": "Configuratie", + "Example": "Example", "Token": "Token", "Copy": "Kopiëren", "Visible": "Zichtbaar", diff --git a/src/locales/pt_BR.json b/src/locales/pt_BR.json index 1a5d90dd..e04029ae 100644 --- a/src/locales/pt_BR.json +++ b/src/locales/pt_BR.json @@ -1453,6 +1453,7 @@ "Disable": "Desabilitar", "Disabled": "Desabilitado", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Copiar", "Visible": "Visível", diff --git a/src/locales/ru.json b/src/locales/ru.json index b2c64cb4..e94e8fe6 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -1453,6 +1453,7 @@ "Disable": "Отключить", "Disabled": "Отключено", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Копировать", "Visible": "Видимый", diff --git a/src/locales/th.json b/src/locales/th.json index 6aa264d9..d9d5737e 100644 --- a/src/locales/th.json +++ b/src/locales/th.json @@ -1453,6 +1453,7 @@ "Disable": "ปิดใช้งาน", "Disabled": "ปิดใช้งานแล้ว", "Configuration": "การตั้งค่า", + "Example": "Example", "Token": "โทเค็น", "Copy": "คัดลอก", "Visible": "มองเห็น", diff --git a/src/locales/uk.json b/src/locales/uk.json index 7bf02dc2..5f2b4523 100644 --- a/src/locales/uk.json +++ b/src/locales/uk.json @@ -1453,6 +1453,7 @@ "Disable": "Вимкнути", "Disabled": "Вимкнено", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Копіювати", "Visible": "Видимий", diff --git a/src/locales/vi.json b/src/locales/vi.json index 42deeef7..64518217 100644 --- a/src/locales/vi.json +++ b/src/locales/vi.json @@ -1453,6 +1453,7 @@ "Disable": "Tắt", "Disabled": "Đã tắt", "Configuration": "Configuration", + "Example": "Example", "Token": "Token", "Copy": "Sao chép", "Visible": "Hiển thị", diff --git a/src/locales/zh_Hans.json b/src/locales/zh_Hans.json index bd73dc5f..ec274b74 100644 --- a/src/locales/zh_Hans.json +++ b/src/locales/zh_Hans.json @@ -1453,6 +1453,7 @@ "Disable": "禁用", "Disabled": "禁用", "Configuration": "配置", + "Example": "示例", "Token": "令牌", "Copy": "复制", "Visible": "可见", diff --git a/src/locales/zh_Hant.json b/src/locales/zh_Hant.json index 882f13e4..4787d605 100644 --- a/src/locales/zh_Hant.json +++ b/src/locales/zh_Hant.json @@ -1453,6 +1453,7 @@ "Disable": "停用", "Disabled": "停用", "Configuration": "設定", + "Example": "範例", "Token": "令牌", "Copy": "複製", "Visible": "可見", diff --git a/src/models/token.ts b/src/models/token.ts index c9304c5d..490f403b 100644 --- a/src/models/token.ts +++ b/src/models/token.ts @@ -21,6 +21,7 @@ export interface TokenRevokeRequest { export interface TokenGenerateAPIResponse { readonly token: string; + readonly apiBaseUrl: string; } export interface TokenGenerateMCPResponse { diff --git a/src/views/desktop/user/settings/dialogs/UserGenerateTokenDialog.vue b/src/views/desktop/user/settings/dialogs/UserGenerateTokenDialog.vue index 8e180727..a57e9f53 100644 --- a/src/views/desktop/user/settings/dialogs/UserGenerateTokenDialog.vue +++ b/src/views/desktop/user/settings/dialogs/UserGenerateTokenDialog.vue @@ -7,6 +7,17 @@ + + + + + + +
+ :rows="4" :value="generatedToken" v-if="(tokenType === 'api' && (!showAPIExample || !serverUrl)) || (tokenType === 'mcp' && (!showMCPConfiguration || !serverUrl))" /> + :rows="5" :value="apiExample" v-if="tokenType === 'api' && showAPIExample && serverUrl" /> +
@@ -121,7 +135,7 @@ import { useI18n } from '@/locales/helpers.ts'; import { useTokensStore } from '@/stores/token.ts'; import { type NameValue } from '@/core/base.ts'; -import { type TokenGenerateMCPResponse } from '@/models/token.ts'; +import { type TokenGenerateAPIResponse, type TokenGenerateMCPResponse } from '@/models/token.ts'; import { isAPITokenEnabled, isMCPServerEnabled } from '@/lib/server_settings.ts'; import { copyTextToClipboard } from '@/lib/ui/common.ts'; @@ -144,6 +158,7 @@ const tokenExpirationTime = ref(86400); const tokenCustomExpirationTime = ref(86400); const currentPassword = ref(''); const generating = ref(false); +const showAPIExample = ref(false); const showMCPConfiguration = ref(false); const serverUrl = ref(''); const generatedToken = ref(''); @@ -162,6 +177,10 @@ const tokenTypeOptions = computed(() => { return options; }); +const apiExample = computed(() => { + return `curl -H 'Authorization: Bearer ${generatedToken.value}' '${serverUrl.value}/v1/users/profile/get.json'`; +}); + const mcpServerConfiguration = computed(() => { return '{\n' + ' "mcpServers": {\n' + @@ -183,6 +202,7 @@ function open(): Promise { tokenExpirationTime.value = 86400; tokenCustomExpirationTime.value = 86400; generating.value = false; + showAPIExample.value = false; showMCPConfiguration.value = false; serverUrl.value = ''; generatedToken.value = ''; @@ -208,7 +228,9 @@ function generateToken(): void { generating.value = false; currentPassword.value = ''; - if (tokenType.value === 'mcp') { + if (tokenType.value === 'api') { + serverUrl.value = (result as TokenGenerateAPIResponse).apiBaseUrl; + } else if (tokenType.value === 'mcp') { serverUrl.value = (result as TokenGenerateMCPResponse).mcpUrl; } @@ -223,7 +245,9 @@ function generateToken(): void { } function copy(): void { - if (showMCPConfiguration.value) { + if (tokenType.value === 'api' && showAPIExample.value) { + copyTextToClipboard(apiExample.value, buttonContainer.value); + } else if (tokenType.value === 'mcp' && showMCPConfiguration.value) { copyTextToClipboard(mcpServerConfiguration.value, buttonContainer.value); } else { copyTextToClipboard(generatedToken.value, buttonContainer.value);