node.js - 使用 Passport-twitter 设置 locomotivejs 的最佳方法?

标签 node.js twitter-oauth passport.js locomotivejs

我正在尝试在我的机车项目中配置 Passport-twitter。

问题是点击/auth/twitter 网址后没有任何反应。

编辑:我点击了 Controller ,但 Twitter 似乎没有被调用。

我所做的是在routes.js中设置与/auth/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;
    

我真的不知道这是否是在机车上使用它的正确方法,任何帮助将不胜感激。

干杯, 法比奥

最佳答案

需要先配置 Passport。有关如何执行此操作的示例,请参阅 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(...);
};

接下来,配置 session 并初始化 Passport:

// 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 是中间件,所以在 Controller 中使用 before Hook 会更容易:

// 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 -推特。不过,除了 passport-local 不需要的回调 URL 之外,基本原理非常相似。

关于node.js - 使用 Passport-twitter 设置 locomotivejs 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943686/

相关文章:

javascript - 使用 Node.js 将 http post 请求的异步响应获取到变量中

javascript - 重复模板

android - 我想在 android 中获取 Twitter 关注者和关注列表但是获取 NullPointer

android - 需要通过 Facebook 和 Twitter 验证用户(PhonegapAndroid)

node.js - Passport.js 支持 'Client Credentials Flow' 吗?

node.js - 浏览器关闭时 Passport session 不会被破坏

node.js - NodeJS Passport session 序列化 - 将用户反序列化到 session 中?

javascript - 子数组上的 MongoDB 查询

node.js - 在运行时在 nodejs 中检查包版本?

ios - PFUser logInWithAuthTypeInBackground(错误 : Twitter auth configuration missing)