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