加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – PassportJs.如何在没有重定向用户的情况下获得face

发布时间:2020-12-17 16:58:47 所属栏目:安全 来源:网络整理
导读:我有一个NodeJs REST服务让我们称它为 – 我后端的NodeRest和前端的AngularJs. NodeRest假设用于移动应用程序以及Web应用程序,在我的情况下它是AngularJs应用程序. 使用PassportJs时,NodeRest的体系结构应解决以下问题: 服务器不应该将用户重定向到Facebook
我有一个NodeJs REST服务让我们称它为 – 我后端的NodeRest和前端的AngularJs.

NodeRest假设用于移动应用程序以及Web应用程序,在我的情况下它是AngularJs应用程序.

使用PassportJs时,NodeRest的体系结构应解决以下问题:

服务器不应该将用户重定向到Facebook以进行授权

app.get('/auth/facebook',passport.authenticate('facebook'));

被称为.

如果它要重定向它,客户端将不会得到任何东西,因为回调URL链接到NodeRest httpL // noderest / facebook / callback.相反它应该提供重定向uri,所以我可以将它发送回客户端(angularJs,mobile等…).像这样的Smth:

app.get('/auth/facebook',passport.authenticate('facebook',function(redirectUri){ 
//emit socket event to the client with redirect uri as a response data. }));

我决定在授权过程中使用socket.io作为通信通道.

客户:

var socket = io.connect(baseUrl);
    socket.on('auth:facebook:callback:getCalled',function (data) {
      // callback get called on server side.
      // user has been authenicated.
      // so now,user can talk with our NodeRest server to get and post data.      
      var firstName = data.firstName;
      var lastName = data.lastName;
});

$http.get(baseUrl + '/login/facebook').success(function(data,status,headers,config){
      redirectUriToAuthenticate = data;      
      $location.path(data);
});

客户将负责重定向到facebook / twitter等,以获得用户授权.之后,用户将被重定向到回调网址.

服务器:

app.get('/auth/facebook/callback',function(){
  passport.authenticate('facebook',{ successRedirect: '/',failureRedirect: '/login' })
  //socket.io emit event to the client with user data.
  io.sockets.on('connection',function (socket) {
  socket.emit('auth:facebook:callback:getCalled',{ data: User });
});

所有这些东西背后的一般想法是从不同类型的客户端应用程序(移动,Web,桌面等)获得授权.客户端必须只能将uri重定向到oauth2提供商(facebook,twitter等)并自行重定向到该uri. NodeRest将关注进一步的步骤(即处理回调和通知客户端).

我不知道这是一个很好的解决方案,我正在努力,所以任何类型的反馈都将是非常有用的.任何反馈我都会感激不尽.

先感谢您,
朱利安

解决方法

护照在这个问题上的记录很少 – 我也在很长一段时间内与它斗争.我发现你可以调用passport.authenticate(type,fn)(req,res,next),在fn中,你可以区分可以登录的用户和不能登录的用户.你可以打电话给req.logIn.

仅供参考,我假设您正在使用会话:

module.exports.createSession = function(req,next) {
  passport.authenticate('local',function(err,user,info) {
    if (err) {
      res.json(500,{ok: false});
    } else if(!user) {
      // you would probably want to do more work here
      // for example distinguishing between bad login credentials,// canceling,users not ready to log in (pending),etc.
      res.json(401,{ok: false});
    } else {
      req.logIn(user,function(err) {
        if (err) {
          res.json(500,{ok: false});
        } else {
          res.json(200,{
            ok:req.isAuthenticated(),username: req.user.username,email: req.user.email
          });
        }
      });
    }
  })(req,next);
};

这是为本地身份验证设置的,但我相信它应该与facebook auth一起使用而不做任何更改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读