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

    相關問題