在 Admin 组件中,我希望有一个像这样的仪表板:
<Admin dashboard={Dashboard}>
<Resource name="list" list={MyList} />
</Admin>
如果用户尚未通过身份验证,我如何强制仪表板要求登录?有没有一种简单的方法可以实现这一点?
最佳答案
当 api 端点返回 401 或 403 http 状态码时,react-admin 将显示登录页面。 来自Authentication page在 react-admin 文档中:
By default, an react-admin app doesn’t require authentication. But if the REST API ever returns a 401 (Unauthorized) or a 403 (Forbidden) response, then the user is redirected to the /login route. You have nothing to do - it’s already built in.
身份验证由 authProvider 属性配置。
<Admin dashboard={Dashboard} authProvider={authProvider}>
<Resource name="list" list={MyList} />
</Admin>
每次用户导航时都会调用身份验证提供程序。 因此,您可以实现您的 authProvider,它检查用户是否已登录或路由到登录页面。
来自Authentication page - Checking Credentials During Navigation :
Redirecting to the login page whenever a REST response uses a 401 status code is usually not enough, because react-admin keeps data on the client side, and could display stale data while contacting the server - even after the credentials are no longer valid.
Fortunately, each time the user navigates, react-admin calls the authProvider with the AUTH_CHECK type, so it’s the ideal place to check for credentials.
关于javascript - React-admin - 如果未通过身份验证,我如何强制 Dasbhoard 要求登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54826921/