【问题标题】:facebook-passport authentication using ionic2 and express app使用 ionic2 和 express 应用程序的 facebook-passport 身份验证
【发布时间】:2017-07-02 09:13:05
【问题描述】:

所以我正在尝试使用 express 和 passportjs 将 facebook 身份验证添加到我的 ionic2 应用程序中。除了授权后将用户重定向回 ionic2 应用程序的最后一部分之外,我几乎让它工作了。我的两个应用程序都在不同的端口上工作; ionic2 在http://localhost:8100 上运行,并为在http://localhost:8000 上运行的后端内容表达应用程序

这些是我遵循的步骤:

  1. 在developers.facebook.com 上创建了应用程序并获得了应用程序ID 和秘密ID。
  2. 在 Products>Facebook>settings 下,我添加了 callBack url 为

  1. 然后在我在 localhost:8000 上运行的 express 应用程序中,我添加了 facebook 身份验证策略,如 https://github.com/jaredhanson/passport-facebook 所示,如下所示:

    passport.use(new FacebookStrategy({
       clientID: FACEBOOK_APP_ID,
       clientSecret: FACEBOOK_APP_SECRET,
       callbackURL: "http://localhost:8000/auth/facebook/callback"
    },
    function(accessToken, refreshToken, profile, cb) {
        User.findOrCreate({ facebookId: profile.id }, function (err, user) {
          return cb(err, user);
        });
    }
    ));
    
     app.get('/auth/facebook',
     passport.authenticate('facebook'));
    
    app.get('/auth/facebook/callback',
        passport.authenticate('facebook', { failureRedirect: '/login' }),
        function(req, res) {
       // Successful authentication, redirect home.
       res.redirect('/');      //how to redirect back to ionic app instead of /
    });
    

    所以现在每当用户点击 ionic2 应用程序上的 facebook 按钮时,它都会转到 facebook 身份验证页面,并且一旦成功通过身份验证,它就会按预期重定向回 l​​ocalhost:8000/auth/facebook/callback 但我想重定向回 ionic 应用程序。我怎么解决这个问题?需要一些指导来解决这个问题。

【问题讨论】:

    标签: node.js express ionic2 facebook-authentication passport-facebook


    【解决方案1】:

    我不确定这是否是您需要的,但我能够打开一个处理 facebook 身份验证的新窗口。这是在下面的提供商中完成的:我的 successRedirect 看起来像这样:

    public loginWithFacebook(callback : (data : any) => any){
        this.callback = callback;
        this.childWindow = this.nativeWindow.open(this.authEndpoint);
        this.closeTimer = setInterval(() => {
          if (this.childWindow.closed){
            clearInterval(this.closeTimer);
            this.childWindow = null;
            this.closeTimer = null;
            var passedJson = this.http.get(this.checkEndpoint).map(res => res.json());
            passedJson.subscribe(data => {
              this.callback(data);
            });
          }
        }, 500);
      }
    

    这会检查窗口是否关闭。问题是我必须自动关闭它。我只是制作了一个成功重定向呈现的 html 页面,该页面关闭了创建的新窗口。这是我的successRedirect

    router.get('/api/auth/facebook/success', function (req, res) {
        res.render('after-auth');
    });
    

    确保在您的 express 应用中,您在 app.js 中设置了视图引擎。

    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'html');
    

    你的views/after-auth.html应该是这样的

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <script type="text/javascript">
            window.close();
        </script>
    </body>
    </html>
    

    现在关闭新窗口。我仍然遇到的唯一问题是,当我尝试进行下一次通话时,会话似乎没有保存。 req.user 对我来说似乎是 undefined

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2018-04-21
      • 2012-11-23
      • 2015-03-16
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多