使用护照 - 脸书 - 令牌处理Nodejs中的身份验证,请求来自前端Facebook SDK [英] Handling authentication in Nodejs with passport-facebook-token, request coming from frontend Facebook SDK

查看:167
本文介绍了使用护照 - 脸书 - 令牌处理Nodejs中的身份验证,请求来自前端Facebook SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity应用程序。对于登录,有两种方法,一种使用电子邮件,另一种使用Facebook。如果单独登录,我没有任何问题。注册和登录电子邮件工作完美。并且用Facebook登录也是完美的。这是工作流程,我创建的只是为了让你清楚。



tl; dr [读取更新]



帐户有另一个模式,用于登录。

  var Account = new Schema({
email:String,
password:String,
facebookId:String
});

有关后端API的事情。


  1. 护照用于验证

  2. 成功登录通过API将电子邮件和令牌返回给客户。

  3. 在客户端,令牌最多是玩游戏并使用整体功能。

正如我所说,我已经覆盖部分,如果客户端使用电子邮件注册和登录,则客户端可以使用该应用程序。但我的困惑是处理与Facebook的登录。 Facebook SDK已经与Unity应用程序集成,登录成功。



现在,如何使用Facebook SDK生成的Facebook登录信息到我的后端,以便我可以在整个系统中授权用户,如同完成在电子邮件登录。



在SO和Google中遇到其他问题,我发现了 passport-facebook-token ,我也尝试使用该插件,但无法想出将数据从SDK处理到Nodejs API的逻辑和流程。有人可以帮助我了解它是如何完成的?



更新1:使用 passport-facebook-token



index.js上的策略

 code> passport.use(新的FacebookTokenStrategy({
clientID:FACEBOOK_APP_ID,
clientSecret:FACEBOOK_APP_SECRET
},function(accessToken,refreshToken,profile,done){
帐户.findOrCreate({facebookId:profile.id},function(error,user){
return done(error,user);
});
}
));

控制器API

  api.post('/ auth / facebook / token',
passport.authenticate('facebook-token'),
function(req,res) {
console.log(req.user);
//使用req.user
res.sendStatus(req.user?200:401);
}
);

现在没有显示错误,但数据没有插入到帐户架构中,我有这个在模型中的findOrCreate()函数。

  Account.statics.findOrCreate = function findOrCreate(profile,cb){
var userObj = new this();
this.findOne({facebookId:profile.id},function(err,result){
if(!result){
userObj.facebookId = profile.id;
/ / ....
userObj.save(cb);
} else {
cb(err,result);
}
});
};


解决方案

你可以使用Facebook护照,你可以请查看文档: https://github.com/jaredhanson/passport-facebook ,但基本上在您已经设置了您的开发者帐户并从Facebook的开发人员网站获取密钥后,您可以执行一个FacebookStrategy对象,如下所示,您必须指定凭据,并且文档示例中的回调是向另一个HTTP请求发送的回调快速服务器的资源,然后您可以将数据保存到mongo

  passport.use(new FacebookStrategy({
clientID:FACEBOOK_APP_ID,
clientSecret:FACEBOOK_APP_SECRET,
callbackURL:http:// localhost:3000 / auth / facebook / callback
},
function(accessToken,refreshToken,profile ,cb){
User.findOrCreate({facebookId:profile.id},func (err,user){
return cb(err,user);
});
}
));


I am working on a Unity App. For login, there are two methods, one using Email and another using Facebook. In case of login separately, I do not have any problem. Registration and Login with Email works perfectly. And Login with Facebook works perfectly as well. Here's the workflow, I created just to make you clear.

tl;dr [read update]

There's another schema for account, which is used for login.

var Account = new Schema({
  email: String,
  password: String,
  facebookId: String
});

Things to know about the backend API.

  1. Passport is used for Authentication
  2. Successful login returns email and token to the client through API.
  3. On client, token is most to play game and use the overall features.

As I said, I have already covered the part when if a client registers and login using email, then client can use the app. But my confusion is handling the logins with Facebook. Facebook SDK is already integrated with the Unity App, and Login is success.

Now, how can I use the Facebook login information that is generated by the Facebook SDK onto my back end, so that I can authorize the user throughout the system, as done in email login.

Going through other questions in SO and Google, I came across passport-facebook-token, I also tried using the plugin but could not came up with the logic and flow for handling the data from SDK into the Nodejs API. Can someone me help understand how it is done?

Update 1: Using passport-facebook-token

Strategy on index.js

passport.use(new FacebookTokenStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET
  }, function(accessToken, refreshToken, profile, done) {
    Account.findOrCreate({facebookId: profile.id}, function (error, user) {
      return done(error, user);
    });
  }
));

Controller API

api.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function (req, res) {
  console.log(req.user);
  // do something with req.user
  res.sendStatus(req.user? 200 : 401);
}
);

Now, there is no error shown, but the data is not inserted into Account Schema, I have this findOrCreate() function in Model.

Account.statics.findOrCreate = function findOrCreate(profile, cb){
var userObj = new this();
this.findOne({facebookId : profile.id},function(err,result){
    if(!result){
        userObj.facebookId = profile.id;
        //....
        userObj.save(cb);
    }else{
        cb(err,result);
    }
});
};

解决方案

you can use facebook-passport for that, you can check the documentation here: https://github.com/jaredhanson/passport-facebook but basically, after you have already set up your developer account and got your keys from the developer site of facebook you can implement a FacebookStrategy object like following where you have to specify your credential and also a callback that in the documentation example is an http request to another resource of an express server where you can then save the data to mongo

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ facebookId: profile.id }, function (err, user) {
    return cb(err, user);
   });
 }
 ));

这篇关于使用护照 - 脸书 - 令牌处理Nodejs中的身份验证,请求来自前端Facebook SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆