Created
December 8, 2010 02:30
-
-
Save evnm/732796 to your computer and use it in GitHub Desktop.
Add cookie and session support to Dropbox file browser example.
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
app.configure(function() { | |
app.use(express.logger()); | |
app.use(express.bodyDecoder()); | |
app.use(express.cookieDecoder()); // Cookie-decoding. | |
app.use(express.session()); // Session-handling. | |
}); |
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 sys = require('sys'), | |
http = require('http'), | |
OAuth = require('oauth').OAuth, | |
DropboxClient = require('dropbox').DropboxClient, | |
express = require('express'), | |
app = express.createServer(); | |
// Initialize OAuth object. | |
var API_URI = 'http://api.dropbox.com/', | |
API_VERSION = '0', | |
consumer_key = 'your_consumer_key', | |
consumer_secret = 'your_consumer_secret', | |
oauth = new OAuth( | |
API_URI + API_VERSION + '/oauth/request_token', | |
API_URI + API_VERSION + '/oauth/access_token', | |
consumer_key, consumer_secret, | |
'1.0', null, 'HMAC-SHA1'); | |
app.configure(function() { | |
app.use(express.logger()); | |
app.use(express.bodyDecoder()); | |
app.use(express.cookieDecoder()); | |
app.use(express.session()); | |
}); | |
// CSS file request. | |
app.get('/*.css', function(req, res) { | |
res.render(req.params[0] + '.sass', { layout: false }); | |
}); | |
// Login page. | |
app.get('/', function(req, res) { | |
res.render('login.jade', { | |
locals: { | |
title: 'Dropbox File Browser' | |
} | |
}); | |
}); | |
// Dropbox credential processing. | |
app.post('/process_creds', function(req, res) { | |
// Get access token. | |
var client = http.createClient(80, 'www.dropbox.com'), | |
request = client.request('GET', API_URI + API_VERSION + | |
'/token?oauth_consumer_key=' + consumer_key + | |
'&email=' + req.body.email + | |
'&password=' + req.body.password, | |
{'host': 'www.api.dropbox.com'}); | |
request.end(); | |
// Upon response, extract access token/secret and redirect to file_browser. | |
request.on('response', function(token_res) { | |
token_res.setEncoding('utf8'); | |
token_res.on('data', function(chunk) { | |
var token_res = JSON.parse(chunk), | |
access_token = token_res['token'], | |
access_token_secret = token_res['secret']; | |
req.session.dropbox = new DropboxClient(oauth, access_token, | |
access_token_secret); | |
res.redirect('/file_browser'); | |
}); | |
}); | |
}); | |
// File browser page. | |
app.get('/file_browser(/*)?', function(req, res) { | |
// Fetch target metadata and render the page. | |
if (req.session.dropbox) { | |
req.session.dropbox.getMetadata(req.params[1] || '', | |
function(err, metadata) { | |
if (err) console.log('Error: ' + sys.inspect(err)); | |
else { | |
// NOTE: Use this to strip leading path(s): str.replace(/^.*\//g, ''); | |
res.render('file_browser.jade', { | |
locals: { | |
title: 'Dropbox File Browser', | |
current_dir: (metadata.path.length > 0) ? metadata.path : 'root', | |
items: metadata.contents | |
} | |
}); | |
} | |
}); | |
} else res.redirect('home'); | |
}); | |
app.listen(3000); |
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
// File browser page. | |
app.get('/file_browser(/*)?', function(req, res) { | |
// Fetch target metadata and render the page. | |
if (req.session.dropbox) { | |
req.session.dropbox.getMetadata(req.params[1] || '', | |
function(err, metadata) { | |
if (err) console.log('Error: ' + sys.inspect(err)); | |
else { | |
// NOTE: Use this to strip leading path(s): str.replace(/^.*\//g, ''); | |
res.render('file_browser.jade', { | |
locals: { | |
title: 'Dropbox File Browser', | |
current_dir: (metadata.path.length > 0) ? metadata.path : 'root', | |
items: metadata.contents | |
} | |
}); | |
} | |
}); | |
} else res.redirect('home'); | |
}); |
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
// Upon response, extract access token/secret and redirect to file_browser. | |
request.on('response', function(token_res) { | |
token_res.setEncoding('utf8'); | |
token_res.on('data', function(chunk) { | |
var token_res = JSON.parse(chunk), | |
access_token = token_res['token'], | |
access_token_secret = token_res['secret']; | |
req.session.dropbox = new DropboxClient(oauth, access_token, | |
access_token_secret); | |
res.redirect('/file_browser'); | |
}); | |
}); |
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
// Set up persistent variables. | |
// Delete this section or comment out the following line. | |
//var access_token = '', access_token_secret = '', dropbox = null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment