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应该保密

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

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

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

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

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

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验证

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

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

    node oauth2验证 In my last articles, we looked at the implementation of the passport-local authenticat ...

  2. firebase登录验证_如何使用Firebase通过三步向身份验证本机添加身份验证

    firebase登录验证 Authentication allows us to secure our apps, or limit access for non-user members. Auth ...

  3. Springboot token令牌验证解决方案 在SpringBoot实现基于Token的用户身份验证

    Springboot token令牌验证解决方案 在SpringBoot实现基于Token的用户身份验证 参考文章: (1)Springboot token令牌验证解决方案 在SpringBoot实现 ...

  4. Facebook身份验证如何有效通过

    进行Facebook运营时最怕出现的就是验证,那Facebook身份验证如何才能有效的通过呢? Facebook运营时会碰到哪些问题? 账号被封,那Facebook就需要你提交相应的身份信息进行验证 ...

  5. HTTP 请求未经客户端身份验证方案“Anonymous”授权。从服务器收到的身份验证标头为“Basic realm=xxxxx”

     asp.net调用java的Web service(Web服务),需要用户及密码认证,弹出IE的登录窗口. 出现如下错误:HTTP 请求未经客户端身份验证方案"Anonymous&qu ...

  6. azure云数据库_在Azure SQL数据库中配置多重身份验证

    azure云数据库 介绍 (Introduction) The new SSMS 17.2 allows users to authenticate using Active Directory wi ...

  7. 谷歌身份验证器验证码不对怎么回事_暴雪战网游戏手机安全令,身份验证器的使用方法...

    对于没有绑定安全令的玩家,国际服客服明确表示只会帮助一次找回被盗账号,意思就是说,如果在没有绑定安全令的情况下,再次账号被盗,客服将不会受理,至于国服没有绑定安全令的情况下,对于再次账号被盗是否提供账 ...

  8. twilio无法验证手机_使用PHP和Twilio进行多因素身份验证

    twilio无法验证手机 There are various approaches used to confirm people are in fact who they say they: refe ...

  9. mysql windows身份验证_SQL Server 2005 怎么就不能用Windows身份验证方式登录呢?

    SQL Server 2005 自从装到我的电脑上始终无法使用Windows身份验证的方式登录,由于使用用户名和密码登录还算顺畅,所以一直忽略了这 SQL Server 2005 自从装到我的电脑上始 ...

最新文章

  1. jQuery EasyUI API 中文文档 - DataGrid 数据表格
  2. C# Marshal类基本概念和入门示例程序
  3. python语音识别播放音乐_使用python语音识别播放和流式转录音频
  4. learning中的数学
  5. 在电路设计中,这7个接口类型太重要了,我难道不该学学么!
  6. 07-图4 哈利·波特的考试 (25 分)
  7. CEF3:https 请求返回状态码canceled
  8. 2010年最具潜力微博网站排行榜(转)
  9. QByteArray与char、int、float(及其数组)之间的互相转化
  10. ST环境进行测试时,事前需要考虑的问题
  11. bat批量剪切命令_批量处理文件.bat命令手册
  12. Linux面试题(总结最全面的面试题!!!)
  13. ssm员工考勤签到请假管理系统 idea maven
  14. 单个文件如何修改MD5
  15. Android飞机大战游戏报告,基于android的飞机大战游戏设计与开发.doc
  16. 《头文字D》热门同人插画欣赏
  17. OSError: could not get source code
  18. D3D9 简单图形的绘制以及显示
  19. python爬虫入门——13行代码制作英语翻译器教程,小白入门一点通
  20. 正则表达式-注册表验证

热门文章

  1. java-JSON: Expected value at 1:0 错误
  2. Linux光盘检测,qpxtool
  3. java复制单个文件
  4. 多线程写mysql数据库_多线程读写mysql数据库
  5. vue获取DOM元素并设置属性
  6. css段落文字(中英文混杂)实现两端对齐
  7. 【CSS】小妙招,各种问题总结方法处理
  8. 基于webpack3.x从0开始搭建React开发环境
  9. [转]我是如何走进黑客世界的?
  10. input输入框的input事件和change事件