1

因此,我正在尝试使用express和passportjs将facebook身份验证添加到我的ionic2应用程序。除了授权后将用户重定向到ionic2应用程序的最后部分外,我几乎可以开展工作。我的两个应用程序都在不同的端口上工作。 ionic2上http://localhost:8100运行和http://localhost:8000使用ionic2和快速应用程序的facebook-passport身份验证

运行后端的东西快递应用这些是我遵循的步骤:

  1. 上developers.facebook.com创建应用程序,并得到了应用程序ID和秘密ID。
  2. 在产品>的Facebook>设置,我加入了回调URL为

enter image description here

  • 然后在其中本地主机上运行我的快递应用:8000,我已经加入如图https://github.com/jaredhanson/passport-facebook这是这样的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/
    }); 
    

    所以现在每当用户点击Facebook上按钮ionic2应用它将进入Facebook身份验证页面,并且一旦成功验证,它将重定向回本地主机:8000/auth/facebook/callback,但我希望重定向回离子应用程序。我怎么解决这个问题?需要一些指导来解决这个问题。

  • 回答

    0

    我不确定这是否是您所需要的,但是我能够打开处理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'); 
    }); 
    

    确保您明确的应用程序你有你的视图引擎在app.js.成立

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

    你的意见/后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

    相关问题