如何使用Node.js和MEAN堆栈进行身份validation?

我目前正在与一个小团队的开发人员进行基于文本的游戏。 游戏需要login,我们正在使用MEAN(MongoDB,Express,Angular,Node)Stack作为应用程序的代码库,但是我被卡在身份validation上,作为一个rails开发者,我习惯于能够放入一个gem并使用助手可用。

有谁有任何经验与MEAN和身份validation?

linnovate的MEAN堆栈使用Passport.js进行身份validation。 护照使用不同的策略进行身份validation。 其中一种策略是用户名和密码对,他们称之为LocalStrategy

以下是Passportjs-Local Github 示例页面中的示例之一

步骤1:需要护照

首先你需要安装npm安装护照后的模块

var passport = require('passport'); 

第2步:configuration“validation”function

使用Passport内的LocalStrategy。 护照中的策略需要verifyfunction, verifyfunction接受凭证(在这种情况下是用户名和密码),并用用户对象调用callback。 在现实世界中,这将查询数据库; 然而,在这个例子中,我们使用了一组用户。

 passport.use(new LocalStrategy( function(username, password, done) { // Find the user by username. If there is no user with the given // username, or the password is not correct, set the user to `false` to // indicate failure and set a flash message. Otherwise, return the // authenticated `user`. findByUsername(username, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } if (user.password != password) { return done(null, false, { message: 'Invalid password' }); } return done(null, user); }) }); } )); 

步骤3:在应用程序上初始化Passport

您需要告诉Express您将使用护照,并且会为您pipe理会话。 这是通过在appconfiguration期间使用app.use()完成的。

 app.use(passport.initialize()); app.use(passport.session()); 

步骤4:在loginURI上configuration中间件

接下来,我们需要创build一个方法,当用户尝试通过POST到特定的URI来尝试login到应用程序时,将接受该方法。 它会看起来像这样

 // POST /login // Use passport.authenticate() as route middleware to authenticate the // request. If authentication fails, the user will be redirected back to the // login page. Otherwise, the primary route function function will be called, // which, in this example, will redirect the user to the home page. // // curl -v -d "username=bob&password=secret" http://127.0.0.1:3000/login app.post('/login', passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }), function(req, res) { res.redirect('/'); }); 

第5步:设置会话您可能必须为正在存储在会话中的用户对象创build自己的序列化。 这是通过以下方式完成的

 // Passport session setup. // To support persistent login sessions, Passport needs to be able to // serialize users into and deserialize users out of the session. Typically, // this will be as simple as storing the user ID when serializing, and finding // the user by ID when deserializing. passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { findById(id, function (err, user) { done(err, user); }); }); 

或者如果您更喜欢自定义实现,我最近发布了一个完整的MEAN堆栈用户注册和login示例

以下是处理authentication的用户服务的片段:

 function authenticate(username, password) { var deferred = Q.defer(); usersDb.findOne({ username: username }, function (err, user) { if (err) deferred.reject(err); if (user && bcrypt.compareSync(password, user.hash)) { // authentication successful deferred.resolve(jwt.sign({ sub: user._id }, config.secret)); } else { // authentication failed deferred.resolve(); } }); return deferred.promise; } 

或者使用开箱即用的用户pipe理的mean.io。