Google Oauth Gives Redeemed Error Code

Hi, I am working on a project in which a user logs in through a google account. (localhost) I have implemented google registration. As soon as I log in to my account, I get the following error.

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)

My code is as follows (google login snippet): -

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);
          });
        }
      });
    });
  }
}));

I am stuck on it. Please help me .. thanks

+5
source share
4 answers

The problem is not your "fragment", look at the routes. This should be the absolute google redirect path.

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

Known issue, follow this link to other workarounds https://github.com/jaredhanson/passport-google-oauth/issues/82

+3
source

. - .

app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
   res.send('get the data');
});

, Google URL-. , Google Google. - , , .

serialiseUser deserialiseUser , cookie , - URL-.

app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
   res.redirect('/servey');  // just a url to go somewhere
});
+1

, . , . , . , .

, Google , , google+ API, , . , , , cookie, Google , . , , .

//add this in current snippet
passport.serializeUser(function(user,done){
    done(null,user.id);
});

cookie, . .

//add this in current snippet
passport.deserializeUser(function(id,done){
    User.findById(id).then(function(user){
        done(null, user);
    });
});

In addition, you need to start a cookie session, and you can do this by adding the code below to the main app.js.

const cookieSession = require('cookie-session');
app.use(cookieSession({
    maxAge: 24*60*60*1000, // age of cookie, the value is always given in milliseconds
    keys:[keys.session.cookiekey]
}));

//initialize passport
app.use(passport.initialize());
app.use(passport.session());

Please note that you need to require a cookie-session package. Install it using

npm install cookie-session

In addition, you need to write an absolute URI in the callbackURL property in your google strategy.

+1
source

I had the same problem.

Resetting the client secret from the Google console solved the problem.

0
source

All Articles