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!
If you just want to log the successful login then put this in your app.js for the route to 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:
And then in a home.jade file:
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.