fix:无法设置token的问题
This commit is contained in:
+2
-2
@@ -5,8 +5,8 @@
|
||||
* 原始 Dart: lib/core/net/interceptor/auth_interceptor.dart
|
||||
*/
|
||||
import type { FetchHook } from "ofetch";
|
||||
import { ApiPath } from "../api_path";
|
||||
import { ApiError, ErrorCode } from "../api_result";
|
||||
import { ApiPath } from "../../../data/services/api/api_path";
|
||||
import { ApiError, ErrorCode } from "../../../data/services/api/api_result";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import { deviceIdentifier } from "@/utils/device_identifier";
|
||||
import { Result } from "@/utils/result";
|
||||
+40
-5
@@ -6,8 +6,8 @@
|
||||
* 原始 Dart: lib/core/net/interceptor/token_interceptor.dart + token_paths.dart
|
||||
*/
|
||||
import type { FetchHook } from "ofetch";
|
||||
import { ApiPath } from "../api_path";
|
||||
import { AuthStorage } from "../../../storage/auth/auth_storage";
|
||||
import { ApiPath } from "../../../data/services/api/api_path";
|
||||
import { AuthStorage } from "../../../data/storage/auth/auth_storage";
|
||||
|
||||
/**
|
||||
* 不需要 token 的路径
|
||||
@@ -47,22 +47,57 @@ async function getAuthToken(): Promise<string | null> {
|
||||
const storage = AuthStorage.getInstance();
|
||||
const loginR = await storage.getLoginToken();
|
||||
if (loginR.success && loginR.data && loginR.data.length > 0) {
|
||||
console.log("[token-interceptor] getAuthToken: using LOGIN token", {
|
||||
length: loginR.data.length,
|
||||
prefix: loginR.data.slice(0, 8),
|
||||
});
|
||||
return loginR.data;
|
||||
}
|
||||
console.log("[token-interceptor] getAuthToken: no login token", {
|
||||
success: loginR.success,
|
||||
hasData: loginR.success ? !!loginR.data : false,
|
||||
});
|
||||
|
||||
const guestR = await storage.getGuestToken();
|
||||
if (guestR.success && guestR.data && guestR.data.length > 0) {
|
||||
console.log(
|
||||
"[token-interceptor] getAuthToken: using GUEST token (login priority, no login available)",
|
||||
{
|
||||
length: guestR.data.length,
|
||||
prefix: guestR.data.slice(0, 8),
|
||||
},
|
||||
);
|
||||
return guestR.data;
|
||||
}
|
||||
console.warn("[token-interceptor] getAuthToken: NO token available (will skip Authorization)", {
|
||||
loginAttempted: true,
|
||||
guestAttempted: true,
|
||||
loginSuccess: loginR.success,
|
||||
guestSuccess: guestR.success,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
export const onRequestToken: FetchHook = async (ctx) => {
|
||||
const path = getRequestPath(ctx.request);
|
||||
if (!needsToken(path)) return;
|
||||
if (!needsToken(path)) {
|
||||
console.log("[token-interceptor] onRequestToken SKIP (no-token path)", { path });
|
||||
return;
|
||||
}
|
||||
|
||||
const token = await getAuthToken();
|
||||
if (token && ctx.request instanceof Request) {
|
||||
ctx.request.headers.set("Authorization", `Bearer ${token}`);
|
||||
if (token) {
|
||||
// ofetch 推荐:改 ctx.options.headers(**不**依赖 ctx.request 是 Request 实例 —— 实例类型不稳定)
|
||||
const headers = new Headers(ctx.options.headers);
|
||||
headers.set("Authorization", `Bearer ${token}`);
|
||||
ctx.options.headers = headers;
|
||||
console.log("[token-interceptor] onRequestToken SET Authorization header (via ctx.options.headers)", {
|
||||
path,
|
||||
length: token.length,
|
||||
prefix: token.slice(0, 8),
|
||||
});
|
||||
} else {
|
||||
console.warn("[token-interceptor] onRequestToken NO token to set", { path });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
import { ofetch, type $Fetch, type FetchHook } from "ofetch";
|
||||
import { ApiError, ErrorCode } from "./api_result";
|
||||
import { getApiConfig } from "../../../core/net/config/api_config";
|
||||
import { authRefreshInterceptor } from "./interceptor/auth_refresh_interceptor";
|
||||
import { loggingInterceptor } from "./interceptor/logging_interceptor";
|
||||
import { tokenInterceptor } from "./interceptor/token_interceptor";
|
||||
import { authRefreshInterceptor } from "../../../core/net/interceptor/auth_refresh_interceptor";
|
||||
import { loggingInterceptor } from "../../../core/net/interceptor/logging_interceptor";
|
||||
import { tokenInterceptor } from "../../../core/net/interceptor/token_interceptor";
|
||||
|
||||
/**
|
||||
* 合并多个同类型 hook 为单个 hook(按顺序执行)
|
||||
|
||||
@@ -10,6 +10,6 @@ export * from "./http_client";
|
||||
export * from "./metrics_api";
|
||||
export * from "./response_helper";
|
||||
export * from "./user_api";
|
||||
export * from "./interceptor/auth_refresh_interceptor";
|
||||
export * from "./interceptor/logging_interceptor";
|
||||
export * from "./interceptor/token_interceptor";
|
||||
export * from "../../../core/net/interceptor/auth_refresh_interceptor";
|
||||
export * from "../../../core/net/interceptor/logging_interceptor";
|
||||
export * from "../../../core/net/interceptor/token_interceptor";
|
||||
|
||||
@@ -11,9 +11,9 @@ export * from "./api/metrics_api";
|
||||
export * from "./api/response_helper";
|
||||
export * from "./api/user_api";
|
||||
export * from "../../core/net/config/api_config";
|
||||
export * from "./api/interceptor/auth_refresh_interceptor";
|
||||
export * from "./api/interceptor/logging_interceptor";
|
||||
export * from "./api/interceptor/token_interceptor";
|
||||
export * from "../../core/net/interceptor/auth_refresh_interceptor";
|
||||
export * from "../../core/net/interceptor/logging_interceptor";
|
||||
export * from "../../core/net/interceptor/token_interceptor";
|
||||
// 注:原 ./auth/* 平台适配层已删除(社交登录改由 NextAuth 处理)
|
||||
export * from "../dto/auth/apple_login_request";
|
||||
export * from "../dto/auth/facebook_login_request";
|
||||
|
||||
Reference in New Issue
Block a user