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的数据库,并添加一些记录,如下所示:

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:

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

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

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

  1. SAP UI5 应用开发教程之五十五 - 如何将本地 SAP UI5 应用通过 Node.js Express 部署到公网上试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  2. 本地跨域处理ajax,Node.js配合node-http-proxy解决本地开发ajax跨域问题

    情景: 前后端分离,本地前端开发调用接口会有跨域问题,一般有以下3种解决方法: 1. 后端接口打包到本地运行(缺点:每次后端更新都要去测试服下一个更新包,还要在本地搭建java运行环境,麻烦) 2. ...

  3. node.js 程序_如何不使用外部程序包创建Node.js Web应用程序

    node.js 程序 by Abhinav Pandey 通过Abhinav Pandey 如何不使用外部程序包创建Node.js Web应用程序 (How to create a Node.js w ...

  4. 常用的js验证代码_数字|电话号码|传真|邮箱|手机号码|邮编

    常用的js验证数字,电话号码,传真,邮箱,手机号码,邮编 1.数字 function testisNum(object)                        {                ...

  5. SAP UI5 应用开发教程之五十五 - 如何将本地 SAP UI5 应用通过 Node.js Express 部署到公网上

    本教程迄今为止的前 54 个步骤,开发出来的 SAP UI5 应用都只能在本地通过 localhost 访问,除非将其部署到 ABAP 服务器上. SAP UI5 应用开发教程之三十五 - 如何把本地 ...

  6. js处理本地.bin音频文件和node.js的fs模块处理本地.bin音频区别

    1.js处理本地.bin文件 <body><input type="file" name="" id="fileInput" ...

  7. js map 排序_数组方法写给女友的一系列 JS 数组操作(建议收藏 | 内附思维导图)...

    前言 最近和女友,咳咳...(说出来可能会被打s)学习JS数组方法,用几个字形容的话就是听说过,实际使用.遇到的时候就分不清具体方法会得到怎样的结果. 今天我将通过这篇文章好好整理一下关于JS数组的方 ...

  8. 查询分析器在哪里_你应该知道的3种Node.js分析器类型

    Node.js类似于许多其他编码语言,因为它需要与正确的工具结合使用来调试程序,克服任何瓶颈并优化其功能.使用正确的分析器,您可以毫不费力地实现这一目标,尽管它们都不是完美的. 在编码中,分析器是一种 ...

  9. Node.js~在linux上的部署~外网不能访问node.js网站的解决方法

    这是上一篇node.js部署到linux上的后续文章,当我们安装完node.js之后,建立了sailsjs的网站,然后在外面电脑上无法访问这个网站,这个问题我们如何去解决? 解决思路: 查看linux ...

最新文章

  1. oracle11g的安装
  2. Design Pattern - Singleton(C#)
  3. HDU2029:Palindromes _easy version
  4. scala函数的定义
  5. QT的QDesignerCustomWidgetCollectionInterface类的使用
  6. mybatis的简单查询用语句吗_面试官:Mybatis中的TypeHandler你用过吗?
  7. 一张图解决Android Studio 项目运行按钮灰色
  8. 学习python的日常7
  9. 【C】malloc(0)问题
  10. 【LeetCode 剑指offer刷题】字符串题3:Reverse String
  11. log函数 oracle power_Oracle 函数大全详细介绍
  12. visual studio运行时库MT、MTd、MD、MDd的区别
  13. [转] React之Immutable学习记录
  14. 毛戈平上市以“光影美学”的理念打造适合中国人的高端化妆品品牌
  15. 一、什么是统一社会信用代码
  16. 移动端APP设计趋势
  17. android使用高德地图SDK获取定位信息
  18. java效果_JAVA 实现漂浮效果
  19. 电厂时钟同步设备(卫星同步时钟)应用方案
  20. linux命令查看raid5,Linux中RAID5搭建与测试

热门文章

  1. switch 条件判断_C语言学习第7篇---C语言三大结构之一判断结构
  2. 服务器内存超限问题_服务器内存爆满最佳处置方案
  3. java情人节_情人节写给女朋友Java Swing代码程序
  4. linux dns及时添加,在ARM Linux上成功实现添加DNS库
  5. 在linux上面找一个脚本,30个Linux Shell脚本经典案例
  6. Nexus搭建Maven私有仓库
  7. Mysql中的转义字符
  8. W3C近期要闻:W3C战略重点报告新版发布
  9. UVA - 455 Periodic Strings【字符串】
  10. Linux的启动流程简析(以Debian为例)