node oauth2验证_如何设置和使用护照OAuth Facebook身份验证(第1部分)| Node.js
node oauth2驗(yàn)證
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.
在上一篇文章中,我們介紹了護(hù)照本地身份驗(yàn)證策略的實(shí)現(xiàn)。 我們還研究了登錄表單入門(mén)的各種要求。
Here are the previous articles,
這是以前的文章,
Passport local strategy section 1 | Node.js
護(hù)照本地策略第1部分| Node.js
Passport local strategy section 2 | Node.js
護(hù)照本地策略第2部分| Node.js
Passport local strategy section 3 | Node.js
護(hù)照本地策略第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.
在本文中,我們將介紹另一種身份驗(yàn)證形式,稱(chēng)為OAuth身份驗(yàn)證,它涉及使用社交媒體登錄或注冊(cè) 。
Don't worry that much about the big term OAuth... you'll get familiar with them as you work with Node.js more often.
不用擔(dān)心OAuth這個(gè)大術(shù)語(yǔ)...隨著您更頻繁地使用Node.js,您會(huì)熟悉它們。
My goal here is to make it simpler for understanding and implementation.
我的目標(biāo)是使其更易于理解和實(shí)施。
These codes are just to help you get started. So you can edit them at any time to confirm your desires.
這些代碼僅是為了幫助您入門(mén)。 因此,您可以隨時(shí)編輯它們以確認(rèn)您的需求。
Note: You should have a basic understanding of Node.js, Express, MongoDB database and HTML.
注意:您應(yīng)該對(duì)Node.js,Express,MongoDB數(shù)據(jù)庫(kù)和HTML有基本的了解。
In this first section, we will set up our Express app with some routes and our HTML form.
在第一部分中,我們將使用一些路線和HTML表單來(lái)設(shè)置Express應(yīng)用。
In the second section, we'll finally set up the authentication strategy it's self on Facebook developers platform and tests our code.
在第二部分中,我們最終將在Facebook開(kāi)發(fā)人員平臺(tái)上自行設(shè)置身份驗(yàn)證策略,并測(cè)試我們的代碼。
Cheers!!!
干杯!!!
Create a new folder for our project where all files will be stored.
為我們的項(xiàng)目創(chuàng)建一個(gè)新文件夾,其中將存儲(chǔ)所有文件。
Setup your Express Server.
設(shè)置您的Express Server。
Require all necessary modules and dependencies.
需要所有必要的模塊和依賴(lài)項(xiàng)。
Create a file app.js and type the following code,
創(chuàng)建一個(gè)文件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.
上面的代碼創(chuàng)建了一個(gè)帶有端口3000的快速服務(wù)器,該路由將讀取我們的index.html文件。
Next, let's create our simple HTML file... (you can at styles as you wish later).
接下來(lái),讓我們創(chuàng)建簡(jiǎn)單HTML文件...(以后可以按需要設(shè)置樣式)。
Create a file called index.html in your project folder and type the following HTML code.
在項(xiàng)目文件夾中創(chuàng)建一個(gè)名為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.
現(xiàn)在,讓我們安裝Passport-facebook并開(kāi)始查看OAuth facebook身份驗(yàn)證所需的內(nèi)容 。
To install passport module for facebook authentication, run the following command on the terminal.
要安裝用于Facebook身份驗(yàn)證的通行證模塊,請(qǐng)?jiān)诮K端上運(yùn)行以下命令。
Let's then configure our strategy and set up routes for success and failure authentication.
然后,讓我們配置策略并設(shè)置成功和失敗身份驗(yàn)證的路由。
Open the app.js file and add the following code below,
打開(kāi)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.
上面的代碼配置模塊并設(shè)置錯(cuò)誤和成功路線。
The success route function runs when authentication is successful while the error route runs when there's an error.
成功路由功能在身份驗(yàn)證成功時(shí)運(yùn)行,而錯(cuò)誤路由在出現(xiàn)錯(cuò)誤時(shí)運(yùn)行。
So if the user successfully logs in with his or her Facebook account, the web page will display ''you have successfully logged in''
因此,如果用戶成功使用他或她的Facebook帳戶登錄,則網(wǎng)頁(yè)將顯示“您已成功登錄”
And that's it guys for the first section of this tutorial...
這就是本教程第一部分的內(nèi)容...
You can also visit the official website of passport to learn more @ http://www.passportjs.org/
您也可以訪問(wèn)護(hù)照的官方網(wǎng)站,以了解更多信息@ http://www.passportjs.org/
Read next: How to setup and use passport OAuth Facebook Authentication (Section 1) | Node.js
閱讀下一篇: 如何設(shè)置和使用護(hù)照OAuth Facebook身份驗(yàn)證(第1節(jié))| Node.js
Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.
感謝您與我編碼! 下次見(jiàn)。 隨意發(fā)表評(píng)論或問(wèn)題。
翻譯自: https://www.includehelp.com/node-js/how-to-setup-and-use-passport-oauth-facebook-authentication-section-1-node-js.aspx
node oauth2驗(yàn)證
總結(jié)
以上是生活随笔為你收集整理的node oauth2验证_如何设置和使用护照OAuth Facebook身份验证(第1部分)| Node.js的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: json转string示例_C.示例中的
- 下一篇: 第一个错误的版本_寻找第一个错误的版本