Passport.js的作用是什么,为什么我们需要它? [英] What does passport.js do and why we need it?

查看:177
本文介绍了Passport.js的作用是什么,为什么我们需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Node.js中的用户身份验证,现在我正在尝试创建一个具有登录系统的网站.我已经使用网站上的代码段使其成功运行,但是我真的不明白为什么我们需要使用password.js作为中间件来进行身份验证.

注册:

让我们以本地护照为例,当我们使用护照中间件时,我们基本上是试图在数据库中创建一个新文档,然后我们可以不用护照就可以做到这一点,例如直接使用MongoClient,检查重复项,并在加密后存储密码.

登录:

我们只需在数据库中检查用户的电子邮件或用户名,然后在匹配电子邮件或用户名后检查密码.同样,无需护照也可以做到这一点.确认用户身份后,我们可以使用express-session将会话存储在cookie中,以实现登录持久性.

可以在此处找到有关我上面描述的过程的视频.. >

我知道必须忽略一些非常重要的功能,但是在浏览了许多Web资源(包括stackoverflow,youtube,passport.js的文档和许多其他资源)之后,我仍然不明白passport.js的作用是我们为什么需要它.

如果问题看起来很傻,请提前道歉.

解决方案

Passport是express.js的中间件.它支持各种登录类型,包括基本,令牌,本地(用户名,密码),OAuth,OAuth2等.我们可以将它们组合在一起,以允许用户通过使用Google,FB或任何少量代码登录的方式进行身份验证.我们还可以使用它来组合外部身份验证服务,以便用户可以选择使用所选策略之一登录,例如谷歌,推特.使用护照进行身份验证要比从头开始构建自己的护照要快得多.这就是我们使用护照的原因.您不需要护照,它只会使开发更快.从那里了解更多信息=> http://www.passportjs.org/

I am not familiar with user authentication in Node.js, now I am trying to create a website with a login system. I have managed to make it work using the code snippets from the website, but I don't really understand why we need the passport.js as a middleware to do the authentication.

Registration:

Let's take passport-local as an example, when we are using the passport middleware, we basically is trying to create a new document in the database, then can we do it without passport, such as using the MongoClient directly, with checkings of duplicates, and store the password after encryption.

Login:

We can simply check the user's email or username against our database, and then check the password after email or username is matched. This, as well, can be done without passport. After user identity has been confirmed we can use the express-session to store the session in the cookie for login persistence.

A video about the process that I described above can be found here.

I understand that there must be some very important functionality that I neglect, but after browsing many web resources, including stackoverflow, youtube, passport.js's docs and many others, I still didn't understand what does passport.js do and why we need it.

Apologies in advance if the question seems silly.

解决方案

Passport is a middleware for express.js. It supports various login types, Basic, Token, Local (username, password), OAuth, OAuth2, etc. We can combine these to allow users to authenticate by signing in with Google, FB, or whatever service with very minimal amount of code. We can also use this to combine external auth services so users can choose to login with one of the selected Strategies, e.g. Google, Twitter. It's much quicker to use passport for authentication than to build one yourself from scratch. This is why we use passport. You don't need passport, it just makes developing quicker. Read more from there website => http://www.passportjs.org/

这篇关于Passport.js的作用是什么,为什么我们需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆