NODE/EXPRESS/PASSPORT - Facebook App with Callback

2019-09-06 07:40发布

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条回答
Rolldiameter
2楼-- · 2019-09-06 08:08

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.

查看更多
登录 后发表回答