博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 验证护照_护照本地策略第2部分| Node.js
阅读量:2530 次
发布时间:2019-05-11

本文共 2625 字,大约阅读时间需要 8 分钟。

js 验证护照

In my last article (), 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和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 */// schemaconst mongoose = require('mongoose');mongoose.connect('mongodb://localhost/MyDatabase', {
useNewUrlParser: true } );const Schema = mongoose.Schema;const UserDetail = new Schema({
username: String, password: String });// modelconst 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.

感谢您与我编码! 下次见。 随意发表评论或问题。

翻译自:

js 验证护照

转载地址:http://pytzd.baihongyu.com/

你可能感兴趣的文章
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>