简体   繁体   English

反应Redux认证

[英]React Redux authentication

I have the following situation, I have a REST API and a React+Redux application. 我有以下情况,我有一个REST API和一个React + Redux应用程序。

When the user login I'm saving the token to the localStorage, based on that, if the user comes back to the page I make a call to the server /api/whoami { token } with the token, there I validate the token and return the user details which I will use to update my Redux store. 当用户登录时,我将令牌保存到localStorage,基于此,如果用户返回该页面,则使用令牌调用服务器/api/whoami { token } ,在那里我验证令牌并返回用于更新Redux存储的用户详细信息。

My problem is that if I refresh and I want to do something in componentDidMount , even if the user is logged in the Redux store isn't updated yet because the request to /api/whoami is not finished. 我的问题是,如果刷新并且我想在componentDidMount做某事,即使用户登录Redux存储区尚未更新,因为对/api/whoami的请求尚未完成。

How can I wait until the request to /api/whoami is done and the user is set then render my application? 我怎样才能等到对/api/whoami的请求完成并且设置了用户然后呈现我的应用程序?

Is there any other solution for this kind of issue? 对于此类问题还有其他解决方案吗?

Thanks! 谢谢!

You can use, what is called as an Aync action in your case, and show a loading component while the details are fetched. 在您的情况下,可以使用所谓的Aync操作 ,并在获取详细信息时显示加载组件。

Here you will have to use the thunk middleware . 在这里,您将不得不使用笨拙的中间件 It comes in a separate package called redux-thunk . 它包含在一个单独的名为redux-thunk软件包中。 When an action creator returns a function, that function will get executed by the Redux Thunk middleware. 当动作创建者返回一个函数时,该函数将由Redux Thunk中间件执行。 Read in more detail here 这里详细阅读

Functioning: Before you start the fetch operation, dispatch and action which sets the isLoading to true and after you have received your response you dispatch another action which will set isLoading: false and on your display just put a condition that if the isLoading is true you return a loading component else you return the profile component 运行功能:在开始进行提取操作之前,将isLoading设置为true分派和操作,在收到响应后,您将分派另一个将isLoading: false设置为isLoading: false并且在显示中仅提出以下条件:如果isLoading为true返回加载组件,否则返回配置文件组件

I suppose that you know react-redux and you are familiar to some concepts like action , store , reducer . 我想您知道react-redux并且熟悉一些概念,例如actionstorereducer

How can I wait until the request to /api/whoami is done? 如何等待/api/whoami的请求完成?

Use a middleware like redux-thunk , [redux-saga][1] ... i recommend you to use redux-saga because it is a library that aims to make side effects (ie asynchronous things like data fetching and impure things like accessing the browser cache) in React/Redux applications easier and better. 使用redux-thunk[redux-saga][1]类的中间件 ...我建议您使用redux-saga因为它是一个旨在产生副作用的库(例如,异步事物(如数据获取和不纯净的事物,如访问)浏览器缓存)在React / Redux应用程序中变得越来越容易。

How the user is set then render my application? 如何设置用户然后呈现我的应用程序?

With using middleware , you can render the loading view while the request is running and render what you want when the request has finished. 通过使用中间件 ,您可以在请求运行时呈现加载视图,并在请求完成后呈现所需的内容。

Read redux-saga offical document for more infomation and usage example. 阅读redux-saga官方文档了解更多信息和用法示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM