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,

這是以前的文章,

  • 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身份驗證的通行證模塊,請在終端上運行以下命令。

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驗證

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540447.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540447.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540447.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vue2.0 引用qrcode.js實現獲取改變二維碼的樣式

vue代碼 <template><div class"qart"><div id"qrcode" ref"qrcode"></div><input type"text" id"getval" value"" placeholder"修改這個值改變二維碼"></div> <…

[轉載] Python列表排序 list.sort方法和內置函數sorted

參考鏈接&#xff1a; Python中的函數 Python列表排序 list.sort方法和內置函數sorted 很多時候我們獲取到一個列表后,這個列表并不滿足我們的需求,我們需要的是一個有特殊順序的列表. 這時候就可以使用list.sort方法和內置函數sorted,本文就是介紹list.sort方法和sorted內…

Java Thread類最終同步的void join(long time_in_ms)方法,帶有示例

線程類最終同步無效連接(long time_in_ms) (Thread Class final synchronized void join(long time_in_ms)) This method is available in package java.lang.Thread.join(long time_in_ms). 軟件包java.lang.Thread.join(long time_in_ms)中提供了此方法。 join(long time_in_…

RYU控制器安裝`

2019獨角獸企業重金招聘Python工程師標準>>> 同樣是參考了http://linton.tw/2014/02/11/note-how-to-set-up-ryu-controller-with-gui-component/的內容。 1. 由于Ubuntu中自帶有Python&#xff0c;因此直接開始安裝pip apt-get install python-pip apt-get i…

[轉載] mac開發者,你不得不知道的環境變更設置方法(如Java的環境變更 source命令 )

參考鏈接&#xff1a; 設置Java環境 Mac是基于Unix的&#xff0c;所有先來幾個常識與命令&#xff1a; Unix中雙引號單引號反引號(" )的區別 Unix中雙引號起到“弱引用”的作用:被引用的字符大部分被按照字符字面的意思解釋執行&#xff0c;除了了$,\,字符除外。 [因…

人形機器人正在美國史密森尼博物館中擔任導游的工作

Te article has been removed, please visit IncludeHelps home page for more articles 該文章已被刪除&#xff0c;請訪問IncludeHelp的主頁以獲取更多文章翻譯自: https://www.includehelp.com/News/a-humanoid-robot-is-doing-the-job-of-a-guide-in-the-smithsonian-museu…

normalizr API

APInormalizedenormalizeschemaArrayEntityObjectUnionValuesnormalize(data, schema)Normalizes input data per the schema definition provided. 根據提供的schema定義規范化輸入數據。data: required Input JSON (or plain JS object) data that needs normalization.schem…

[轉載] 【Java】基礎06:HelloWorld入門程序

參考鏈接&#xff1a; 從Hello World示例開始Java編程 HelloWorld它的中文意思是&#xff1a;“你好&#xff0c;世界”。 仿佛代表著計算機對世界說出來的第一句話&#xff0c;因為它簡潔實用&#xff0c;所以被作為入門程序廣泛使用。 Java程序開發三步驟&#xff1a;編…

[轉載] Java中的命名參數

參考鏈接&#xff1a; Java命名約定 創建具有許多參數的方法是一個主要的缺點。 每當需要創建這樣的方法時&#xff0c;就在空氣中聞一聞&#xff1a;這是代碼的味道。 強化單元測試&#xff0c;然后進行重構。 沒有借口&#xff0c;沒有屁股。 重構&#xff01; 使用構建器模…

[轉載] JVM(一):JVM體系結構詳解

參考鏈接&#xff1a; JVM如何工作–JVM體系結構 JVM簡介 JVM是Java程序得以運行的平臺&#xff0c;也是Java程序可以跨平臺的底層支撐&#xff0c;從整體上來看&#xff0c;JVM的主要功能可以分為加載和執行兩大塊。其中類加載器負責.class文件的尋址與加載&#xff0…

數據庫連接池的設計思路及java實現

2019獨角獸企業重金招聘Python工程師標準>>> connectionPool.DBConnectionManager [java] view plain copy package connectionPool; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; i…

[轉載] java虛擬機 jvm 出入java棧 棧空間內存分配

參考鏈接&#xff1a; Java虛擬機(JVM)堆棧區域 java棧空間是一塊線程私有的內存空間&#xff0c;java堆和程序數據密切相關&#xff0c;那么java棧就是和線程執行密切相關。線程最基本的執行行為就是函數的調用。每次函數調用其實是通過java棧傳遞數據的。 數據結構中的棧的…

SVN命令行更新代碼

命令列表 svn help查看幫助信息 Available subcommands: add auth blame (praise, annotate, ann) cat changeli…

[轉載] Java中Runtime的使用

參考鏈接&#xff1a; Java中的JVM的關閉掛鉤 1 JDK中Runtime的定義 http://blog.csdn.net/lysnow_oss/archive/2007/05/12/1606349.aspx <轉載> 那就首先說點Runtime類吧&#xff0c;他是一個與JVM運行時環境有關的類&#xff0c;這個類是Singleton的。我…

窄帶物聯網(NB-IoT)初步了解

哪有什么天生如此&#xff0c;只是我們天天堅持。既然總有人要贏的話&#xff0c;為什么不能是我呢&#xff1f;[TOC] 什么是NB-Iot? 基于蜂窩的窄帶物聯網&#xff08;Narrow Band Internet of Things, NB-IoT&#xff09;成為萬物互聯網絡的一個重要分支。NB-IoT構建于蜂窩網…

ai人工智能_人工智能能力問答中的人工智能不確定性

ai人工智能1) Which of the following is true with respect to uncertainty in AI systems? Uncertainty arises when we are not 100 percent confident in our decisionsWhenever uncertainty arises, there is needs to be an estimation taken for getting to any conclu…

[轉載] 弄懂JDK、JRE和JVM到底是什么

參考鏈接&#xff1a; JDK JRE和JVM之間的區別 首先是JDK JDK(Java Development Kit) 是 Java 語言的軟件開發工具包(SDK)。 在JDK的安裝目錄下有一個jre目錄&#xff0c;里面有兩個文件夾bin和lib&#xff0c;在這里可以認為bin里的就是jvm&#xff0c;lib中則是jvm工作所需要…

mcq 隊列_人工智能搜索問題能力問題解答(MCQ)

mcq 隊列1) The main Aim of the AI system is to provide a solution for real-life problems by acting and thinking humanly. Whenever an agent is confronted by a problem, what is the first step that it follows towards searching a solution to the problem? Sear…

JavaOne大事紀:IBM談OpenJ9和Open Liberty

JavaOne大會以IBM陳述其最近對開源社區的貢獻作為開場&#xff1a;OpenJ9、Open Liberty和MicroProfile。IBM杰出工程師John Duimovich做了“IBM和Java&#xff1a;助力下一代創新”的開場演講。\\讀者可以回看演講視頻。\\Duimovich說IBM之所以致力于推動Java生態系統的創新&a…

[轉載] JVM中對象的回收過程

參考鏈接&#xff1a; JVM是否創建Main類(具有main()的類)的對象 當我們的程序開啟運行之后就&#xff0c;就會在我們的java堆中不斷的產生新的對象&#xff0c;而這是需要占用我們的存儲空間的&#xff0c;因為創建一個新的對象需要分配對應的內存空間&#xff0c;顯然我的內…