首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >relayjs:使用中继进行身份验证,使用哪种变异?

relayjs:使用中继进行身份验证,使用哪种变异?
EN

Stack Overflow用户
提问于 2015-09-12 12:25:23
回答 3查看 3.2K关注 0票数 13

我目前正在使用DefaultNetworkLayer中的自定义标头集处理中继之外的身份验证。它是首选的方式吗?有没有在接力赛中这样做的方法?当我尝试在中继中实现注册功能时,我被卡住了:在中继中,有以下配置: FIELDS_CHANGE,NODE_DELETE,RANGE_ADD,RANGE_DELETE。所以RANGE_ADD在这里是唯一适用的,但是我需要一个父级和连接,这对于新创建的用户是没有的……

EN

回答 3

Stack Overflow用户

发布于 2016-01-24 18:23:20

我在我的项目中也遇到了这个问题,下面是我如何处理它的。我使用这些字段创建了一个用户模式

代码语言:javascript
复制
export var GraphQLUser = new GraphQLObjectType({
  name: 'User',
  fields: {
  id: globalIdField('User'), //this id is an immutable string that never change.
  userID: {
    type: GraphQLString,
    description: 'the database user\'s id',
  },
  name: {
    type: GraphQLString,
    description: 'the name of the user',
  },
  mail: {
    type: GraphQLString,
    description: 'the mail of the user',
  }
})

下面是我的根模式中的用户字段

代码语言:javascript
复制
var GraphQLRoot = new GraphQLObjectType({
  user: {
  type: new GraphQLNonNull(GraphQLUser),
  description: 'the user',
  resolve: (root, {id}, {rootValue}) => co(function*() {
    var user = yield getUser(rootValue);
    return user;
  })
}

在您的根模式中,您请求我实现的getUser函数,如下所示,这是主要的技巧!

代码语言:javascript
复制
const logID = 'qldfjbe2434RZRFeerg'; // random logID that will  remain the same forever for any user logged in, this is the id I use for my FIELD_CHANGE mutation client side

export function getUser(rootValue) {
  //IF there is no userID cookie field, no-one is logged in
  if (!rootValue.cookies.get('userID')) return {
    name: '',
    mail: '',
    id: logID
  };
  return co(function*() {
    var user =  yield getDatabaseUserByID(rootValue.cookies.get('userID'));
    user.userID = user.id;
    user.id = logID // change the id field with default immutable logID to handle FIELD_CHANGE mutation
    return user;
  })
}

下面是loginMutation客户端

代码语言:javascript
复制
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,
      id: this.props.user.id
    };
  }
  getConfigs() {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {
        user: this.props.user.id,
      }
    }];
  }
  getOptimisticResponse() {
    return {
      mail: this.props.credentials.pseudo,
      id: this.props.user.id
    };
  }
  getFatQuery() {
    return Relay.QL`
    fragment on LoginPayload {
      user {
        userID,
        mail,
        name,
      }
    }
    `;
  }

然后是loginMutation服务器端

代码语言:javascript
复制
export var LoginMutation = mutationWithClientMutationId({
  name: 'Login',
  inputFields: {
    mail: {
      type: new GraphQLNonNull(GraphQLString)
    },
    password: {
      type: new GraphQLNonNull(GraphQLString)
     },
     id: {
       type: new GraphQLNonNull(GraphQLString)
    }
  },
  outputFields: {
    user: {
      type: GraphQLUser,
      resolve: (newUser) => newUser
    }
  },
  mutateAndGetPayload: (credentials, {rootValue}) => co(function*() {
    var newUser = yield getUserByCredentials(credentials, rootValue);
    //don't forget to fill your cookie with the new userID (database id)
    rootValue.cookies.set('userID', user.userID);
    return newUser;
  })
});

就是这样!为了回答你的问题,我使用了FIELD_CHANGE变异,并共享相同的id,无论哪个用户登录,用于获得正确用户的真实id实际上是userID。它在我的项目中工作得很好。你可以在这里看一下Relay-Graphql-repo

PS:你必须把它添加到你的networkLayer上才能接受cookies

代码语言:javascript
复制
Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('/graphql', {
  credentials: 'same-origin'
  })
);
票数 16
EN

Stack Overflow用户

发布于 2015-09-13 06:10:09

中继有意地与身份验证机制无关,因此通过网络层使用自定义标头是合适的。

至于如何处理登录后的响应,最好是在此时触发整个页面刷新或重定向,因为Relay目前没有重置所有内部状态的方法(尽管是it is on the list of desirable items)。鉴于查看者身份可能会极大地影响项目可见性,为了保持正确/健壮的行为,完全重置是非常必要的。

票数 1
EN

Stack Overflow用户

发布于 2015-09-21 10:05:42

存在一个未记录的配置类型,它看起来非常适合该用例:https://github.com/facebook/relay/issues/237

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32535141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档