refactor: overhaul auth system, routing, and navigation flows
This commit overhauls several core parts of the codebase: - Replace legacy localStorage token storage with secure cookie-based authentication with automatic token refresh logic - Rewrite checkToken and goLogin hooks to support redirect targets and proper async error handling - Update all component navigation flows to use the updated auth checks and correct redirect behavior - Refactor router configuration to support both client-side and server-side rendering via conditional history usage - Remove unused speech recognition constant file and stale component registration for RecordingWaveBtn - Add unit tests for navigation utilities and auth token handling - Fix login post-success redirect logic and add login success event emissions - Overhaul HTTP request handling to include automatic auth token injection and retry on unauthorized errors
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { request } from "../utils/request";
|
||||
import { storeAuthTokens } from "@/constants/token";
|
||||
import type { OAuthTokenResponse } from "@/constants/token";
|
||||
import { request, requestRaw } from "../utils/request";
|
||||
|
||||
// 获取oauth token
|
||||
export interface OauthTokenRequest {
|
||||
@@ -28,18 +30,27 @@ export function buildFormUrlEncodedParams(
|
||||
return params;
|
||||
}
|
||||
|
||||
export function oauthToken(data: OauthTokenRequest) {
|
||||
export async function oauthToken(data: OauthTokenRequest): Promise<OAuthTokenResponse> {
|
||||
const params = buildFormUrlEncodedParams(data as Record<string, unknown>);
|
||||
|
||||
return request({
|
||||
url: "/auth/oauth2/token",
|
||||
method: "post",
|
||||
data: params,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: "Basic Y3VzdG9tOmN1c3RvbQ==",
|
||||
const res = await requestRaw<OAuthTokenResponse>(
|
||||
{
|
||||
url: "/auth/oauth2/token",
|
||||
method: "post",
|
||||
data: params,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: "Basic Y3VzdG9tOmN1c3RvbQ==",
|
||||
},
|
||||
},
|
||||
{ skipAuth: true },
|
||||
);
|
||||
|
||||
storeAuthTokens(res, {
|
||||
requireRefreshToken: data.grant_type !== "refresh_token",
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// 发送手机验证码
|
||||
@@ -50,7 +61,7 @@ export function sendCode(mobile: string) {
|
||||
return request({
|
||||
url: `/admin/mobile/${encoded}`,
|
||||
method: "get",
|
||||
});
|
||||
}, { skipAuth: true });
|
||||
}
|
||||
|
||||
// 检测用户是否绑定手机号
|
||||
|
||||
Reference in New Issue
Block a user