首页 > 解决方案 > 续集 ^6.6.5,bcryot ^5.0.1。无法散列用户密码

问题描述

有人可以帮我在我这里的代码中使用 bcrypt 吗?这是我在 react-native 项目中使用的 node.js 后端,所以如果有人能在这里帮助我,我将不胜感激。

1 const User = require('../models/User');
2 const bcrypt = require('bcrypt');
3
4 module.exports = {
5
6 async store(req, res) {
7 const { name, age, city, email, password } = req.body;
8
9 const salt = bcrypt.genSaltSync(10);
10 const hash = bcrypt.hashSync(password, salt);
11
12 const user = await User.create({ name, age, city, email, password: hash });
13
14 return res.json(user);
15},
16
17 async index(req, res) {
18 const users = await User.findAll();
19
20 return res.json(users);
21 },
22
23 async findById(req, res) {
24 const { id } = req.params;
25 const user = await User.findAll({
26 where: {
27 id: id
28}
29 } );
30
31 return res.json(user);
32},
33
34 };
35

如果我不使用 bcrypt,我可以注册,但现在我尝试通过加密密码进行注册,我收到此错误:

(node:7096) UnhandledPromiseRejectionWarning: Error: data must be a string or Buffer and salt must either be a salt string or a number of rounds at Object.hashSync (F:\nodejs\Rocketseat\node_modules\bcrypt\bcrypt.js:95 :15)
    at store (F:\nodejs\Rocketseat\src\controllers\UserController.js:10:25)
    at Layer.handle [as handle_request] (F:\nodejs\Rocketseat\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\nodejs\Rocketseat\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\nodejs\Rocketseat\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (F:\nodejs\Rocketseat\node_modules\express\lib\router\layer.js:95:5)
    at F:\nodejs\Rocketseat\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (F:\nodejs\Rocketseat\node_modules\express\lib\router\index.js:335:12)
    at next (F:\nodejs\Rocketseat\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (F:\nodejs\Rocketseat\node_modules\express\lib\router\index.js:174:3)
(node:7096) UnhandledPromiseRejectionWarning: Unhandled rejection promise. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`
(see https://nodejs.org/api/cli.html...). (rejection id: 1)
(node:7096) [DEP0018] DeprecationWarning: Unhandled promises are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我还使用了我的用户变量所在的模型文件夹,仅此而已:

1 const { Model, DataTypes } = require('sequelize');
2
3  class User extends Model {
4    static init(sequelize) {
5      super.init({
6        name: DataTypes.STRING,
7        age: DataTypes.INTEGER,
8        city: DataTypes.STRING,
9        email: DataTypes.STRING,
10       password: DataTypes.STRING,
11      }, {
12        sequelize
13      })
14    }
15
16    static associate(models) {
17      this.hasMany(models.Post, { foreignKey: 'user_id', as: 'posts' });
18    }
19  }
20
21 module.exports = User;
22

所以我认为我已经做了足够的密码使用 bcrypt,但是......帮助?

标签: javascriptnode.jshashsequelize.jsbcrypt

解决方案


推荐阅读