Last active
August 29, 2015 14:03
-
-
Save davidmerfield/9335c6f7506091ad606e to your computer and use it in GitHub Desktop.
Dropbox authentication with Node Express server
This file contains hidden or 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 express = require('express'), | |
server = express(), | |
events = require('events'), | |
eventEmitter = new events.EventEmitter(), | |
Dropbox = require("dropbox"), | |
options = { | |
key: 'DROPBOX_KEY', | |
secret: 'DROPBOX_SECRET' | |
}; | |
server.get('/auth', function (request, response) { | |
// We need a new dropbox client for every user we authenticate | |
var client = new Dropbox.Client(options); | |
// Register a new authDriver | |
client.authDriver({ | |
// Not sure what this does | |
authType: function() {return "code"}, | |
// Make sure you've registered the url below with Dropbox | |
// as an OAUTH Redirect URI. Dropbox calls this once the user has agreed | |
url: function() {return "http://localhost:8080/auth/callback"}, | |
doAuthorize: function(authUrl, stateParam, client, callback) { | |
// Tell express to redirect the user to the correct authURL on dropbox.com | |
response.redirect(authUrl); | |
eventEmitter.once('auth_completed', function(state, code){ | |
if (state === stateParam) {return callback({code: code})} | |
}); | |
}, | |
oauthQueryParams: ['access_token', 'expires_in', 'scope', 'token_type', 'code', 'error', 'error_description', 'error_uri', 'mac_key', 'mac_algorithm'].sort() | |
}); | |
client.authenticate(function(error, client) { | |
if (error) {return console.log(error)} | |
// Authenticated! | |
doSomethingCool(client); | |
}); | |
}); | |
server.get('/auth/callback', function(req, res){ | |
var code = req.query.code, | |
state = req.query.state; | |
eventEmitter.emit('auth_completed', state, code); | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment