我有一个使用redux-oidc的登录系统,其中keycloak作为 ID 管理器。该应用程序的登录页面是登录屏幕。当用户单击登录按钮时,他们会被引导使用 keycloak 登录。在登录时,它们会被重定向回我们的应用程序。问题是登录屏幕在被带到应用程序之前会短暂闪烁。我们正试图摆脱这种情况。以下是页面从重定向加载后触发的 redux 操作:
我可以在这里看到登录屏幕闪烁的原因 -LOADING_USER设置state.oidc.isLoadingUser为 true,但USER_EXPIRED将其设置为 false。然后USER_FOUND设置用户,我们LOGIN_SUCCESS将所有用户/会话数据保存在存储中并调用state.auth.authenticatedtrue。在从重定向加载页面时,有一个短暂的时刻,我没有用户数据和身份验证,并且无法知道 oidc 客户端仍在加载我的用户。
一旦已经登录,页面重新加载就不是这种情况。重新加载时,我得到这个:
这是有道理的,因为用户数据已经存在,所以要么state.oidc.isLoadingUser或要么state.oidc.user是真的。但我希望这在重定向的第一页加载时是相同的,因为用户数据是使用redirect_uri. 看一下代码,我们在 store 定义后立即调用redux-oidc's :loadUser
// in index.js
const store = createStore(rootReducer, middleware)
loadUser(store, userManager);
userManager据我所知,我们正在以一种非常标准的方式进行设置:
const userManagerConfig = {
authority: `${window.location.protocol}//${window.location.hostname}:8081/auth/realms/master`,
client_id: "myclientid",
redirect_uri: `${window.location.protocol}//${window.location.hostname}${
window.location.port ? `:${window.location.port}` : ""
}${window.location.pathname}`,
response_type: "code",
scope: "openid",
automaticSilentRenew: true,
accessTokenExpiringNotificationTime: 20,
revokeAccessTokenOnSignout: true,
post_logout_redirect_uri: `${window.location.protocol}//${
window.location.hostname
}${window.location.port ? `:${window.location.port}` : ""}`
};
回调组件是它LoginPage本身:
// LoginScreen.tsx
<CallbackComponent userManager={userManager} >
<div>
<h3>Log me in Scotty</h3>
<Button onClick={() => { userManager.signinRedirect()} } >
Login
</Button>
</div>
</CallbackComponent>
在应用程序中,我们仅在用户为authenticated. 否则,我们渲染登录屏幕:
// App.tsx
const App = () => {
if (authenticated) {
return (
<Authenticated_App_With_All_The_Routes />
);
}
return <LoginPage />;
}
oidc 和 live 之间的连接authenticated在我们的 sagas 中USER_FOUND被拦截,我们调用后续动作将有效负载传递给 store 并设置authenticated为 true。
也许这是 TMI,但我想给出这个问题的全部范围。我相信它的核心是,由于某种原因,redux-oidc 正在调用USER_EXPIRED,然后调用USER_FOUND一次 keycloak 重定向回我们的应用程序。为什么?我如何跟踪我们仍在等待 redux-oidc 找到用户这一事实的页面加载?我需要让我的应用程序(和用户)知道您刚刚通过第三方登录,等待我们将您重定向到该应用程序。我是 OIDC 的新手,所以如果我遗漏了一些明显的问题,请原谅我。谢谢阅读。

