javascript-React-admin-如果未通过身份验证,如何强制Dasbhoard要求登录

在管理组件中,我想要一个仪表板,如下所示:

<Admin dashboard={Dashboard}>
    <Resource name="list" list={MyList} />
</Admin>

如果用户尚未通过身份验证,如何强制仪表板要求登录?有没有简单的方法可以做到这一点?

最佳答案
当api端点返回401或403 http状态代码时,react-admin显示登录页面.
react-admin文档中的Authentication page

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>

每次用户导航时都会调用auth提供程序.
因此,您可以实现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.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...