Help us understand the problem. What is going on with this article?

compoundでTwitter認証

More than 3 years have passed since last update.

Node.jsのcompoundでTwitter認証しようと思ったら想像以上に苦戦したのでメモ

パッケージインストール

$ npm install compound-passport passport passport-twitter

config/passport.ymlを作成

development:
  baseURL: 'http://localhost:3000/'
  twitter:
    apiKey: TWITTER_CONSUMER_KEY
    secret: TWITTER_CONSUMER_SECRET
production:
  baseURL: 'http://example.com:3000/'
  twitter:
    apiKey: TWITTER_CONSUMER_KEY
    secret: TWITTER_CONSUMER_SECRET

config/autoload.jsを編集

'compound-passport'を追加

var defaultModules = [
    'jugglingdb',
    'co-assets-compiler',
    'compound-passport'
], developmentModules = [];

Userモデルにメソッド追加

モデルのプロパティは適宜読み替えてください

User.findOrCreate = function (data, done) {
   /* TWITTER */
   if (data.twitterId) {
       var twitterdata = data.profile._json;
       User.all({
           where: {
               uid: twitterdata.id
           }, limit: 1
       }, function (err, user) {
           if (user[0]){
               user[0].name = twitterdata.name;
               user[0].screenname = twitterdata.screen_name;
               user[0].icon = twitterdata.profile_image_url;
               user[0].save();
               done(err, user[0]);
           }
           else{
               console.log(twitterdata);

               console.log("create a new user");
               User.create({
                   uid: twitterdata.id,
                   name: twitterdata.name,
                   screenname: twitterdata.screen_name,
                   icon: twitterdata.profile_image_url
               }, done);
           }
       });
    }
};

application_controller.jsに追記

this.userに格納しておくとviewから直接アクセスできるようになる

before(function requireManager() {
    User.find(session.passport.user, function (err, user) {
        console.log(user);
        if (user) {
            req.user = user;
        }
        this.user = req.user;
        next();
    });
});

ログアウト用のコントローラー作成

コントローラー内で以下のメソッドを呼べばOK

req.logout();
res.redirect('/');

おわりに

/auth/twitterにアクセスすることでTwitter認証できます。
このcompound-passportを使用すると、ライブラリを弄らないとaccesstokenは取得できないようですね。

参考

abcang
JavaScriptとかRubyとかがすきです
https://abcang.net
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした