0

I am creating a facebook app. And I want to let users to login to my website using FB. I have integrated the code but FB cannot found the callback page/url.

URL: http://www.mywebsite.com:3000/auth/facebook/callback?code={here_goes_the_callback_code} Error: The webpage cannot be found

app.js

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');

var passport = require('passport');
passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

// begin facebook passport -->
var FacebookStrategy = require('passport-facebook').Strategy;
var FACEBOOK_APP_ID = "---MY_FB_APP_ID---"
var FACEBOOK_APP_SECRET = "---MY_FB_APP_SECRET---";

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "http://www.mywebsite.com:3000/auth/facebook/callback"
 },
 function(accessToken, refreshToken, profile, done) {
    process.nextTick(function () {    
      return done(null, profile);
    });
  }
));
// <-- end facebook passport

var app = express();
var v_login = require('./routes/login');

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

app.get('/login', v_login.login);

app.get('/auth/facebook',
  passport.authenticate('facebook'),
  function(req, res){
  //this function will not be called
  });

app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { 
        successRedirect : '/home',
        failureRedirect: '/login'
    })
);

var isAuthenticated = function (req, res, next) {
    app.get('/home', isAuthenticated, function(req, res, next) {
    console.log("fb.user:"+req.user);
    res.render('home');
    });
}


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/login')
}

facebook.jade

doctype html
html(lang='en')
body
a(href='/auth/facebook') Facebook App

Thanks in advance!

1 Answers1

1

If you just want to log the successful login then put this in your app.js for the route to home:

router.get('/home', isAuthenticated, function(req, res, next) {
  console.log('GET /home login success for [%s]', req.user.username);
  res.render('home');
});

If you also want to greet the person on your home page by username...

Somewhere in your app.js since you're using that as your router:

var isAuthenticated = function (req, res, next) {
  // if user is authenticated in the session, call the next() to call the next request handler 
  // Passport adds this method to request object. A middleware is allowed to add properties to
  // request and response objects
  if (req.isAuthenticated())
    return next();
  // if the user is not authenticated then redirect him to the login page
  res.redirect('/login');
}

router.get('/home', isAuthenticated, function(req, res, next) {
  console.log('GET /home login success for [%s]', req.user.username);
  res.render('home', { user: req.user });
});

And then in a home.jade file:

//- Incoming param(s): user
doctype html
html(lang='en')
body
  #{user.username}

Note the use of isAuthenticated in the router. You use this to force private content to only be seen after authentication. So if you have bookmarked /home on your website and want to revisit it the next day they'll be forced to re-authenticate.

Michael Blankenship
  • 1,639
  • 10
  • 16
  • I added the code you told me, but I got the following error: app.get('/home', isAuthenticated, function(req, res, next) { ^ ReferenceError: isAuthenticated is not defined – Patricio Carvajal H. Jun 19 '15 at 17:21
  • Updated. (I thought isAuthenticated() came with Passport already.) – Michael Blankenship Jun 19 '15 at 17:34
  • I updated the code, I am not getting the error now, but it does not show the console.log message, did I miss something? --> console.log("fb.user:"+req.user); – Patricio Carvajal H. Jun 19 '15 at 18:55
  • We had a console.log() in this version in the GET for /home. It looks like this [ console.log('GET /home login success for [%s]', req.user.username);] You'd need to visit the /home page and successfully log into your mechanism for the logging then to take place. If you've logged in successfully using this code and you see the home page and it doesn't log with the text "GET /home login success for [somebody]" then there's a problem. – Michael Blankenship Jun 21 '15 at 16:15