Relayjs Graphql用户身份验证 [英] Relayjs Graphql user authentication

查看:68
本文介绍了Relayjs Graphql用户身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅通过graphql服务器并与Relay& amp; amp;结合使用来验证具有不同角色的用户有反应吗?

Is it possible to authenticate users with different roles solely trough a graphql server in combination with relay & react?

我环顾四周,找不到关于该主题的太多信息.

I looked around, and couldn't find much info about this topic.

在我当前的设置中,具有不同角色的登录功能仍在通过传统的REST API ...(使用json网络令牌进行保护").

In my current setup, the login features with different roles, are still going trough a traditional REST API... ('secured' with json web tokens).

推荐答案

我是在我的一个应用中完成的,基本上,您只需要一个用户界面,如果没有人登录,则该界面在第一个根查询上返回null,并且然后,您可以通过传递登录凭据的登录突变来更新它.主要问题是在中继后请求中获取cookie或会话,因为它不处理请求中的cookie字段.

I did it in one of my app, basically you just need a User Interface, this one return null on the first root query if nobody is logged in, and you can then update it with a login mutation passing in the credentials. The main problem is to get cookies or session inside the post relay request since it does'nt handle the cookie field in the request.

这是我的客户突变:

 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
      }
    }
    `;
  }
}

这是我的架构方面的变异

and here is my schema side mutation

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字段...这是目前使其正常工作的唯一方法...

to keep my users logged through page refresh I send my own request and fill it with a cookie field... This is for now the only way to make it work...

这篇关于Relayjs Graphql用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆