【问题标题】:how to return token from graphql mutation如何从graphql突变返回令牌
【发布时间】:2018-12-19 01:11:57
【问题描述】:

这是我的突变代码,其中我使用具有名称、电子邮件、密码的用户类型,并且我为注册用户和登录用户进行了两次突变。我已经搜索了有关 graphql 的所有文档并阅读了所有与身份验证相关的博客,但无法得到从突变中返回令牌的答案

    const mutation = new GraphQLObjectType({
      name: "Mutation",
      fields: {
        addUser: {
          type: UserType,
          args: {
            name: { type: GraphQLString },
            email: { type: GraphQLString },
            password: { type: GraphQLString },
            avatar: { type: GraphQLString }
          },
          resolve(parentValue, args) {
            const avatar = gravatar.url(args.email);
            return bcrypt
              .hash(args.password, 10)
              .then(hash => {
               args.password = hash;
               const newUser = new User({
                  name: args.name,
                  email: args.email,
                  password: args.password,
                  avatar
                });
                return newUser
                  .save()
                  .then(user => user)
                  .catch(e => e);
              })
               .catch(e => e);
          }
        },
        login: {
          name: "Login",

          type: UserType,
          args: {
            email: { type: GraphQLString },
            password: { type: GraphQLString }
          },
          resolve(parentValue, args, context) {
            return User.findOne({ email: args.email })
              .then(user => {
                if (user) {
                  return bcrypt
                    .compare(args.password, user.password)
                    .then(isValid => {
                      if (!isValid) {
                        throw new Error({ message: "password Incrrect" });
                      } else {
                        const token = jwt.sign(
                          { name: user.name, id: user.id },
                          "mySecret"
                        );
                        return user;
                      }
                    })
                    .catch(e => e);
                } else {
                  throw new Error({ message: "email Incorrect" });
                }
              })
              .catch(e => e);
          }
        }
      }
    });

这是我的用户类型

    const UserType = new GraphQLObjectType({
      name: "User",
      fields: {
        id: { type: GraphQLString },
        name: { type: GraphQLString },
        email: { type: GraphQLString },
        password: { type: GraphQLString },
        avatar: { type: GraphQLString }
      }
    });

【问题讨论】:

    标签: jwt graphql


    【解决方案1】:

    我建议您通过删除密码字段并添加一个令牌字段来更新您的UserType

    const UserType = new GraphQLObjectType({
          name: "User",
          fields: {
            id: { type: GraphQLString },
            name: { type: GraphQLString },
            email: { type: GraphQLString },
            avatar: { type: GraphQLString },
            token: { type: GraphQLString }
          }
     });
    

    原因是UserType是突变的返回类型,所以它是“public”的,也许我们不应该向public发送密码(因为我们在服务器端进行身份验证),但是JWT是public的,这样我们就可以把它寄回去了。

    并在您的 login 突变中将令牌添加到用户对象中,例如:

       login: {
          name: "Login",
          type: UserType,
          args: { email: { type: GraphQLString }, password: { type: GraphQLString } },
          resolve(parentValue, args, context) {
            return User.findOne({ email: args.email })
              .then(user => {
            .........
                        const token = jwt.sign(
                          { name: user.name, id: user.id },
                          "mySecret"
                        );
                        user.token = token;
                        return user;
                      }
            ........
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 2018-06-18
      • 2020-07-27
      • 2021-04-01
      • 2018-10-17
      • 2021-02-27
      • 2019-06-28
      • 2020-12-22
      • 2020-02-25
      相关资源
      最近更新 更多