feat(auth): introduce demo token support and enhance API token handling
CI / changes (push) Successful in 10s
CI / commitlint (push) Has been skipped
CI / openapi (push) Successful in 28s
CI / web (push) Successful in 57s
CI / go (push) Successful in 1m9s
CI / bird2 (push) Successful in 17s
CI / release (push) Successful in 3m57s
CI / changes (push) Successful in 10s
CI / commitlint (push) Has been skipped
CI / openapi (push) Successful in 28s
CI / web (push) Successful in 57s
CI / go (push) Successful in 1m9s
CI / bird2 (push) Successful in 17s
CI / release (push) Successful in 3m57s
Added a local demo token for development purposes and improved the API token management by normalizing input tokens. Updated the authentication flow to utilize the new token handling, allowing for better session management and user experience. Enhanced the settings component to support the demo token and provide clear instructions for its use in local development.
This commit is contained in:
@@ -11,6 +11,9 @@ import type {
|
||||
|
||||
export const TOKEN_STORAGE_KEY = 'evobgp_api_token'
|
||||
|
||||
/** Локальный demo-токен (operator) при включённом demo-seed — см. docs/access.md */
|
||||
export const DEV_API_TOKEN = 'dev'
|
||||
|
||||
export type Problem = {
|
||||
type?: string
|
||||
title?: string
|
||||
@@ -18,14 +21,31 @@ export type Problem = {
|
||||
detail?: string
|
||||
}
|
||||
|
||||
/** Убирает пробелы и опциональный префикс Bearer (UI часто вставляет «Bearer dev»). */
|
||||
export function normalizeApiToken(raw: string): string {
|
||||
let t = raw.trim()
|
||||
if (/^bearer\s+/i.test(t)) {
|
||||
t = t.replace(/^bearer\s+/i, '').trim()
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
function getToken(): string | null {
|
||||
if (typeof window === 'undefined') return null
|
||||
return window.localStorage.getItem(TOKEN_STORAGE_KEY)
|
||||
const raw = window.localStorage.getItem(TOKEN_STORAGE_KEY)
|
||||
if (!raw) return null
|
||||
const normalized = normalizeApiToken(raw)
|
||||
return normalized || null
|
||||
}
|
||||
|
||||
export function setToken(token: string | null): void {
|
||||
if (typeof window === 'undefined') return
|
||||
if (token) window.localStorage.setItem(TOKEN_STORAGE_KEY, token)
|
||||
if (!token) {
|
||||
window.localStorage.removeItem(TOKEN_STORAGE_KEY)
|
||||
return
|
||||
}
|
||||
const normalized = normalizeApiToken(token)
|
||||
if (normalized) window.localStorage.setItem(TOKEN_STORAGE_KEY, normalized)
|
||||
else window.localStorage.removeItem(TOKEN_STORAGE_KEY)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user