简体   繁体   English

在Relay Modern中处理身份验证

[英]Handling authentication in Relay Modern

I am using token-based authentication and wonder how to fully tie it together in Relay Modern. 我正在使用基于令牌的身份验证,并且想知道如何在Relay Modern中将其完全捆绑在一起。 I am halfway through. 我中途了。 Any help is much appreciated. 任何帮助深表感谢。 This is the setup I have: 这是我的设置:

  • At the top level inside <App /> I render the <QueryRenderer /> entry point. <App />的顶层,我渲染<QueryRenderer />入口点。
  • Inside <LoginPage /> component I have a form that triggers LoginUserMutation on submit. <LoginPage />组件内部,我有一个表单在提交时触发LoginUserMutation When I provide a valid username and password combination I receive back a token: 当我提供有效的用户名和密码组合时,我会收到令牌:
    • I save that token in localStorage via localStorage.setItem('token', token) 我通过localStorage.setItem('token', token)将令牌保存在localStorage中
    • I then want to redirect the user to the /dashboard route via history.push('/dashboard') 然后,我想通过history.push('/dashboard')将用户重定向到/dashboard路线
  • Inside createRelayEnvironment.js 's fetchQuery I define how to make a network request to the GraphQL backend: createRelayEnvironment.jsfetchQuery内部,我定义了如何向GraphQL后端发出网络请求:
    • Here I read whether or not I have a token stored in localStorage ie const token = localStorage.getItem('token'); 在这里,我读取是否在本地存储中存储了令牌,即const token = localStorage.getItem('token');
    • If the token is available I set the following header: { ..., 'authorization': 'Bearer ${token}' } . 如果令牌可用,则设置以下标头: { ..., 'authorization': 'Bearer ${token}' } The backend is then able to authenticate the user. 然后,后端便可以对用户进行身份验证。

Soooo goood so far. 到目前为止,太好了。 Unfortunately, there is one issue that is spoiling the picture. 不幸的是,有一个问题破坏了画面。 This setup works great when the app initializes after the token has already been stored into localStorage. 当令牌已存储到localStorage后,应用程序初始化时,此设置非常有用。 Not so though, if you are currently unauthenticated without a token entry in the localStorage. 但是不是这样,如果您当前未通过身份验证,并且在localStorage中没有令牌条目。

As mentioned before, imagine you are inside the LoginUserMutation , the mutation has been successful and you get a valid token back in the onComplete callback. 如前所述,假设您位于LoginUserMutation内部,该突变已成功完成,并且您在onComplete回调中获得了有效令牌。 Now what to do with it? 现在该怎么办? Yes I store it inside the localStorage . 是的,我将其存储在localStorage But if I redirect the user to the <Dashboard /> component I am screwed. 但是,如果我将用户重定向到<Dashboard />组件,则会很麻烦。 Why? 为什么? - the dashboard component needs restricted data. -仪表板组件需要受限的数据。 When the app first initializes though the restricted data is not provided as no token is being sent to the GraphQL endpoint. 当应用程序首次初始化时,尽管未提供受限数据,因为没有令牌发送到GraphQL端点。 Then later when the token is eventually available in the LoginUserMutation Relay does not actually do anything with it. 然后,当令牌最终在LoginUserMutation中继中可用时,中继实际上不会对其执行任何操作。

tl;dr tl; dr

Q1 How can I - equipped with a valid token - trigger a refetch of data required by the <Dashboard /> component, before I send the user to /dashboard . Q1我如何为用户配备有效的令牌-在将用户发送到/dashboard之前,触发<Dashboard />组件所需的数据的重新提取。

Q2 Is there a better way to handle authentication w/ Relay when using JSON Web Token (JWT)? Q2使用JSON Web令牌(JWT)时,是否有更好的方法来处理带有中继的身份验证? In particular, what to do from the point where you receive a valid token inside the onComplete callback of LoginUserMutation ? 特别是,从LoginUserMutationonComplete回调内收到有效令牌的那一点起,该怎么办?

When authentifying your user, re-creates the Relay environment. 对用户进行身份验证时,请重新创建中继环境。 You will drop any existing cache. 您将删除所有现有的缓存。 This is what is recommended as anyways your cache should be different depending on which user is logged in, so dropping it entirely is OK. 推荐这样做是因为无论如何,您的缓存都应根据登录的用户而有所不同,因此可以完全删除它。

Pseudo-code: 伪代码:

class App extends PureComponent {
  state = {
    environment: createRelayEnvironment(),
  };

  login = () => {
    doLoginMutation({
      onSuccess: () => this.setState({ environment: createRelayEnvironment() }),
    })
  }

  ...
}

After some tinkering found some solution which can work. 经过一番修补,找到了一些可行的解决方案。 I need to change my code to incorporate the below. 我需要更改代码以合并以下内容。 Check this one out: https://github.com/apollographql/apollo-fetch 看看这个: https : //github.com/apollographql/apollo-fetch

It can resolve the issue of middllewares with Fetch. 它可以通过Fetch解决中间件的问题。

Updates: I tried to make it work but was unsuccessful. 更新:我试图使其正常运行,但未成功。

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

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