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

js 验证护照

In my last article (Passport local strategy section 1 | Node.js), we started the implementation of the passport-local authentication strategy. We also looked at the various requirements to get started with the login form. In this article, we will continue with setting up passport and MongoDB database.

在我的上一篇文章( Passport本地策略第1节| Node.js )中,我们开始了护照本地身份验证策略的实现。 我们还研究了登录表单入门的各种要求。 在本文中,我们将继续设置passport和MongoDB数据库

These codes are just to help you get started. So you can edit them.!

这些代码仅是为了帮助您入门。 因此您可以编辑它们。

护照设置 (Passport setup)

In the app.js file, add the following code at the bottom

app.js文件中,在底部添加以下代码

/*  PASSPORT SETUP  */  

const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

app.get('/success', (req, res) => res.send("Welcome "+req.query.username+"!!"));
app.get('/error', (req, res) => res.send("error logging in"));

passport.serializeUser(function(user, cb) {
  cb(null, user.id);
});

passport.deserializeUser(function(id, cb) {
  User.findById(id, function(err, user) {
    cb(err, user);
  });
});

The code above requires the passport module and creates 2 additional routes which handles successful login and when there's an error.

上面的代码需要通行证模块,并创建2条附加路由来处理成功登录和出现错误时的情况。

猫鼬的设置 (Mongoose setup )

Before setting up mongoose, first of all, create a database with name MyDatabase with collection userInfo and add some few records as shown below:

在设置猫鼬之前,首先,创建一个名称为MyDatabase且具有集合userInfo的数据库,并添加一些记录,如下所示:

passport module 6

Mongoose is what helps our node application to connect with our MongoDB database. It makes use of schema and models.

Mongoose是帮助我们的节点应用程序连接到MongoDB数据库的工具。 它利用模式和模型。

Schema helps us define our data structure and is later used to create our model as you'll see later in the code.

Schema帮助我们定义数据结构,以后将用于创建我们的模型,如稍后在代码中所见。

First of all install mongoose by running the following command in a terminal:

首先,通过在终端中运行以下命令来安装猫鼬:

passport module 7

In the app.js file, add the following code at the bottom

在app.js文件中,在底部添加以下代码

/* MONGOOSE SETUP */

// schema

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/MyDatabase', { useNewUrlParser: true } );

const Schema = mongoose.Schema;
const UserDetail = new Schema({
      username: String,
      password: String
    });

// model

const UserDetails = mongoose.model('userInfo', UserDetail, 'userInfo');

That's all for this second section. In the last section, we are going to set up our passport-local authentication strategy since we are working on a form and finally test our work.

这是第二部分的全部内容。 在上一节中,由于我们正在处理表单并最终测试我们的工作,因此我们将建立我们的护照本地身份验证策略。

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-2-node-js.aspx

js 验证护照

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值