Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- };
- 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);
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement