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

node oauth2验证

In my last article (How to set up and use passport OAuth Facebook Authentication (Section 1) | Node.js), we looked at another form of authentication called the OAuth authentication which involves sign in or signup using social media.

在我的上一篇文章( 如何设置和使用护照OAuth Facebook身份验证(第1节)| Node.js )中,我们介绍了另一种身份验证形式,称为OAuth身份验证,它涉及使用社交媒体登录或注册。

In this first section, we set up our Express app with some routes and our HTML form, installed passport-Facebook and configured the strategy.

在第一部分中,我们使用一些路线和HTML表单设置了Express应用程序,安装了password-Facebook,并配置了该策略。

In this section, we'll finally set up the authentication strategy it's self on the Facebook developers platform and tests our code.

在本部分中,我们最终将在Facebook开发人员平台上自行设置身份验证策略,并测试我们的代码

In section 1, we created 2 files: app.js and index.html.

在第1节中 ,我们创建了2个文件: app.jsindex.html

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));
/*  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);
});

<html>
<head>
<title>Node.js OAuth</title>
</head>
<body>
<center>
<a href=auth/facebook>Sign in with Facebook</a>
</center>
</body>
</html>

最后步骤 (Final Steps)

To authenticate with facebook, we need to set up some legal issues with the service provider (facebook).

要通过Facebook进行身份验证,我们需要与服务提供商(facebook)建立一些法律问题。

Open https://developers.facebook.com and create an app where you'll add your node app authentication url and also, you'll be given an APP_ID and APP_SECRET.

打开https://developers.facebook.com并创建一个应用程序,您将在其中添加节点应用程序身份验证URL,并且还将获得一个APP_ID和APP_SECRET 。

Note: Most articles have not emphasized on the fact that APP ID and APP SECRET can also be called clientID and clientSecret respectively.

注意:大多数文章都没有强调APP ID和APP SECRET也可以分别称为clientID和clientSecret 。

Remember: Please your APP_ID and APP_SECRET should be confidential

切记:请您的APP_ID和APP_SECRET应该保密

passport OAuth Facebook Authentication 2

As you can see, the site url should be same with url connected to our sign in with facebook link.

如您所见, 网站网址应与通过Facebook链接登录到我们的网址相同。

passport OAuth Facebook Authentication 3

To get your app id and app secret, click on settings and then move to basics.

要获取您的应用ID和应用秘诀,请点击设置 ,然后转到基本

passport OAuth Facebook Authentication 4

Now that we have successfully gotten our app id and secret, let's apply them to our code.

现在,我们已经成功获取了应用程序ID和密码,让我们将其应用于代码。

Open the app.js file and add the following code below,

打开app.js文件,并在下面添加以下代码,

const FacebookStrategy = require('passport-facebook').Strategy;
const FACEBOOK_APP_ID = 'your app id';
const FACEBOOK_APP_SECRET = 'your app secret';
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, cb) {
return cb(null, profile);
}
));
app.get('/auth/facebook',
passport.authenticate('facebook'));
/* 
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['user_friends', 'manage_pages', 'user_photos'] }));
*/
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/error'
}),
function(req, res) {
res.redirect('/success');
});

The code above proceeds to handle the authentication request putting into action the success and error routes depending on the results obtained from authentication.

上面的代码继续处理身份验证请求,根据从身份验证获得的结果,将成功和错误路由付诸实践。

The lines in the comment are used for permissions. The scope option asks the user permission to access certain important information like facebook pages, photos, friends and so on.

注释中的行用于权限。 范围选项要求用户访问某些重要信息,例如Facebook页面,照片,朋友等。

The code above can also be gotten from the passport js official website.

上面的代码也可以从passport js官方网站获得。

So if the user successfully logs in with his or her Facebook account, the web page will display ''you have successfully logged in''

因此,如果用户成功使用他或她的Facebook帐户登录,则网页将显示“您已成功登录”

Finally, let's run and see our output.

最后,让我们运行并查看输出。

Open a terminal from your project folder and run the command.

从项目文件夹中打开一个终端,然后运行命令。

    Node app.js

passport OAuth Facebook Authentication 5
passport OAuth Facebook Authentication 6

You can also visit the official website of passport to learn more @ http://www.passportjs.org/

您也可以访问护照的官方网站,以了解更多信息@ http://www.passportjs.org/

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-2-node-js.aspx

node oauth2验证

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376949.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python and Microsoft Word

国外网站看到的文章&#xff1a;Accessing Microsoft Word with Python follows the same syntax that we used for Excel. Let’s take a quick look at how to access Word.from time import sleep import win32com.client as win32RANGE range(3, 8)def word():word win32…

东哥读书小记 之 《一个广告人的自白》

掰着指头一算&#xff0c;端午假期确实完成不少事情&#xff0c;过的太尼玛充实鸟&#xff1a;去健身房2小时&#xff0c;且老夫的平板支撑终于能坚持超过1分钟&#xff0c;普大喜奔有木有&#xff1b;给合租的室友买蛋糕过了个生日&#xff1b;去 去哪儿 参加W3ctech的技术交流…

Redis的文件事件与时间事件处理

目录文件事件处理事件类型客户端和服务端的通信过程时间事件处理执行器执行周期性事件作用事件的调度与执行文件事件处理 Redis基于Reactor模式开发了文件事件处理器。文件事件处理器以单线程方式运行&#xff0c;通过IO多路复用程序监听多个套接字&#xff0c;实现了高性能网…

fisher-yates_使用Fisher-Yates随机播放算法以O(n)时间随机播放给定数组

fisher-yatesExample: 例&#xff1a; Say the input array is [1, 2 3, 4, 5 6, 7]After reshuffling it can be anything like[4, 3, 7, 2, 1, 5, 1]Our goal is that the reshuffling should be as random as possible. 我们的目标是&#xff0c;改组应尽可能地随机。 The…

[分享]一些在 WPF/Silverlight 中应用 MVVM 模式时可能会有点用途的代码

想来这个博客也已经有很久没更新过了&#xff0c;新年新气象&#xff0c;现在就开始写新内容吧。 最初的起因 在最近的几个月中我做的开发总是要跟 XAML 打交道&#xff0c;也就是 WPF 啊&#xff0c;Silverlight 啊&#xff0c;WF 啊这些。 在进行 WPF 和 Silverlight 开发的…

手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。...

1、拍照裁剪后 点击EditText会弹出输入法&#xff0c;却不能输入。可是点击点一EdtiText就能够输入了&#xff0c;所以我就写了一个看不见的EdtiText&#xff0c;切换焦点&#xff0c;这样就攻克了这个奇怪的这问题&#xff0c;应该是android内部的问题。 这是网络一个牛人留下…

Redis一个命令请求从发送到完成的步骤以及初始化服务器步骤

一个命令请求从发送到完成的步骤 如下&#xff1a; 1、客户端将命令请求发送给服务器 当用户在客户端中键入一个命令请求时&#xff0c;客户端会将这个命令请求转换成协议格式&#xff0c;然后通过连接到服务器的套接字&#xff0c;将协议格式的命令请求发送给服务器。 2、服…

c打印行号和函数_使用C中的函数名称,行号从任何函数打印错误消息

c打印行号和函数Sometimes, it is necessary to print some message on logic failure or anytime with the function name and line number, so that program can be debugged and fixed the issue. 有时&#xff0c;有必要在逻辑故障时或在任何时候使用功能名称和行​​号打印…

Linux SPI框架

水平有限&#xff0c;描述不当之处还请指出&#xff0c;转载请注明出处http://blog.csdn.net/vanbreaker/article/details/7733476 Linux的SPI子系统采用主机驱动和外设驱动分离的思想&#xff0c;首先主机SPI控制器是一种平台设备&#xff0c;因此它以platform的方式注册进内…

dbms标识符无效_DBMS中的嵌套查询,相关的嵌套查询和集合比较运算符

dbms标识符无效嵌套查询 (Nested Queries) A query embedded in a query. This type of relation is termed as Nested Query and the Embedded Query is termed as a subquery. 查询中嵌入的查询。 这种类型的关系称为嵌套查询&#xff0c;而嵌入式查询称为子查询。 For exam…

重构——解决过长参数列表(long parameter list)

目录1、Replace Param with Query2、Preserve Whole Object3、Introduce Param Object4、Remove Flag Argument5、Combine Functions into ClassReference当我们需要在超长函数中提炼子函数时&#xff0c;如果函数内有大量的参数和临时变量&#xff0c;这将会对函数的提炼形成很…

C# 点点滴滴: out和ref

用c#很长一段时间了&#xff0c;不过基本是啥都不会&#xff0c;当C用的&#xff0c;作为写单片机的&#xff0c;还是真心觉得C比较亲切&#xff0c;呵呵。 不过总是要进步啊&#xff0c;慢慢积累呗&#xff0c;这次是写一个CAN的上位机模板出来&#xff0c;以后的项目就要彻底…

css控制图片最宽 最高值

.content img{width:expression_r(this.width > 500 && this.height < this.width ? 500:true);max-width:500px;height:expression_r(this.height >500 ? 500:true);max-height:500px; }转载于:https://www.cnblogs.com/panlin/archive/2013/01/06/2848017…

踩踩踩

http://china.findlaw.cn/laodongfa/ctjg/cy/cybc/ 二、合法裁员经济补偿标准的计算 按照《劳动合同法》第四十七条规定&#xff0c;经济补偿按劳动者在本单位工作的年限&#xff0c;每满一年支付一个月工资的标准向劳动者支付。六个月以上不满一年的&#xff0c;按一年计算;不…

c# 字节十六进制转十进制_用C中的十进制,八进制和十六进制数字初始化字节数组...

c# 字节十六进制转十进制C中的字节数组 (byte array in C) In C programming language, an unsigned char type can be used to declare byte array in C programming language. An unsigned char can contain a value from 0 to 255, which is the value of a byte. 在C编程语…

从uptime、stress、mpstat、pidstat观察CPU密集型、IO密集型、进程密集型切换的系统性能

uptime dyydyy-Lenovo-ThinkBook-14-IIL:~$ uptime10:27:10 up 7 min, 1 user, load average: 1.32, 0.99, 0.49结果分别对应&#xff1a;当前时间、系统运行时间、当前用户数目、过去 1 分钟、5 分钟、15 分钟的平均负载(Load Average) 平均负载是指单位时间内&#xff0c…

解析和创建xml

http://www.cnblogs.com/Li-Cheng/p/3610474.html 转载于:https://www.cnblogs.com/mxw272618/p/3769900.html

python - VirtualEnv virtualenvwrapper

VirtualEnv 是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境&#xff0c;VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。 为什么要用 - 隔离项目之间的第三方包依赖&#xff0c;如A项目依赖django1.2.5&#xff0c;B项目依赖django1.3。- 为部署应用…

多台计算机共享内存_共享内存多处理器和指令执行| 计算机架构

多台计算机共享内存共享内存多处理器 (Shared Memory Multiprocessor) There are three types of shared memory multiprocessor: 共有三种类型的共享内存多处理器&#xff1a; UMA (Uniform Memory Access) UMA(统一内存访问) NUMA (Non- uniform Memory Access) NUMA(非统一…

htop与atop

htop htop使用详解–史上最强 atop Linux atop监控工具部署