2015-07-01 1 views
3

Привет, я работаю над проектом, в котором пользователь входит в систему через учетную запись google. (Localhost) Я внедрил регистрацию google. Как только я вхожу в систему с моей учетной записи, я получаю следующую ошибку.Google Oauth дает код исправленной ошибки

TokenError: Code was already redeemed. 
     at Strategy.OAuth2Strategy.parseErrorResponse (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:298:12) 
     at Strategy.OAuth2Strategy._createOAuthError (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:345:16) 
     at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:171:43 
     at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:176:18 
     at passBackControl (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9) 
     at IncomingMessage.<anonymous> (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:142:7) 
     at IncomingMessage.emit (events.js:129:20) 
     at _stream_readable.js:908:16 
     at process._tickCallback (node.js:355:11) 

Мой код выглядит следующим образом (фрагмент кода для входа в Google): -

passport.use(new GoogleStrategy(google, function(req, accessToken, refreshToken, profile, done) { 
    if (req.user) { 
    User.findOne({ google: profile.id }, function(err, existingUser) { 
     if (existingUser) { 
     console.log('There is already a Google+ account that belongs to you. Sign in with that account or delete it, then link it with your current account.'); 
     done(err); 
     } else { 
     User.findById(req.user.id, function(err, user) { 
      user.google = profile.id; 
      user.tokens.push({ kind: 'google', accessToken: accessToken }); 
      user.profile.displayName = user.profile.displayName || profile.displayName; 
      user.profile.gender = user.profile.gender || profile._json.gender; 
      //user.profile.picture = user.profile.picture || 'https://graph.facebook.com/' + profile.id + '/picture?type=large'; 
      user.save(function(err) { 
      console.log('Google account has been linked.'); 
      done(err, user); 
      }); 
     }); 
     } 
    }); 
    } else { 
    User.findOne({ google: profile.id }, function(err, existingUser) { 
     if (existingUser) return done(null, existingUser); 
     User.findOne({ email: profile._json.email }, function(err, existingEmailUser) { 
     if (existingEmailUser) { 
      console.log('There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.'); 
      done(err); 
     } else { 
      var user = new User(); 
      user.email = profile._json.email; 
      user.google = profile.id; 
      user.tokens.push({ kind: 'google', accessToken: accessToken }); 
      user.profile.displayName = profile.displayName; 
      user.profile.gender = profile._json.gender; 
      //user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large'; 
      user.profile.location = (profile._json.location) ? profile._json.location.name : ''; 
      user.save(function(err) { 
      done(err, user); 
      }); 
     } 
     }); 
    }); 
    } 
})); 

Я застрял на it.please помочь мне out..thanks

ответ

2

Проблема не в вашем «сниппет», посмотрите маршруты. Это должен быть абсолютный путь для перенаправления для google.

router.get('/auth/google/callback', 
passport.authenticate('google', { failureRedirect: '#/signIn' }), 
function(req, res) { 
// absolute path 
    res.redirect('http://localhost:8888/#/home'); 
}); 

Это известная проблема, перейдите по этой ссылке на другие обходные пути https://github.com/jaredhanson/passport-google-oauth/issues/82

+0

Заметка, у меня была проблема, когда мое приложение застряло на этапе аутентификации. Убедитесь, что если вы перемещаете свой код, вы переустанавливаете mongodb на новый хост-компьютер. –

0

У меня была такая же проблема.

Отзыв Клиентский секрет от google console решил проблему.