Search code examples
node.jsfacebookpassport.jspassport-facebook

Passport-facebook FacebookTokenError: This authorization code has been used


This is my passport.js

var FacebookStrategy = require('passport-facebook').Strategy;


var User = require('./app/models/users');
var config = require('./config');

module.exports = function (passport) {


    passport.serializeUser(function (user, done) {
        done(null, user.id);
    });

    passport.deserializeUser(function (id, done) {
        User.findById(id, function (err, user) {
            done(err, user);
        });
    });



    passport.use(new FacebookStrategy({
            clientID: config.facebook.appId,
            clientSecret: config.facebook.appSecret,
            callbackURL: config.facebook.redirectUrl,
            profileFields: ['id', 'name', 'displayName', 'emails', 'photos']

        },
        function (accessToken, refreshToken, profile, done) {
            process.nextTick(function () {

                User.findOne({'facebook.id': profile.id}, function (err, user) {

                        console.log(profile);

                    if (err) {
                        // console.log("tick errr");
                        return done(err)
                    }
                    ;
                    if (user)
                        return done(null, user);
                    else {
                        var newUser = new User();
                        newUser.facebook.id = profile.id;
                        newUser.facebook.token = accessToken;
                        newUser.facebook.name = profile.displayName;
                        newUser.facebook.email = profile.emails[0].value;
                        newUser.facebook.picurl = profile.photos[0].value;
                        // console.log("access to ken is "+accessToken);
                        //console.log(newUser.facebook.id);

                        newUser.save(function (err) {
                            if (err)
                                console.log(err);
                            return done(null, newUser);
                        })
                    }
                });
                });
            // done(null, profile);
        }
    ));

api.js

 api.get('/auth/facebook', function (req, res, next) {

        passport.authenticate('facebook', {scope: ['email']})(req, res, next);
    });


    api.get('/auth/facebook/callback',

        passport.authenticate('facebook', {


            successRedirect: '#/home',
            failureRedirect: '#/login'


        })

    );


    };

I am having trouble using facebook login. I am getting following error:

FacebookTokenError: This authorization code has been used.
   at Strategy.parseErrorResponse (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\lib\strategy.js:199:12)
   at Strategy.OAuth2Strategy._createOAuthError (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:345:16)
   at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:171:43
   at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:177:18
   at passBackControl (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9)
   at IncomingMessage.<anonymous> (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
   at IncomingMessage.emit (events.js:129:20)
   at _stream_readable.js:908:16
   at process._tickDomainCallback (node.js:381:11)

I am able to login on facebook page and then after that this error shows up. The details which i am fetching from facebook are still successfully stored in my database; I think it has something to do with the auth/facebook/callback

users.js file :

var UserSchema = mongoose.Schema({
    local: {

        name: String,
        username: {type: String, index: {unique: true}},
        password: {type: String, select: false}
    },
    facebook: {
        id: String,
        token: String,
        email: String,
        name: String,
        picurl: String,
    },
    favouriteid: [{eventid: String}]

});


UserSchema.pre('save', function(next) {

    var user = this;


    if (!user.isModified('local.password')) return next();

    bcrypt.hash(user.local.password, null, null, function (err, hash) {
        if(err) return next(err);

        user.local.password = hash;
        next();

    });
});

UserSchema.methods.comparePassword = function(password) {

    var user = this;

    var a = bcrypt.compareSync(password, user.local.password);

    if (a == true)
        return true;
    else {
        console.log('error in compare password');
        return false;
    }

}

Solution

  • This Error occur when leaved Apps login data in your Facebook account. Delete your login data to solve this error.

    1. Log in Facebook
    2. Go to settings
    3. Go to Apps
    4. Search login data of your app.
    5. Push the "X"button

    After follow above progress, try to login Facebook Login.