NODE/EXPRESS/PASSPORT - 带有回调的 Facebook 应用 URL
NODE/EXPRESS/PASSPORT - Facebook App with Callback URL
我正在创建一个 Facebook 应用程序。我想让用户使用 FB 登录我的网站。我已经集成了代码,但 FB 找不到回调 page/url。
URL: 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
提前致谢!
如果您只想记录成功的登录,请将其放入 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 书签并想在第二天重新访问它,他们将被迫重新进行身份验证。
我正在创建一个 Facebook 应用程序。我想让用户使用 FB 登录我的网站。我已经集成了代码,但 FB 找不到回调 page/url。
URL: 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
提前致谢!
如果您只想记录成功的登录,请将其放入 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 书签并想在第二天重新访问它,他们将被迫重新进行身份验证。