NODE / EXPRESS / PASSPORT - 具有回拨URL的Facebook应用程序

时间:2015-06-19 14:44:21

标签: node.js facebook express callback

我正在创建一个Facebook应用程序。我想让用户使用FB登录我的网站。我已经集成了代码,但FB无法找到回调页面/网址。

网址:http://www.mywebsite.com:3000/auth/facebook/callback?code= {here_goes_the_callback_code} 错误:找不到网页

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果您只想记录成功登录,请将其放入您的app.js中以获取回家路线:

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

如果您还想通过用户名问候主页上的人...

app.js中的某处,因为你将它用作路由器:

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 });
});

然后在home.jade文件中:

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

请注意在路由器中使用isAuthenticated。您可以使用它来强制私有内容仅在身份验证后才能看到。因此,如果您在网站上添加了书签/ home并希望第二天重新访问它们,则会被迫重新进行身份验证。