2

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

}
4

1 回答 1

0

当在您的 Facebook 帐户中留下应用程序登录数据时会发生此错误。删除您的登录数据以解决此错误。

  1. 登录脸书
  2. 前往设置
  3. 转到应用程序
  4. 搜索您应用的登录数据。
  5. 按下“X”按钮

按照上述进度后,尝试登录 Facebook 登录。

于 2017-10-24T02:35:11.627 回答