js 验证护照
Prerequisite:
先决条件:
In this article, we will finally configure the passport-local strategy, itself and the authentication request.
在本文中,我们将最终配置本地护照策略,其本身和身份验证请求 。
These codes are just to help you get started. So you can edit them at any time to confirm your own desires.
这些代码仅是为了帮助您入门。 因此,您可以随时对其进行编辑以确认自己的需求。
Note: You should have a basic understanding of Node.js, Express, MongoDB database and HTML.
注意:您应该对Node.js,Express,MongoDB数据库和HTML有基本的了解。
配置策略 (Configure Strategy)
In the app.js file, add the following code at the bottom,
在app.js文件中,在底部添加以下代码,
const LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
UserDetails.findOne({
username: username
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (user.password != password) {
return done(null, false);
}
return done(null, user);
});
}
));
In the code above, we required the passport-local strategy and then created a function that verifies the username and password input by the user to check if it's in the database.
在上面的代码中,我们需要本地护照策略,然后创建一个函数来验证用户输入的用户名和密码,以检查其是否在数据库中。
配置身份验证请求 (Configure Authentication Request)
At this point, we are going to configure what happens after the user has submitted his or her user name and password.
此时,我们将配置用户提交其用户名和密码后将发生的情况。
There are only 2 probabilities at this point, it's either the user logged in successfully or there's an error.
目前只有两种可能性,要么是用户成功登录,要么是错误。
Remember we created a route that will be used to handle both instances.
记住,我们创建了一条路由,用于处理两个实例。
In the app.js file, add the following code at the bottom,
在app.js文件中,在底部添加以下代码,
app.post('/',
passport.authenticate('local', {
failureRedirect: '/error'
}),
function(req, res) {
res.redirect('/success?username=' + req.user.username);
});
Finally, everything has been setup!!
Initiate your app and open it the port in a browser.
最后,一切都已设置!
启动您的应用程序,然后在浏览器中打开端口。
![Passport local strategy section 3 | Node.js](https://i-blog.csdnimg.cn/blog_migrate/321087942cc4c62e044aaf6825ec5f85.png)
Start your MongoDB Server...
启动您的MongoDB服务器...
Type any correct user name and password you created in the database and click submit.
键入您在数据库中创建的任何正确的用户名和密码,然后单击提交。
![Passport local strategy section 3 | Node.js](https://i-blog.csdnimg.cn/blog_migrate/700575544217b9b5bdc4c75e3c0dbbd6.png)
![Passport local strategy section 3 | Node.js](https://i-blog.csdnimg.cn/blog_migrate/0f3fa2e85c0bcd7b81edfaefa7a82355.png)
You can also see the function of the success and error routes.
您还可以查看成功和错误路由的功能。
That's an easy way to get started with passport-local strategy authentication.
这是开始使用护照本地策略身份验证的简单方法。
You can also visit the official website of passport to learn more @ http://www.passportjs.org/
您也可以访问护照的官方网站,以了解更多信息@ http://www.passportjs.org/
Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.
感谢您与我编码! 下次见。 随意发表评论或问题。
翻译自: https://www.includehelp.com/node-js/passport-local-strategy-section-3-node-js.aspx
js 验证护照