iosnode.jsfacebookpassport.jspassport-facebook

Passport-facebook-token keeps returning 401


Currently, I am using NodeJS with Express and Passport-facebook-token module for my iOS app. I am having trouble with this because the service is always returning 401 response even though we are getting passed the OAuth logic. Any ideas with this code as to why it keeps returning 401 even though it successfully authenticated and came to our handleAuthentiation method but doesn't reach the method I have console.log("THIS IS NOT BEING CALLED") called out in?

var http = require("http");
var express = require("express");
var bodyParser = require('body-parser');
var database = require('./database.js');
var passport = require('passport'),
    FacebookTokenStrategy = require('passport-facebook-token').Strategy;

var User = require('./User.js');
var app = express();

//setup json parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

//setup facebook authentication
passport.use(new FacebookTokenStrategy({
        clientID: "2454efafd33r",
        clientSecret: "323245343"
    },
    function(accessToken, refreshToken, profile, done) {
        User.handleAuthentication(accessToken, refreshToken, profile, done, function(err, user) {
            console.log("handleAuthentication user is " + JSON.stringify(user));
            console.log("handleAuthentication error is " + JSON.stringify(err));
            done(err, user);
        });
    }
));

app.post('/auth/facebook/token',
    passport.authenticate('facebook-token'),
    function(req, res) {

        console.log("THIS IS NOT BEING CALLED");

        // do something with req.user
        res.status(200);
        res.send(user);
    }
);

http.createServer(app).listen(3000);

I am trying to trace the call thorough the other libraries and I cant seem to find any method that has success defined. I noticed that done method is mapped to verified which then calls success.


Solution

  • figured it out. Turns out i was not initializing Oauth correctly. I needed to set the following:

    // Configure Express
    app.use(logger('combined'));
    app.use(cookieParser());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    app.use(methodOverride('X-HTTP-Method-Override'));
    app.use(session({secret: 'supernova', saveUninitialized: true, resave: true}));
    app.use(passport.initialize());
    app.use(passport.session());