Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit c579b03bbab9d2369b62a6861523dcb3f3710d8f @pgte committed Jul 16, 2011
Showing with 80 additions and 0 deletions.
  1. +34 −0 app.js
  2. +40 −0 lib/users.js
  3. +4 −0 views/home.jade
  4. +2 −0 views/layout.jade
34 app.js
@@ -0,0 +1,34 @@
+var express = require('express'),
+ everyauth = require('everyauth'),
+ util = require('util'),
+ users = require('./lib/users');
+
+everyauth.twitter
+ .consumerKey('OExJ9lyDkQlaedNVB6QQ')
+ .consumerSecret('qDOXx67ArhbiA6fYeGOuxgbMYyph0hiJmMfEDtosc')
+ .findOrCreateUser(function(session, accessToken, accessTokenSecret, twitterUserData) {
+ var promise = this.Promise();
+ users.findOrCreateByTwitterData(twitterUserData, accessToken, accessTokenSecret, promise);
+ return promise;
+ })
+ .redirectPath('/');
+
+var app = express.createServer();
+app.configure(function() {
+ app.use(express.bodyParser());
+ app.use(express.cookieParser());
+ app.use(express.session({secret: "90ndsj9dfdsf"}));
+ app.use(everyauth.middleware());
+ app.use(app.router);
+ app.set('view engine', 'jade');
+ app.use(express.static(__dirname + '/public'));
+ app.use(express.errorHandler());
+ everyauth.helpExpress(app);
+});
+
+
+app.get('/', function(req, res) {
+ res.render('home');
+});
+
+app.listen(4000);
@@ -0,0 +1,40 @@
+var cradle = require('cradle'),
+ util = require('util');
+
+var c = new cradle.Connection('nodetuts26.iriscouch.com', 80, {
+ auth: { username: 'node', password: 'tuts'}
+});
+var users = c.database('users');
+
+exports.findOrCreateByTwitterData = function(twitterUserData, accessToken, accessTokenSecret, promise) {
+ users.view('docs/twitterId', {key: twitterUserData.id_str}, function(err, docs) {
+ if (err) {
+ console.log("Error using users/_design/docs/_view/twitterId:");
+ console.log(err);
+ promise.fail(err);
+ return;
+ }
+ if (docs.length > 0) {
+ var user = docs[0].value;
+ console.log('user exists: ' + util.inspect(user));
+ promise.fulfill(user);
+ } else {
+ var doc = {
+ accessToken: accessToken,
+ accessTokenSecret: accessTokenSecret,
+ name: twitterUserData.name,
+ twitterId: twitterUserData.id
+ };
+ c.database('users').save(doc, function(err, res) {
+ if (err) {
+ console.log("Error using users:");
+ console.log(err);
+ promise.fail(err);
+ return;
+ }
+ console.log('user created: ' + util.inspect(doc));
+ promise.fulfill(doc);
+ })
+ }
+ });
+}
@@ -0,0 +1,4 @@
+- if(everyauth.loggedIn)
+ p Hello #{everyauth.twitter.user.name}!
+- else
+ a(href:"/auth/twitter") log in with twitter
@@ -0,0 +1,2 @@
+!!! 5
+body!= body

0 comments on commit c579b03

Please sign in to comment.