我有以下用户授权路由代码:
var User = require('models/user').User;
var HttpError = require('error').HttpError;
var AuthError = require('models/user').AuthError;
var async = require('async');
exports.get = function(req, res) {
res.render('lobby', {title: 'Lobby'});
};
exports.post = function(req, res, next) {
var username = req.body.username;
var password = req.body.password;
User.authorize(username, password, function (err, user) {
if (err) {
if (err instanceof AuthError) {
return next(new HttpError(403, err.message));
} else {
return next(err);
};
};
req.session.user = user._id;
res.send({});
});
};
这是我的用户型号代码:
var crypto = require('crypto');
var async = require('async');
var util = require('util');
var mongoose = require('lib/mongoose'),
Schema = mongoose.Schema;
var schema = new Schema({
username: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
salt: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now
}
});
schema.methods.encryptPassword = function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};
schema.virtual('password')
.set(function(password) {
this._plainPassword = password;
this.salt = Math.random() + '';
this.hashedPassword = this.encryptPassword(password);
})
.get(function() { return this._plainPassword; });
schema.methods.checkPassword = function(password) {
return this.encryptPassword(password) === this.hashedPassword;
};
schema.statics.authorize = function(username, password, callback) {
async.waterfall([
function(callback) {
User.findOne({username: username}, callback);
},
function(user, callback) {
if (user) {
if (user.checkPassword(password)) {
callback(null, user);
} else {
callback(new AuthError("Wrong password"));
}
} else {
callback(new AuthError("Username does not exist"));
}
}
], callback);
};
var User = exports.User = mongoose.model('User', schema);
function AuthError(message) {
Error.apply(this, arguments);
Error.captureStackTrace(this, AuthError);
this.message = message;
}
util.inherits(AuthError, Error);
AuthError.prototype.name = 'AuthError';
exports.AuthError = AuthError;
我正在徘徊如何才能使用户注册路由是用户名应该是唯一的,如果不是用户获得“非唯一用户名”消息。我是编程的新手。
答案 0 :(得分:0)
这适用于用户模型:
schema.pre("save",function(callback) {
var self = this;
async.waterfall([
function(callback) {
mongoose.models["User"].findOne({username : self.username}, callback);
},
function(user, callback) {
if (user) {
callback(new AuthError("Wrong password"));
} else {
callback();
}
}
], callback);
});
这是注册路线:
var User = require('models/user').User;
var HttpError = require('error').HttpError;
var AuthError = require('models/user').AuthError;
var async = require('async');
exports.post = function(req, res, next) {
var username = req.body.username;
var password = req.body.password;
var user = new User({username: username, password: password})
user.save(function (err, user) {
if (err) {
if (err instanceof AuthError) {
return next(new HttpError(403, err.message));
} else {
return next(err);
};
};
req.session.user = user._id;
res.send({});
});
}
答案 1 :(得分:0)
您可以使用此代码进行注册:如果您有任何疑问,请告诉我!
var User = require('models/user');
app.post('/register',function(req,res){
var username = req.body.username;
var password = req.body.password;
var user = new User({username: username, password: password})
User.find({username: username},function(err,res){
if (err) throw err;
if(result != null)
console.log("user exists");
else
{
user.save(function (err, result) {
if (err) throw err;
console.log("show results: " + result);
});
}
});
});