大佬教程收集整理的这篇文章主要介绍了node.js – 使用Passport和OAuth2社交网络的NodeJS REST身份验证,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想让用户使用用户名和密码登录,或使用Facebook,Google和Twitter等社交网络。
我使用自己的OAuth2.0服务器,使用oauth2orize模块发出访问和刷新令牌。所以现在我可以注册新用户,然后发出令牌。
我遵循本教程:
http://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/
验证路由用户:
// api ------------------------------------------------------------------------------------ app.get('/api/userInfo',passport.authenticate('bearer',{ session: false }),function(req,res) { // req.authInfo is set using the `info` argument supplied by // `BearerStrategy`. it is typically used to inDicate scope of the token,// and used in access control checks. For illustrative purposes,this // example simply returns the scope in the response. res.json({ user_id: req.user.userId,name: req.user.username,scope: req.authInfo.scope }) } );
所有这一切都很好。不幸的是,我不知道如何实施社会认证。
我正在阅读本教程:
http://scotch.io/tutorials/javascript/easy-node-authentication-facebook
但是在本教程中,他们并没有做出真正的RESTful API。根据本教程,我已经实现了用户模式,本地用户的令牌存储在分离的模型中。
// define the scheR_316_11845@a for our user model var userscheR_316_11845@a = mongoose.scheR_316_11845@a({ local: { username: { type: String,unique: true,required: true },hashedpassword: { type: String,created: { type: Date,default: Date.Now } },facebook: { id: String,token: String,email: String,name: String },twitter: { id: String,displayName: String,username: String },google: { id: String,name: String } });
passport.authenticate('bearer',
这只是验证对我的数据库的承载令牌,但是如何验证社会令牌?我错过了什么吗?
然后我决定创建一个使用API的mobile version of the same app。我决定这样做:移动应用程序通过Facebook登录,并将Facebook用户ID和FB访问令牌发送到API,API调用Facebook的API来验证这些参数,并且如果成功注册新用户(或登录在我的应用程序的DB中创建一个自定义令牌,并将其返回到移动应用程序。从这里,移动应用会发送此自定义令牌,以使用API验证应用。
这里有一些代码:
API中的auth(使用fbgraph npm模块):
var graph = require('fbgraph'),Promise = require('bluebird') ... Promise.promisify(graph.get); ... var postAuthHandler = function (req,res) { var fbUserId = req.body.fbId,fBACcessToken = req.body.fBACcessToken,accessToken = req.body.accessToken; ... graph.setAppSecret(config.facebook.app.secret); graph.setAccessToken(fBACcessToken); var graphUser; var p = graph.getAsync('me?fields=id,name,picture') .then(function (fbGraphUser) { //when the given fb id and token mismatch: if (!fbGraphUser || fbGraphUser.id !== fbUserId) { console.error("Invalid user from fBACcessToken!"); res.status(httpStatus.FORBIDDEN).json({}); return p.cancel(); } graphUser = fbGraphUser; return User.fb(fbUserId); }) .then(function (user) { if (user) { //user found by his FB access token res.status(httpStatus.OK).json({accessToken: user.accessToken}); //stop the promises chain here return p.cancel(); } ...create the user,generate a custom token and return it as above...
用户型号:
var userscheR_316_11845@a = new mongoose.scheR_316_11845@a({ facebookId: { type: String,required: true,unique: true },accessToken: { type: String,name: { type: String,required: true },photo: { type: String,categories: [{ type: mongoose.scheR_316_11845@a.Types.ObjectId,ref: 'Category' }],notepads: [{ type: mongoose.scheR_316_11845@a.Types.ObjectId,ref: 'Notepad' }] });
Facebook在手机应用程序中的看法:
auth: function(fbId,fBACcessToken) { return $http({ url: apiBase + '/users/auth',data: { fbId: fbId,fBACcessToken: fBACcessToken },method: 'POST',cache: false }); },...
https://github.com/iliyan-trifonov/notepads-ionic/blob/master/www/js/services.js#L33。
移动应用发送令牌与请求:
notepads: { list: function() { return $http({ url: apiBase + '/notepads?insidecats=1' + '&token=' + User.get().accessToken/*gets the token from the local storage*/,method: 'GET',
这是一个Ionic / Angular / Cordova应用程序。从手机应用程序的Facebook登录启动您的手机上安装的Facebook应用程序,或打开一个弹出窗口登录Facebook。然后,回调将Facebook用户的身份信息和访问令牌返回到我的移动应用。
fbgraph npm模块:https://github.com/criso/fbgraph
以上是大佬教程为你收集整理的node.js – 使用Passport和OAuth2社交网络的NodeJS REST身份验证全部内容,希望文章能够帮你解决node.js – 使用Passport和OAuth2社交网络的NodeJS REST身份验证所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。