TypeError: isPassword is not a function

0

I'm having this error: TypeError: isPassword is not a function. I did several searches on the net, found similarities but still could not solve. So in another way I solved it, but as I'm starting my studies, I wanted to know what does not happen, where I might be wrong to create a classMethod and use it:

I created this js:

import bcrypt from "bcrypt";

module.exports = (sequelize, DataType) => {
  const Users = sequelize.define("usuarios", {
    id: {
      type: DataType.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    nome: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    password: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      },
      field : 'senha'
    },
    email: {
      type: DataType.STRING,
      unique: true,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    datacadastro: {
      type: DataType.DATE,
      defaultValue: new Date()
    }
  }, 
  {
      hooks: {
          beforeCreate: user => {
             const salt = bcrypt.genSaltSync();
             user.password = bcrypt.hashSync(user.password, salt);
          }
        },
      classMethods: {
          **//estou tendo erro em utilizar este isPassword**
          isPassword: (encodedPassword, password) => {
            return bcrypt.compareSync(password, encodedPassword);
          }
        }
  });
  return Users;
};

Here in this token.js that will be used to validate the email and password:

import jwt from "jwt-simple";

module.exports = app => {
  const cfg = app.libs.config;
  const Users = app.db.models.usuarios;

  app.post("/token", (req, res) => {
    if (req.body.email && req.body.password) {
      const email = req.body.email;
      const password = req.body.password;
      Users.findOne({where: {email: email}})
        .then(user => {
          **//Na linha abaixo que acontece o erro citado**
          if (Users.isPassword(user.password, password)) {
            const payload = {id: user.id};
            res.json({
              token: jwt.encode(payload, cfg.jwtSecret)
            });
          } else {
            res.sendStatus(401);
          }
        })
        .catch(error => res.sendStatus(401));
    } else {
      res.sendStatus(401);
    }
  });
};
    
asked by anonymous 22.08.2017 / 18:39

1 answer

1

To whom it may concern I found the pq of this error in this link:

link

In summary is the version difference of the sequelize the User.js (model) looks like this:

import bcrypt from "bcrypt";

module.exports = (sequelize, DataType) => {
  const Users = sequelize.define("usuarios", {
    id: {
      type: DataType.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    nome: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    password: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      },
      field : 'senha'
    },
    email: {
      type: DataType.STRING,
      unique: true,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    datacadastro: {
      type: DataType.DATE,
      defaultValue: new Date()
    }
  }, {
    hooks: {
      beforeCreate: user => {
        const salt = bcrypt.genSaltSync();
        user.password = bcrypt.hashSync(user.password, salt);
      }
   }
 });

  Users.isPassword = (encodedPassword, password) => {
    return bcrypt.compareSync(password, encodedPassword);
  }

  return Users;
};
    
22.08.2017 / 20:08