mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 15:39:24 +08:00
refactor: remove unnecessary NavigateIfNotSignIn wrapper from AdminProvider (#6268)
* refactor: remove unnecessary NavigateIfNotSignIn wrapper from AdminProvider * refactor: remove NavigateToSigninWithRedirect and NavigateIfNotSignIn * refactor: streamline error handling in authCheckMiddleware * fix: prevent notification for user role errors in authCheckMiddleware * fix: use app error to show error message for USER_HAS_NO_ROLES_ERR in authCheckMiddleware
This commit is contained in:
parent
23aadf74e0
commit
8d52fc6c84
@ -29,7 +29,6 @@ import { useDataSourceKey } from '../data-source/data-source/DataSourceProvider'
|
||||
import { SchemaComponentOptions, useDesignable } from '../schema-component';
|
||||
|
||||
import { useApp } from '../application';
|
||||
import { NavigateToSigninWithRedirect } from '../user/CurrentUserProvider';
|
||||
|
||||
// 注意: 必须要对 useBlockRequestContext 进行引用,否则会导致 Data sources 页面报错,原因未知
|
||||
useBlockRequestContext;
|
||||
@ -89,9 +88,6 @@ export const ACLRolesCheckProvider = (props) => {
|
||||
if (result.loading) {
|
||||
return render();
|
||||
}
|
||||
if (result.error) {
|
||||
return <NavigateToSigninWithRedirect />;
|
||||
}
|
||||
return <ACLContext.Provider value={result}>{props.children}</ACLContext.Provider>;
|
||||
};
|
||||
|
||||
|
@ -28,7 +28,6 @@ import {
|
||||
CurrentAppInfoProvider,
|
||||
findByUid,
|
||||
findMenuItem,
|
||||
NavigateIfNotSignIn,
|
||||
PinnedPluginList,
|
||||
RemoteCollectionManagerProvider,
|
||||
RemoteSchemaComponent,
|
||||
@ -543,17 +542,15 @@ export const AdminProvider = (props) => {
|
||||
<CurrentPageUidProvider>
|
||||
<CurrentTabUidProvider>
|
||||
<IsSubPageClosedByPageMenuProvider>
|
||||
<NavigateIfNotSignIn>
|
||||
<ACLRolesCheckProvider>
|
||||
<MenuSchemaRequestProvider>
|
||||
<RemoteCollectionManagerProvider>
|
||||
<CurrentAppInfoProvider>
|
||||
<RemoteSchemaTemplateManagerProvider>{props.children}</RemoteSchemaTemplateManagerProvider>
|
||||
</CurrentAppInfoProvider>
|
||||
</RemoteCollectionManagerProvider>
|
||||
</MenuSchemaRequestProvider>
|
||||
</ACLRolesCheckProvider>
|
||||
</NavigateIfNotSignIn>
|
||||
<ACLRolesCheckProvider>
|
||||
<MenuSchemaRequestProvider>
|
||||
<RemoteCollectionManagerProvider>
|
||||
<CurrentAppInfoProvider>
|
||||
<RemoteSchemaTemplateManagerProvider>{props.children}</RemoteSchemaTemplateManagerProvider>
|
||||
</CurrentAppInfoProvider>
|
||||
</RemoteCollectionManagerProvider>
|
||||
</MenuSchemaRequestProvider>
|
||||
</ACLRolesCheckProvider>
|
||||
</IsSubPageClosedByPageMenuProvider>
|
||||
</CurrentTabUidProvider>
|
||||
</CurrentPageUidProvider>
|
||||
|
@ -8,10 +8,9 @@
|
||||
*/
|
||||
|
||||
import React, { createContext, useContext, useMemo } from 'react';
|
||||
import { Navigate } from 'react-router-dom';
|
||||
import { useACLRoleContext } from '../acl';
|
||||
import { ReturnTypeOfUseRequest, useAPIClient, useRequest } from '../api-client';
|
||||
import { useAppSpin, useLocationNoUpdate } from '../application';
|
||||
import { useAppSpin } from '../application';
|
||||
import { useCompile } from '../schema-component';
|
||||
|
||||
export const CurrentUserContext = createContext<ReturnTypeOfUseRequest>(null);
|
||||
@ -57,18 +56,3 @@ export const CurrentUserProvider = (props) => {
|
||||
|
||||
return <CurrentUserContext.Provider value={result}>{props.children}</CurrentUserContext.Provider>;
|
||||
};
|
||||
|
||||
export const NavigateToSigninWithRedirect = () => {
|
||||
const { pathname, search } = useLocationNoUpdate();
|
||||
const redirect = `?redirect=${pathname}${search}`;
|
||||
return <Navigate replace to={`/signin${redirect}`} />;
|
||||
};
|
||||
|
||||
export const NavigateIfNotSignIn = ({ children }) => {
|
||||
const result = useCurrentUserContext();
|
||||
|
||||
if (result.loading === false && !result.data?.data?.id) {
|
||||
return <NavigateToSigninWithRedirect />;
|
||||
}
|
||||
return <>{children}</>;
|
||||
};
|
||||
|
@ -50,36 +50,34 @@ export function authCheckMiddleware({ app }: { app: Application }) {
|
||||
};
|
||||
const errHandler = (error) => {
|
||||
const newToken = error?.response?.headers?.['x-new-token'];
|
||||
const errors = error?.response?.data?.errors;
|
||||
const firstError = Array.isArray(errors) ? errors[0] : null;
|
||||
|
||||
const state = app.router.state;
|
||||
const { pathname, search } = state.location;
|
||||
const basename = app.router.basename;
|
||||
|
||||
if (newToken) {
|
||||
app.apiClient.auth.setToken(newToken);
|
||||
}
|
||||
if (error.status === 401 && !error.config?.skipAuth) {
|
||||
const requestToken = error?.config?.headers?.Authorization?.replace(/^Bearer\s+/gi, '');
|
||||
const currentToken = app.apiClient.auth.getToken();
|
||||
// if (currentToken && currentToken !== requestToken) {
|
||||
// error.config.skipNotify = true;
|
||||
// return app.apiClient.request(error.config);
|
||||
// }
|
||||
app.apiClient.auth.setToken('');
|
||||
const errors = error?.response?.data?.errors;
|
||||
const firstError = Array.isArray(errors) ? errors[0] : null;
|
||||
if (!firstError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// if the code
|
||||
if (firstError?.code === AuthErrorCode.SKIP_TOKEN_RENEW) {
|
||||
throw error;
|
||||
if (error.status === 401) {
|
||||
app.apiClient.auth.setToken('');
|
||||
if (pathname === app.getHref('signin') && firstError?.code !== AuthErrorCode.EMPTY_TOKEN && error.config) {
|
||||
error.config.skipNotify = false;
|
||||
}
|
||||
|
||||
if (firstError?.code === 'USER_HAS_NO_ROLES_ERR') {
|
||||
// use app error to show error message
|
||||
error.config.skipNotify = true;
|
||||
app.error = firstError;
|
||||
}
|
||||
}
|
||||
|
||||
if (error.status === 401 && !error.config?.skipAuth) {
|
||||
if (!firstError || firstError?.code === AuthErrorCode.SKIP_TOKEN_RENEW) {
|
||||
throw error;
|
||||
}
|
||||
const state = app.router.state;
|
||||
const { pathname, search } = state.location;
|
||||
const basename = app.router.basename;
|
||||
|
||||
if (pathname !== app.getHref('signin')) {
|
||||
const redirectPath = removeBasename(pathname, basename);
|
||||
|
Loading…
x
Reference in New Issue
Block a user