我想建立一个简单的练习网站,人们可以通过该网站注册和登录,进入他们想要的页面。
当用户注册时,用户名和密码通过使用passportLocalMongoose获得盐渍和散列。然后它被添加到数据库中。
这是通过使用passportLocalMongoose添加盐渍和散列的用户名和密码的样子。
{ "_id" : ObjectId("56ecb64ff2860fee176fb102"), "salt" : "f59b3e4bdd0c9081fdf84a227133d9cca2c40fdd1f304b23a786efd15e835a03", "hash" : "6ae24365b2001821253aaf62d9ad34cd5c80cc1bb369ad22701fd4f06e085c1bc32b6ffe3850bb38b23ee4947cc1dc078cd151e32de7f8a18d0bdf3732533a3c2a8ed91ec78a52fa74b266a933bc047fe2c6ab2708bfe6232ea1379069c73c154a3eaf4168aaa94d6a90f073cffbd651dd0ed5211850254832062da8b276d90efcfa59042189bab1833eef52d72ef827cd613429706d586292129221ddaed53747c51a4c92f820be98672975a400a75f49a9aa68ce6be7aa0a0b1c9de59a0004589959939ddaa06ae48b1d388ed985cb1f0b821d14039ace1448a48171d12cad04a812ec8c1716aef244afcfde0576161e1ab4ee43d58409d432068a9d716c840bff7b9cdf5a4fd04cebfb4cf1fdf23724bc4e436511e9b4510179fcaf22edb58f84ca1264773fb1df5c0e4a20298fbd9b240d5fb52008dfe443f88433f12c58b61489b7342093c873ee8bc233c570579621ec91970f01b79026d82da5f3710a3ba7be784f01a0fc49862043371e343921d544a4c50cf977ec34e5a951f5ab25523f968efe3d475927ff9cff6b6559afc2f11464b4e1b85d191ded29070488a4ad4af3d59217ce74b72afada062b7f7e81ca2da52d9e2f63bd67aff95b4578a062350f1e105a5a14899ebb59613e66d1a69b4b0b3389cb64f2400104d1b86f3838bc16091a86014d3ef3dd74dc4b910189fe7f6e63bfbf36a62d1ac8446f9f21", "username" : "John", "__v" : 0 }
我想要的是在不通过passportLocalMongoose的情况下做同样的事情。 我想使用mongod通过命令行使用salt和hash用户名和密码。
就像那样的东西
> use accounts
db.user.[something that salts and hashes]
我很抱歉使用了一些错误的术语(“腌制”,“哈希”),但我不知道我怎么能写下来。
我想补充一点,例如:
> use Accounts
>db.student.insert('username':'Mark', 'password':'1234')
然后当我尝试通过命令行登录创建用户名和密码时,它会给我“GET / favicon.ico 500 post”错误...
这是哈希(注册)的代码:
router.post('/register', function(req, res, next) {
Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) {
if (err) {
return res.render("register", {info: "Sorry. That username already exists. Try again."});
}
passport.authenticate('local')(req, res, function () {
req.session.save(function (err) {
if (err) {
return next(err);
}
res.redirect('/');
});
});
});
});
以下是用户尝试登录的代码:
router.get('/login', function(req, res) {
console.log(Error);
res.render('login', { user : req.user, message : req.flash('error')});
});
router.post('/login', passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }), function(req, res, next) {
req.session.save(function (err) {
if (err) {
console.log("error maybe?");
return next(err);
}
console.log("not error maybe?")
res.redirect('/');
});
});
这是我的联系:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var Account = new Schema({
username: String,
password: String
});
Account.plugin(passportLocalMongoose);
module.exports = mongoose.model('accounts', Account);
谢谢大家。