2013-02-18 44 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

相关问题