gpt4 book ai didi

reactjs - Redux oidc 回调被调用两次

转载 作者:行者123 更新时间:2023-12-03 14:18:45 29 4
gpt4 key购买 nike

我的 React 容器如下所示:

class App extends React.Component {
componentDidMount() {
if (this.props.location && this.props.location.pathname != '/callback') {
userManager.getUser().then(response => {
if (!response || response.expired) {
userManager.signinRedirect();
}
else{
this.props.dispatch(userFound(response));
}
});
}
}
render() {
return (
<div>
<Switch>
<PrivateRoute exact path="/" component={HomePage} user={this.props.user} />
<PrivateRoute exact path="/dashboard" component={Dashboard} user={this.props.user} />
<Route exact path="/callback" component={CallbackPage} />
<Route component={NotFoundPage} />
</Switch>
</div>
);
}
}

回调组件如下所示:

class CallbackPage extends React.Component {
render() {
// just redirect to '/' in both cases
return (
<CallbackComponent
userManager={userManager}
successCallback={() => this.props.dispatch(push("/"))}
errorCallback={error => {
this.props.dispatch(push("/"));
console.error(error);
}}
>
<div>Redirecting...</div>
</CallbackComponent>
);
}
}

我的私有(private)路线如下所示:

const PrivateRoute = ({ component: Component, user, ...rest }) => (  
<Route {...rest} render={props => (
user ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: '/notFoundPage',
state: { from: props.location }
}}
/>
)
)} />
);

export default PrivateRoute;

我的商店看起来像:

export default function configureStore(initialState = {}, history) {

const middlewares = [
sagaMiddleware,
routerMiddleware(history),
];
const enhancers = [
applyMiddleware(...middlewares),
];
const composeEnhancers =
process.env.NODE_ENV !== 'production' &&
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// TODO Try to remove when `react-router-redux` is out of beta, LOCATION_CHANGE should not be fired more than once after hot reloading
// Prevent recomputing reducers for `replaceReducer`
shouldHotReload: false,
})
: compose;
const store = createStore(
createReducer(),
fromJS(initialState),
composeEnhancers(...enhancers)
);
store.runSaga = sagaMiddleware.run;
store.injectedReducers = {};
store.injectedSagas = {};
loadUser(store, userManager);
return store;
}

我的问题是:回调组件被调用两次。我找不到它从哪里触发?第一次,它会按预期转到成功函数,然后第二次,它将转到错误函数。然后页面卡住,浏览器中显示的 URL 是回调链接。我不明白为什么这个回调运行两次?有人可以帮我解决这个问题吗?我希望你理解这个问题。

此代码基于 redux-oidc 示例。请点击以下链接。 Redux-oidc example

最佳答案

你的路由器和 redux 存储配置看起来不错。但您不需要在 componentWillMount 中调用 getUser。您的应用程序应以这样的方式配置,即仅当存在有效用户时才呈现路由。

一个可能的解决方案是使用 render 而不是 componentWillMount,如下所示:

render() {
const routes = (
<Switch>
<Route exact path="/" component={HomePage} user={this.props.user} />
<Route exact path="/dashboard" component={Dashboard} user={this.props.user} />
<Route exact path="/callback" component={CallbackPage} />
<Route component={NotFoundPage} />
</Switch>
);

const loginPage = <LoginPage />; // <-- create a dedicated login page component where signinRedirect is called

const isValidUserPresent = this.props.user && !this.props.user.expired;
const isCallbackRouteRequested = this.props.location.pathname === '/callback';
const shouldRenderRoutes = isValidUserPresent || isCallbackRouteRequested;

return shouldRenderRoutes ? routes : loginPage;
}

关于reactjs - Redux oidc 回调被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50542672/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com