Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import bcrypt from "bcrypt";
  2.  
  3. module.exports = (sequelize, DataType) => {
  4. const Users = sequelize.define("usuarios", {
  5. id: {
  6. type: DataType.INTEGER,
  7. primaryKey: true,
  8. autoIncrement: true
  9. },
  10. nome: {
  11. type: DataType.STRING,
  12. allowNull: false,
  13. validate: {
  14. notEmpty: true
  15. }
  16. },
  17. password: {
  18. type: DataType.STRING,
  19. allowNull: false,
  20. validate: {
  21. notEmpty: true
  22. },
  23. field : 'senha'
  24. },
  25. email: {
  26. type: DataType.STRING,
  27. unique: true,
  28. allowNull: false,
  29. validate: {
  30. notEmpty: true
  31. }
  32. },
  33. datacadastro: {
  34. type: DataType.DATE,
  35. defaultValue: new Date()
  36. }
  37. },
  38. {
  39. hooks: {
  40. beforeCreate: user => {
  41. const salt = bcrypt.genSaltSync();
  42. user.password = bcrypt.hashSync(user.password, salt);
  43. }
  44. },
  45. classMethods: {
  46. **//estou tendo erro em utilizar este isPassword**
  47. isPassword: (encodedPassword, password) => {
  48. return bcrypt.compareSync(password, encodedPassword);
  49. }
  50. }
  51. });
  52. return Users;
  53. };
  54.  
  55. import jwt from "jwt-simple";
  56.  
  57. module.exports = app => {
  58. const cfg = app.libs.config;
  59. const Users = app.db.models.usuarios;
  60.  
  61. app.post("/token", (req, res) => {
  62. if (req.body.email && req.body.password) {
  63. const email = req.body.email;
  64. const password = req.body.password;
  65. Users.findOne({where: {email: email}})
  66. .then(user => {
  67. **//Na linha abaixo que acontece o erro citado**
  68. if (Users.isPassword(user.password, password)) {
  69. const payload = {id: user.id};
  70. res.json({
  71. token: jwt.encode(payload, cfg.jwtSecret)
  72. });
  73. } else {
  74. res.sendStatus(401);
  75. }
  76. })
  77. .catch(error => res.sendStatus(401));
  78. } else {
  79. res.sendStatus(401);
  80. }
  81. });
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement