【问题标题】:Relayjs Graphql user authenticationRelayjs Graphql 用户认证
【发布时间】:2016-01-08 07:24:15
【问题描述】:

是否可以仅通过 graphql 服务器结合中继和反应来验证具有不同角色的用户?

我环顾四周,找不到关于这个主题的太多信息。

在我当前的设置中,具有不同角色的登录功能仍然通过传统的 REST API...(使用 json Web 令牌“保护”)。

【问题讨论】:

    标签: graphql relayjs


    【解决方案1】:

    我在我的一个应用程序中完成了它,基本上你只需要一个用户界面,如果没有人登录,这个用户界面在第一个根查询中返回 null,然后你可以使用传递凭据的登录突变来更新它。 主要问题是在 post 中继请求中获取 cookie 或 session,因为它不处理请求中的 cookie 字段。

    这是我的客户端突变:

     export default class LoginMutation extends Relay.Mutation {
      static fragments = {
        user: () => Relay.QL`
          fragment on User {
            id,
            mail
          }
        `,
      };
      getMutation() {
        return Relay.QL`mutation{Login}`;
      }
    
      getVariables() {
        return {
          mail: this.props.credentials.pseudo,
          password: this.props.credentials.password,
        };
      }
      getConfigs() {
        return [{
          type: 'FIELDS_CHANGE',
          fieldIDs: {
            user: this.props.user.id,
          }
        }];
      }
      getOptimisticResponse() {
        return {
          mail: this.props.credentials.pseudo,
        };
      }
      getFatQuery() {
        return Relay.QL`
        fragment on LoginPayload {
          user {
            userID,
            mail
          }
        }
        `;
      }
    }
    

    这是我的架构侧突变

    var LoginMutation = mutationWithClientMutationId({
      name: 'Login',
      inputFields: {
        mail: {
          type: new GraphQLNonNull(GraphQLString)
        },
        password: {
          type: new GraphQLNonNull(GraphQLString)
        }
      },
      outputFields: {
        user: {
          type: GraphQLUser,
          resolve: (newUser) => newUser
        }
      },
      mutateAndGetPayload: (credentials, {
        rootValue
      }) => co(function*() {
        var newUser = yield getUserByCredentials(credentials, rootValue);
        console.log('schema:loginmutation');
        delete newUser.id;
        return newUser;
      })
    });
    

    为了让我的用户通过页面刷新保持登录状态,我发送自己的请求并用 cookie 字段填充它...这是目前使其工作的唯一方法...

    【讨论】:

    • 谢谢,我明天试试。您是否在 getUserByCredentials 函数中封装了“我发送自己的请求并用 cookie 字段填充”?
    • 其实这确实是relay的问题,post请求下你没有访问cookies的权限。我等到突变返回客户端,然后发送带有凭据的全新自制获取请求以填充我的 cookie...,然后在刷新时通过 html 发送用户 ID,并在根查询中传入该用户 ID。跨度>
    • 在没有重置功能(Relay currently doesn't have)的情况下,通过 Relay 本身执行此操作可能不是一个好主意。因为 Relay 在内部缓存 GraphQL 数据,并且该数据可能是特定于用户的,所以您需要一种方法在注销 登录时使该缓存无效。目前,我们建议人们在其中任何一个事件发生时进行整页刷新。
    猜你喜欢
    • 2020-05-12
    • 1970-01-01
    • 2022-10-20
    • 2017-05-01
    • 2017-11-04
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多