Relayjs Graphql 用户认证 [英] Relayjs Graphql user authentication

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

问题描述

是否有可能仅通过 graphql 服务器结合中继 & 对具有不同角色的用户进行身份验证?有反应吗?

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 Web 令牌保护").

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

推荐答案

我在我的一个应用程序中做到了,基本上你只需要一个用户界面,如果没有人登录,这个在第一个根查询时返回 null,并且然后,您可以使用传入凭据的登录更改来更新它.主要问题是在 post 中继请求中获取 cookie 或 session,因为它不处理请求中的 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
      }
    }
    `;
  }
}

这是我的模式侧变异

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天全站免登陆