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.pswp.cn/news/376949.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376949.shtml
英文地址,請注明出處:http://en.pswp.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監控工具部署