简体   繁体   English

获取“错误:字符串”不是有效的BCrypt哈希。“在Mocha ExpressJS测试期间抛出了一个错误:)”

[英]Getting “Error: the string ”Not a valid BCrypt hash.“ was thrown, throw an Error :)” during Mocha ExpressJS testing

I have a MEAN stack app that is using Passport for authentication. 我有一个使用Passport进行身份验证的MEAN堆栈应用程序。

I'm trying to write a unit test that logs in and checks whether you are redirected to the root ( / ). 我正在尝试编写一个登录的单元测试,并检查是否重定向到根目录( / )。 However, whenever I run Mocha I get the following error message: 但是,每当我运行Mocha时,我都会收到以下错误消息:

1) POST /home Login test should redirect to / after login:
   Error: the string "Not a valid BCrypt hash." was thrown, throw an Error :)

Here's my unit test LoginSpec.js : 这是我的单元测试LoginSpec.js

var should = require("should");
var app = require("../app");
var mongoose = require("mongoose");
var User = mongoose.model("User");
var request = require("supertest");
var agent = request.agent(app);
...
describe('POST /home', function() {
    before(function(done) {
        user = new User({
            email: "john@email.com",
            firstName: "John",
            lastName: "Doe",
            password: "strongPassword",
            username: "johndoe"
        });

        user.save(done);
    })

    describe('Login test', function() {
        it ('should redirect to / after login', function(done) {
            agent.post('/login')
                .send({
                    username: 'johndoe',
                    password: 'strongPassword'
                })
                .end(function(err, res) {
                    done();
                })
        })

        after(function(done) {
            User.remove().exec();
            return done();
        })
    })
})

Do I need to BCrype my password? 我需要BCrype我的密码吗? If so, how do I do this? 如果是这样,我该怎么做?

Also, how come some of the online examples I'm seeing for logging in don't do it? 另外,为什么我登录的一些在线示例怎么办呢? Such as NodeJS/Passport - Testing user login with mocha and superagent and How to authenticate Supertest requests with Passport? 比如NodeJS / Passport - 使用mocha和superagent测试用户登录以及如何使用Passport验证Supertest请求?

I thought I'd answer this since I had the same issue and could not find anywhere with a direct answer. 我以为我会回答这个问题,因为我遇到了同样的问题而无法在任何地方找到答案。

Where you are defining a new user you will need to use bcrypt to encrypt that password, also when you are login in you will then need to use bcrypt to compare the password to the one saved in the user you have fetched. 在您定义新用户的位置,您需要使用bcrypt加密该密码,当您登录时,您将需要使用bcrypt将密码与您提取的用户中保存的密码进行比较。 Otherwise you will continue to get the issue of "Not a valid BCrypt hash.". 否则,您将继续遇到“不是有效的BCrypt哈希”的问题。

Here is a simple encrypt and compare function that I use in my app 这是我在我的应用程序中使用的简单加密和比较功能

UserSchema.methods.encryptPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
}

UserSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
}

More information can be found here: https://www.npmjs.com/package/bcrypt 更多信息可以在这里找到: https//www.npmjs.com/package/bcrypt

It happen because your password field on database have just a string, not a hashed string. 这是因为数据库上的密码字段只有一个字符串,而不是散列字符串。

It must be like $2a$08$LMXAGOARNn4XmnC/rQuhfujrWVwgK/RuHuGpLtXvcv/yruY1v3yXa but probably are just the original password. 它必须像$2a$08$LMXAGOARNn4XmnC/rQuhfujrWVwgK/RuHuGpLtXvcv/yruY1v3yXa但可能只是原始密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM