node oauth2验证_如何设置和使用护照OAuth Facebook身份验证(第1部分)| Node.js

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,

这是以前的文章,

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身份验证的通行证模块,请在终端上运行以下命令。

passport OAuth Facebook Authentication

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值