GraphQL 登录模式

编程入门 行业动态 更新时间:2024-10-03 23:33:23

GraphQL 登录<a href=https://www.elefans.com/category/jswz/34/1771241.html style=模式"/>

GraphQL 登录模式

我的 user.password 和 user.id 出错了

“属性‘密码’在类型‘模型’上不存在。” “属性‘密码’在类型‘模型’上不存在。”

知道如何解决这个问题,我已经尝试检查 user.password 是否可用,如果没有则抛出错误

if(!user.password){
   throw new Error('User not found');
}

但是 user.password 出现相同的错误...

我的 GraphQL 架构

import config from '../../config/config';
import { GraphQLObjectType, GraphQLString } from 'graphql';
import User from '../../models/userModel';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    email: { type: GraphQLString },
    password: { type: GraphQLString },
  }),
});

const mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    // User Login
    login: {
      type: UserType,
      args: {
        email: { type: GraphQLString },
        password: { type: GraphQLString },
      },
      async resolve(parent, args, res) {
        const user = await User.findOne({ where: { email: args.email } });
        if (!user) {
          throw new Error('Password not found');
        }
        const isMatch = await bcryptpare(args.password, user.password);
        if (!isMatch) {
          throw new Error('Invalid password');
        }
        const refresh_token = createRefreshToken({ id: user.id });

        res.cookie('refreshtoken', refresh_token, {
          httpOnly: true,
          path: '/user/refresh_token',
          maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
        });

        return { msg: 'Login success!' };
      },
    },
  },
});

interface jwt {
  id: string;
}

const createRefreshToken = (payload: jwt) => {
  return jwt.sign(payload, config.JWT_SECRET, {
    expiresIn: '7d',
  });
};

我的用户模型:

import { DataTypes } from 'sequelize';
import sequelize from '../config/sequalize';

const User = sequelize.define(
  'user',
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  },
  {
    timestamps: true,
  }
);

export default User;

回答如下:

更多推荐

GraphQL 登录模式

本文发布于:2024-05-30 05:11:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   GraphQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!