- 新增 dev-only 且仅限本机访问的 admin 免登入口 - 管理端支持通过 ?devLogin=admin 自动换取登录态并清理 URL 参数 - 删除未受保护的临时 token 接口并补充关键单测
40 lines
1009 B
TypeScript
40 lines
1009 B
TypeScript
import type { LocationQuery, LocationQueryRaw } from 'vue-router';
|
|
|
|
const DEV_LOGIN_ACCOUNT = 'admin';
|
|
const DEV_LOGIN_QUERY_KEY = 'devLogin';
|
|
|
|
function normalizeQueryValue(
|
|
value: LocationQuery[string] | LocationQueryRaw[string],
|
|
) {
|
|
if (Array.isArray(value)) {
|
|
return value[0] ?? null;
|
|
}
|
|
return value ?? null;
|
|
}
|
|
|
|
export function getDevLoginAccount(query: LocationQuery | LocationQueryRaw) {
|
|
const value = normalizeQueryValue(query[DEV_LOGIN_QUERY_KEY]);
|
|
if (typeof value !== 'string') {
|
|
return null;
|
|
}
|
|
const account = value.trim();
|
|
return account.length > 0 ? account : null;
|
|
}
|
|
|
|
export function removeDevLoginQuery(query: LocationQuery | LocationQueryRaw) {
|
|
const { [DEV_LOGIN_QUERY_KEY]: _ignored, ...nextQuery } = query;
|
|
return nextQuery;
|
|
}
|
|
|
|
export function shouldAttemptDevLogin(params: {
|
|
account: null | string;
|
|
hasAccessToken: boolean;
|
|
isDev: boolean;
|
|
}) {
|
|
return (
|
|
params.isDev &&
|
|
!params.hasAccessToken &&
|
|
params.account === DEV_LOGIN_ACCOUNT
|
|
);
|
|
}
|