node oauth2验证
In my last articles, we looked at the implementation of the passport-local authentication strategy. We also looked at the various requirements to get started with the login form.
在上一篇文章中,我们介绍了护照本地身份验证策略的实现。 我们还研究了登录表单入门的各种要求。
Here are the previous articles,
这是以前的文章,
Passport local strategy section 1 | Node.js
护照本地策略第1部分| Node.js
Passport local strategy section 2 | Node.js
护照本地策略第2部分| Node.js
Passport local strategy section 3 | Node.js
护照本地策略第3部分| Node.js
In this article, we will look at another form of authentication called the OAuth authentication which involves sign in or signup using social media.
在本文中,我们将介绍另一种身份验证形式,称为OAuth身份验证,它涉及使用社交媒体登录或注册 。
Don't worry that much about the big term OAuth... you'll get familiar with them as you work with Node.js more often.
不用担心OAuth这个大术语...随着您更频繁地使用Node.js,您会熟悉它们。
My goal here is to make it simpler for understanding and implementation.
我的目标是使其更易于理解和实施。
These codes are just to help you get started. So you can edit them at any time to confirm your desires.
这些代码仅是为了帮助您入门。 因此,您可以随时编辑它们以确认您的需求。
Note: You should have a basic understanding of Node.js, Express, MongoDB database and HTML.
注意:您应该对Node.js,Express,MongoDB数据库和HTML有基本的了解。
In this first section, we will set up our Express app with some routes and our HTML form.
在第一部分中,我们将使用一些路线和HTML表单来设置Express应用。
In the second section, we'll finally set up the authentication strategy it's self on Facebook developers platform and tests our code.
在第二部分中,我们最终将在Facebook开发人员平台上自行设置身份验证策略,并测试我们的代码。
Cheers!!!
干杯!!!
Create a new folder for our project where all files will be stored.
为我们的项目创建一个新文件夹,其中将存储所有文件。
Setup your Express Server.
设置您的Express Server。
Require all necessary modules and dependencies.
需要所有必要的模块和依赖项。
Create a file app.js and type the following code,
创建一个文件app.js并输入以下代码,
/* EXPRESS SETUP */
const express = require('express');
const app = express();
app.get('/', (req, res) => res.sendFile('index.html', {
root: __dirname
}));
const port = process.env.PORT || 8080;
app.listen(port, () => console.log('App listening on port ' + port));
The code above creates an express server with port 3000 and a route that will read our index.html file.
上面的代码创建了一个带有端口3000的快速服务器,该路由将读取我们的index.html文件。
Next, let's create our simple HTML file... (you can at styles as you wish later).
接下来,让我们创建简单HTML文件...(以后可以按需要设置样式)。
Create a file called index.html in your project folder and type the following HTML code.
在项目文件夹中创建一个名为index.html的文件,然后键入以下HTML代码。
<html>
<head>
<title>Node.js OAuth</title>
</head>
<body>
<center>
<a href=auth/facebook>Sign in with Facebook</a>
</center>
</body>
</html>
Now, let's install passport-facebook and start looking at what we need for our OAuth facebook authentication.
现在,让我们安装Passport-facebook并开始查看OAuth facebook身份验证所需的内容 。
To install passport module for facebook authentication, run the following command on the terminal.
要安装用于Facebook身份验证的通行证模块,请在终端上运行以下命令。
Let's then configure our strategy and set up routes for success and failure authentication.
然后,让我们配置策略并设置成功和失败身份验证的路由。
Open the app.js file and add the following code below,
打开app.js文件,并在下面添加以下代码,
/* CONFIGURATION */
const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());
//success route
app.get('/success', (req, res) => res.send("You have successfully logged in"));
//error route
app.get('/error', (req, res) => res.send("error logging in"));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
The code above configures the module and sets up the error and success route.
上面的代码配置模块并设置错误和成功路线。
The success route function runs when authentication is successful while the error route runs when there's an error.
成功路由功能在身份验证成功时运行,而错误路由在出现错误时运行。
So if the user successfully logs in with his or her Facebook account, the web page will display ''you have successfully logged in''
因此,如果用户成功使用他或她的Facebook帐户登录,则网页将显示“您已成功登录”
And that's it guys for the first section of this tutorial...
这就是本教程第一部分的内容...
You can also visit the official website of passport to learn more @ http://www.passportjs.org/
您也可以访问护照的官方网站,以了解更多信息@ http://www.passportjs.org/
Read next: How to setup and use passport OAuth Facebook Authentication (Section 1) | Node.js
阅读下一篇: 如何设置和使用护照OAuth Facebook身份验证(第1节)| Node.js
Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.
感谢您与我编码! 下次见。 随意发表评论或问题。
翻译自: https://www.includehelp.com/node-js/how-to-setup-and-use-passport-oauth-facebook-authentication-section-1-node-js.aspx
node oauth2验证