PassportJs Google Auth2 failureRedirect无效

时间:2018-01-12 13:42:56

标签: javascript node.js express passport.js passport-google-oauth

我使用 passport-google-oauth 进行Google登录身份验证。几乎所有工作正常但Error creating testtable table { Error: Specified key was too long; max key length is 767 bytes at Packet.asError (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\packets\packet.js:713:13) at Query.Command.execute (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\commands\command.js:28:22) at Connection.handlePacket (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\connection.js:515:28) at PacketParser.onPacket (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\connection.js:94:16) at PacketParser.executeStart (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\packet_parser.js:77:14) at Socket.<anonymous> (C:\Users\nicoe\WebstormProjects\worktrackr\backend\node_modules\mysql2\lib\connection.js:102:29) at Socket.emit (events.js:159:13) at addChunk (_stream_readable.js:265:12) at readableAddChunk (_stream_readable.js:252:11) at Socket.Readable.push (_stream_readable.js:209:10) at TCP.onread (net.js:598:20) code: 'ER_TOO_LONG_KEY', errno: 1071, sqlState: '42000', sqlMessage: 'Specified key was too long; max key length is 767 bytes' } failureRedirect文件返回错误时从未调用过,config/google.js

通过例子解释。请仔细阅读以下代码: -

配置/ google.js

return done(new Error('Sorry! Your email id is not found. Please contact to Admin.'));

server.js

module.exports = (jwt, app, passport) => {
  const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
  const User = require('../../models/user.model');

  passport.use(new GoogleStrategy({
    clientID: "xxxxxxxxxxxxxxxxxxxxxx",
    clientSecret: "xxxxxxxxxxxxxxx",
    callbackURL: "http://localhost:3000/api/user/google/callback"
  },
    function (accessToken, refreshToken, profile, done) {
      User.findOne({
        email: profile.emails[0].value,
        isActive: true
      }, function (err, user) {
        if (err) {
          return done(err);
        }
        if (!user) {
          return done(new Error('Sorry! Your email id is not found. Please contact to Admin.'));
        } else {
          return done(err, user);
        }
      });
    }
  ));
  return passport;
} 

user.route.js

........
const passportGoogle = require('./server/config/auth/google')(jwt, app, passport);
const userRoute = require('./server/routes/user.route')(jwt, app, passportGoogle);
..........

1 个答案:

答案 0 :(得分:0)

我只是路过,看到您的问题未得到解决,我想如果您想在Google护照策略中拒绝身份验证,则应该发送false作为第二个参数,而不是在完成的回调中发送用户。这将触发failureRedirect

  function (accessToken, refreshToken, profile, done) {
          User.findOne({
            email: profile.emails[0].value,
            isActive: true
          }, function (err, user) {
            if (err) {
              return done(err);
            }
            if (!user) {
              return done(new Error('Sorry! Your email id is not found. Please contact to Admin.', false));
              // Add False As Second Parameter.
            } else {
              return done(err, user);
            }
          });
        }
      ));