node.js - NODE/EXPRESS/PASSPORT - Facebook App with Callback URL -
i creating facebook app. , want let users login website using fb. have integrated code fb cannot found callback page/url.
url: http://www.mywebsite.com:3000/auth/facebook/callback?code={here_goes_the_callback_code} error: webpage cannot 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 not 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 want log successful login put in app.js route home:
router.get('/home', isauthenticated, function(req, res, next) { console.log('get /home login success [%s]', req.user.username); res.render('home'); });
if want greet person on home page username...
somewhere in app.js since you're using router:
var isauthenticated = function (req, res, next) { // if user authenticated in session, call next() call next request handler // passport adds method request object. middleware allowed add properties // request , response objects if (req.isauthenticated()) return next(); // if user not authenticated redirect him login page res.redirect('/login'); } router.get('/home', isauthenticated, function(req, res, next) { console.log('get /home login success [%s]', req.user.username); res.render('home', { user: req.user }); });
and in home.jade file:
//- incoming param(s): user doctype html html(lang='en') body #{user.username}
note use of isauthenticated in router. use force private content seen after authentication. if have bookmarked /home on website , want revisit next day they'll forced re-authenticate.
Comments
Post a Comment