2013-02-18 43 views
1

我正在嘗試在我的機車項目中配置passport-twitter。用passport-twitter設置locomotivejs的最佳方法是什麼?

問題是在點擊/ auth/twitter url後沒有任何反應。

編輯:我打了控制器,但Twitter似乎沒有被調用。

我所做的就是設置一個匹配/ AUTH在routes.js/Twitter和映射這一個auth_controller.js

類似下面的代碼:

  • routes.js

    this.match('auth/twitter/', 'auth#twitter'); 
    
    this.match('auth/twitter/callback/', 'auth#callback'); 
    
  • auth_controller.js

    var locomotive = require('locomotive') 
    , Controller = locomotive.Controller 
    , passport = require('passport'); 
    
    var AuthController = new Controller(); 
    
    AuthController.twitter = function() { 
    
    console.log('[##] AuthController.twitter [##]');  
    
    passport.authenticate('twitter'), function(req, res) {}; 
    } 
    
    AuthController.callback = function() { 
    console.log('[##] AuthController.callback [##]'); 
    
    passport.authenticate('twitter', { failureRedirect: '/show' }), 
        function(req, res) { 
        res.redirect('/list'); 
        };  
    } 
    
    module.exports = AuthController; 
    

我真的不知道如果這是正確的方式來使用它與機車,任何幫助將非常感激。

乾杯, 法比奧

回答

6

護照需要先配置。如何做到這一點的例子可以發現here。在LocomotiveJS的情況下,把該配置的顯着位置將是一個初始化:

// config/initializers/10_passport_twitter.js <-- you can pick filename yourself 
module.exports = function(done) {  
    // At least the following calls are needed: 
    passport.use(new TwitterStrategy(...)); 
    passport.serializeUser(...); 
    passport.deserializeUser(...); 
}; 

接下來,配置會話和初始化護照:

// config/environments/all.js 
module.exports = { 
    ... 
    // Enable session support. 
    this.use(connect.cookieParser()); 
    this.use(connect.session({ secret: YOUR_SECRET })); 
    // Alternative for the previous line: use express.cookieSession() to enable client-side sessions 
    /* 
    this.use(express.cookieSession({ 
    secret : YOUR_SECRET, 
    cookie : { 
     maxAge : 3600 * 6 * 1000 // expiry in ms (6 hours) 
    } 
    })); 
    */ 

    // Initialize Passport. 
    this.use(passport.initialize()); 
    this.use(passport.session()); 
    ... 
}; 

接下來,路由配置:

// config/routes.js 
this.match('auth/twitter/', 'auth#twitter'); 
this.match('auth/twitter/callback/', 'auth#callback'); 

因爲passport.authenticate是中間件,所以在控制器中使用before掛鉤更容易:

// app/controllers/auth_controller.js 
... 
AuthController.twitter = function() { 
    // does nothing, only a placeholder for the following hook. 
}; 
AuthController.before('twitter', passport.authenticate('twitter')); 

AuthController.callback = function() { 
    // This will only be called when authentication succeeded. 
    this.redirect('/list'); 
} 
AuthController.before('callback', passport.authenticate('twitter', { failureRedirect: '/auth/twitter' })}; 

聲明:我沒有測試上面的代碼,我使其基於我自己的代碼,我最近在一個項目中使用,並且其使用passport-local而不是passport-twitter。然而,除了passport-local不需要的回調URL之外,基本的內容非常相似。

+0

完美!回顧一下代碼,我發現護照初始化器存在一些錯誤配置問題,而且你提出的'掛鉤'就像一個魅力!謝謝!! – ffhenkes 2013-02-19 16:18:24

+0

謝謝。 @robertklep:你能否給我們提供護照本地的「版本」?我沒有得到它:( – 2014-07-13 23:06:23

+0

@TobiasOberrauch這個特定的項目使用passport-local v0.1.6。我發現它在幾個月前進入了1.0.0,不知道這兩個版本之間有什麼區別。 – robertklep 2014-07-14 10:24:37

相關問題