简体   繁体   English

Flux + React.js登录/注册流程

[英]Flux + React.js Login/register flow

I'm looking for some guide how to organize best login/registration flow in my Flux+React.js app. 我正在寻找有关如何在Flux + React.js应用程序中组织最佳登录/注册流程的指南。

What I have is a REST API which while registration/login returns access_token which I can use in all other requests to get user data. 我拥有的是一个REST API,在注册/登录时会返回access_token ,我可以在所有其他请求中使用它来获取用户数据。 Very basic. 很基本。

I'm using ReactRouter and what comes to mind firstly is to use 2 <Route> 's for login pages and for secured ones, eg: 我正在使用ReactRouter,首先想到的是对登录页面和受保护的页面使用2 <Route> ,例如:

<Route name="app" handler={AppHandler} path="/">
  <Route name="auth" handler={AuthHandler}>
    <Route name="login" handler={RegisterHandler} path="/register"/>
    <Route name="register" handler={LoginHandler} path="/login"/>
  </Route>
  <Route name="secured" handler={EnsureAuthHandler}>
    <Route name="dashboard" handler={Dashboard} />
  </Route>
  <NotFoundRoute handler={NotFound}/>
</Route>

Inside EnsureAuthHandler I want to check if user has access token, if "NO" I would like to redirect him to login, if "YES" children route handler can fire actions to load its' data. EnsureAuthHandler我想检查用户是否具有访问令牌,如果要“否”,我想将他重定向到登录名,如果“是”,则子级路由处理程序可以触发操作以加载其数据。

Inside RegisterHandler and LoginHandler I have forms with several fields. RegisterHandlerLoginHandler我具有包含多个字段的表单。 I'm using one AuthStore and one AuthActions files. 我正在使用一个AuthStore和一个AuthActions文件。 On submit I'm firing smth like AuthActions.register(formData) and if submit fails form component gets errors to state from AuthStore but if no errors happen I redirect user to Dashboard . 在提交时我击发水木清华像AuthActions.register(formData)如果提交失败形式组件获得错误stateAuthStore但如果没有错误发生,我重定向用户Dashboard

I'm feeling like it's a bit overcomplicated? 我觉得有点复杂吗? For example using stores to save errors of login/register. 例如,使用商店来保存登录/注册错误。

Another point, how to intercept unauthorized API error in one place, addListener to Flux dispatcher? 另一点,如何在一个地方拦截未授权的API错误,即将addListener到Flux调度程序?

You can use callback function in React.run point of your application: 您可以在应用程序的React.run点中使用回调函数:

// Defaults to `Router.HashLocation`
// callback is called whenever the hash changes
Router.run(routes, callback);

// HTML5 History
// callback is called when history events happen
Router.run(routes, Router.HistoryLocation, callback);

// Server rendering
// callback is called once, immediately.
Router.run(routes, '/some/path', callback);

Which receives two arguments: 接收两个参数:

  1. Root
  2. state

Root - A ReactComponent class with the current match all wrapped up inside it, ready for you to render. -一个带有当前匹配项的ReactComponent类,所有内容都包装在其中,以供您渲染。

state - An object containing the matched state. 状态 -包含匹配状态的对象。

Just go to http://rackt.github.io/react-router/#Router.run 只需转到http://rackt.github.io/react-router/#Router.run

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

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