Last active
August 29, 2015 14:19
-
-
Save missinglink/03809a786b30e714828f to your computer and use it in GitHub Desktop.
[broken] Attempt to resolve https://github.com/missinglink/passport-facebook-canvas/issues/3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var http = require('http'); | |
var https = require('https'); | |
var app = require('express')(); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
var multer = require('multer'); | |
var session = require('express-session'); | |
var FacebookStrategy = require('../'); | |
var passport = require('passport'); | |
// ---- middleware ---- | |
app.use(cookieParser()); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(multer()); | |
app.use(session({secret: 'secret'})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// ---- user CRUD --- | |
function User(){} | |
User.users = {}; | |
User.findOrCreate = function( profile, cb ){ | |
User.users[ profile.facebookId ] = profile; | |
return cb( null, User.users[ profile.facebookId ] ); | |
}; | |
User.findById = function( id, cb ){ | |
return cb( null, User.users[ id ] ); | |
}; | |
passport.serializeUser(function(user, done) { | |
console.log( 'serialize', user ); | |
done(null, user.facebookId); | |
}); | |
passport.deserializeUser(function(id, done) { | |
console.log( 'deserialize', id ); | |
User.findById( id, done ); | |
}); | |
// ---- passport --- | |
passport.use(new FacebookStrategy({ | |
clientID: {{your id}}, | |
clientSecret: {{your secret}}, | |
callbackURL: "https://apps.facebook.com/890324081018750" | |
}, | |
function(accessToken, refreshToken, profile, done) { | |
console.log( 'profile', profile ); | |
User.findOrCreate({ facebookId: profile.id }, function (err, user) { | |
return done(null, user); | |
}); | |
} | |
)); | |
// ---- routes --- | |
app.all('*', function(req,res,next){ | |
console.log( req.method, req.url ); | |
next(); | |
}); | |
app.all('/', function( req, res ){ | |
console.log( Object.keys( req.query ) ); | |
res.status(200).send( 'home page: ' + JSON.stringify( req.user ) + ' ' + JSON.stringify( req.session ) ); | |
}); | |
app.get('/auth/facebook', passport.authenticate('facebook-canvas')); | |
app.get('/error', function(req,res,next){ | |
console.log( req ); | |
process.exit(1); | |
}); | |
// app.get('/auth/facebook/callback', | |
// passport.authenticate('facebook-canvas', { successRedirect: '/', | |
// failureRedirect: '/error' })); | |
app.post('/auth/facebook/canvas', | |
passport.authenticate('facebook-canvas', { scope: ['public_profile', 'email'], | |
successRedirect: '/', | |
failureRedirect: '/auth/facebook/canvas/autologin' })); | |
app.get('/auth/facebook/canvas/autologin', function( req, res ){ | |
// res.redirect('/auth/facebook/callback'); | |
res.status(200).send( '<!DOCTYPE html>' + | |
'<html>' + | |
'<body>' + | |
'<script type="text/javascript">' + | |
'top.location.replace("/auth/facebook");' + | |
'</script>' + | |
'</body>' + | |
'</html>' ); | |
}); | |
// ---- server --- | |
var certificate = { | |
key: fs.readFileSync(path.resolve(__dirname, './self_signed_ssl.key'), 'utf8'), | |
cert: fs.readFileSync(path.resolve(__dirname, './self_signed_ssl.crt'), 'utf8') | |
}; | |
https.createServer(certificate, app).listen(3001); |
Author
missinglink
commented
Apr 13, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment