Last active
November 16, 2016 15:10
-
-
Save glynnbird/89f1e9d2b350a5e6b9ce4fc7cc08ebcf to your computer and use it in GitHub Desktop.
Cloudant Envoy - Facebook auth - app3.js
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 uuid = require('uuid'), | |
express = require('express'), | |
CryptoJS = require("crypto-js"), | |
cloudant = null, | |
tokensdb = null, | |
router = express.Router(); | |
var encrypt = function(str, key) { | |
return CryptoJS.AES.encrypt(str, key).toString(); | |
}; | |
var decrypt = function(str, key) { | |
var bytes = CryptoJS.AES.decrypt(str, key); | |
return bytes.toString(CryptoJS.enc.Utf8); | |
}; | |
// setup passport | |
var passport = require('passport'), | |
FacebookStrategy = require('passport-facebook').Strategy; | |
var getOrCreateUser = function(profile, callback) { | |
var user_id = profile.id; | |
var name = profile.displayName; | |
envoy.auth.getUser(user_id, function (err, data) { | |
if (err) { | |
var meta = { | |
user_id: user_id, | |
name: name | |
}; | |
var password = uuid.v4(); | |
meta.password = encrypt(password, process.env.CLIENT_ID); | |
envoy.auth.newUser(user_id, password, meta, function (err, data) { | |
envoy.auth.getUser(user_id, function(err, data) { | |
console.log("created new user", data); | |
callback(err, data); | |
}); | |
}) | |
} else { | |
console.log("User already exists", data); | |
callback(err, data); | |
} | |
}); | |
}; | |
var opts = { | |
clientID: process.env.CLIENT_ID, | |
clientSecret: process.env.CLIENT_SECRET, | |
callbackURL: 'http://mypretenddomain.com:8000/_facebook/callback' | |
}; | |
passport.use(new FacebookStrategy(opts , function(accessToken, refreshToken, profile, done) { | |
getOrCreateUser(profile, done); | |
})); | |
router.get('/_facebook', passport.authenticate('facebook', { session: false })); | |
router.get('/_facebook/callback', passport.authenticate('facebook', { session: false }), function(req, res) { | |
var data = req.user; | |
data._id = uuid.v4(); | |
data.ts = new Date().getTime() + 1000*60*60; | |
delete data._rev; | |
tokensdb.insert(data, function (err, data) { | |
res.redirect('/token.html#?token=' + data.id) | |
}); | |
}); | |
// my custom API call | |
router.get('/_token/:token', function(req, res) { | |
tokensdb.get(req.params.token, function(err, data) { | |
if (err) { | |
res.send({ok: false}); | |
} else { | |
console.log(data); | |
tokensdb.destroy(data._id, data._rev); | |
if (data.ts > new Date().getTime()) { | |
data.meta.password = decrypt(data.meta.password, process.env.CLIENT_ID); | |
res.send(data); | |
} else { | |
res.send({ok: false, msg: 'out of date'}); | |
} | |
} | |
}); | |
}); | |
// setup Envoy to serve out our static files | |
var opts = { | |
static: require('path').join(__dirname, './public'), | |
router: router | |
}; | |
// start up the web server | |
var envoy = require('cloudant-envoy')(opts); | |
envoy.events.on('listening', function() { | |
// setup tokens database | |
cloudant = envoy.cloudant; | |
cloudant.db.create('tokens'); | |
tokensdb = cloudant.db.use('tokens'); | |
console.log('[OK] Server is up'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment