Просмотр исходного кода

新Config で Google Connect を使えるように

Sotaro KARASAWA 11 лет назад
Родитель
Сommit
69b13240c7
5 измененных файлов с 46 добавлено и 32 удалено
  1. 21 16
      lib/googleAuth.js
  2. 7 4
      models/user.js
  3. 11 8
      routes/login.js
  4. 5 2
      routes/me.js
  5. 2 2
      views/login.html

+ 21 - 16
lib/googleAuth.js

@@ -2,21 +2,24 @@
  * googleAuth utility
  */
 
-'use strict';
+module.exports = function(app) {
+  'use strict';
 
-var googleapis = require('googleapis');
-var config   = app.use('config');
+  var googleapis = require('googleapis')
+    , debug = require('debug')('crowi:lib:googleAuth')
+    , config = app.set('config')
+    , lib = {}
+    ;
 
-function createOauth2Client(url) {
-  return new googleapis.auth.OAuth2Client(
-    config.google.clientId,
-    config.google.clientSecret,
-    url
-  );
-}
+  function createOauth2Client(url) {
+    return new googleapis.auth.OAuth2Client(
+      config.crowi['google:clientId'],
+      config.crowi['google:clientSecret'],
+      url
+    );
+  }
 
-module.exports = {
-  createAuthUrl: function(req, callback) {
+  lib.createAuthUrl = function(req, callback) {
     var callbackUrl = req.baseUrl + '/google/callback';
     var google = createOauth2Client(callbackUrl);
 
@@ -26,8 +29,9 @@ module.exports = {
     });
 
     callback(null, redirectUrl);
-  },
-  handleCallback: function(req, callback) {
+  };
+
+  lib.handleCallback = function(req, callback) {
     var callbackUrl = req.baseUrl + '/google/callback';
     var google = createOauth2Client(callbackUrl);
     var code = req.session.googleAuthCode || null;
@@ -37,7 +41,6 @@ module.exports = {
     }
 
     google.getToken(code, function(err, tokens) {
-      console.log('getToken', err, tokens);
       if (err) {
         return callback(new Error('[googleAuth.handleCallback] Error to get token.'), null);
       }
@@ -57,5 +60,7 @@ module.exports = {
         });
       });
     });
-  }
+  };
+
+  return lib;
 };

+ 7 - 4
models/user.js

@@ -198,11 +198,14 @@ module.exports = function(app, models) {
   };
 
   userSchema.statics.isEmailValid = function(email, callback) {
-    return config.crowi['security.registrationWhiteList'].some(function(allowedEmail) {
-      var re = new RegExp(allowedEmail + '$');
+    if (Array.isArray(config.crowi['security.registrationWhiteList'])) {
+      return config.crowi['security.registrationWhiteList'].some(function(allowedEmail) {
+        var re = new RegExp(allowedEmail + '$');
+        return re.test(email);
+      });
+    }
 
-      return re.test(email);
-    });
+    return true;
   };
 
   userSchema.statics.findUsers = function(options, callback) {

+ 11 - 8
routes/login.js

@@ -60,10 +60,11 @@ module.exports = function(app) {
   };
 
   actions.loginGoogle = function(req, res) {
+    var googleAuth = require('../lib/googleAuth')(app);
     var code = req.session.googleAuthCode || null;
 
     if (!code) {
-      require('../lib/googleAuth').createAuthUrl(req, function(err, redirectUrl) {
+      googleAuth.createAuthUrl(req, function(err, redirectUrl) {
         if (err) {
           // TODO
         }
@@ -72,15 +73,15 @@ module.exports = function(app) {
         return res.redirect(redirectUrl);
       });
     } else {
-      require('../lib/googleAuth').handleCallback(req, function(err, tokenInfo) {
-        console.log('handleCallback', err, tokenInfo);
+      googleAuth.handleCallback(req, function(err, tokenInfo) {
+        debug('handleCallback', err, tokenInfo);
         if (err) {
           return loginFailure(req, res);
         }
 
         var googleId = tokenInfo.user_id;
         User.findUserByGoogleId(googleId, function(err, userData) {
-          console.log('findUserByGoogleId', err, userData);
+          debug('findUserByGoogleId', err, userData);
           if (!userData) {
             return loginFailure(req, res);
           }
@@ -100,7 +101,7 @@ module.exports = function(app) {
       }
 
       User.findUserByFacebookId(fbId, function(err, userData) {
-        console.log('on login findUserByFacebookId', err, userData);
+        debug('on login findUserByFacebookId', err, userData);
         if (userData) {
           return loginSuccess(req, res, userData);
         } else {
@@ -112,6 +113,7 @@ module.exports = function(app) {
 
   actions.register = function(req, res) {
     var registerForm = req.body.registerForm || {};
+    var googleAuth = require('../lib/googleAuth')(app);
 
     // ログイン済みならさようなら
     if (req.user) {
@@ -175,9 +177,9 @@ module.exports = function(app) {
       // google callback を受ける可能性もある
       var code = req.session.googleAuthCode || null;
 
-      console.log('register. if code', code);
+      debug('register. if code', code);
       if (code) {
-        require('../lib/googleAuth').handleCallback(req, function(err, tokenInfo) {
+        googleAuth.handleCallback(req, function(err, tokenInfo) {
           if (err) {
             req.flash('registerWarningMessage', 'Googleコネクト中にエラーが発生しました。');
             return res.redirect('/login?register=1'); // TODO Handling
@@ -203,7 +205,8 @@ module.exports = function(app) {
   };
 
   actions.registerGoogle = function(req, res) {
-    require('../lib/googleAuth').createAuthUrl(req, function(err, redirectUrl) {
+    var googleAuth = require('../lib/googleAuth')(app);
+    googleAuth.createAuthUrl(req, function(err, redirectUrl) {
       if (err) {
         // TODO
       }

+ 5 - 2
routes/me.js

@@ -164,6 +164,8 @@ module.exports = function(app) {
   };
 
   actions.authGoogle = function(req, res) {
+    var googleAuth = require('../lib/googleAuth')(app);
+
     var userData = req.user;
 
     var toDisconnect = req.body.disconnectGoogle ? true : false;
@@ -175,7 +177,7 @@ module.exports = function(app) {
         return res.redirect('/me');
       });
     } else if (toConnect) {
-      require('../lib/googleAuth').createAuthUrl(req, function(err, redirectUrl) {
+      googleAuth.createAuthUrl(req, function(err, redirectUrl) {
         if (err) {
           // TODO
         }
@@ -189,9 +191,10 @@ module.exports = function(app) {
   };
 
   actions.authGoogleCallback = function(req, res) {
+    var googleAuth = require('../lib/googleAuth')(app);
     var userData = req.user;
 
-    require('../lib/googleAuth').handleCallback(req, function(err, tokenInfo) {
+    googleAuth.handleCallback(req, function(err, tokenInfo) {
       if (err) {
         req.flash('warningMessage.auth.google', err.message); // FIXME: show library error message directly
         return res.redirect('/me'); // TODO Handling

+ 2 - 2
views/login.html

@@ -87,8 +87,8 @@
     {% endif %}
 
     {% if googleId %}
-    <div class="google-info">
-      <code>{{ email }}</code> この Google アカウントで登録します<br>
+    <div class="google-info alert alert-info">
+      <code>{{ googleEmail }}</code> この Google アカウントで登録します<br>
       ユーザーID、名前、パスワードを決めて登録を継続してください。
     </div>
     {% endif %}