js 验证护照_护照本地策略第3部分| Node.js

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

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
Passport local strategy section 3 | Node.js

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 验证护照

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值