Removing EveryAuth from app.js and from requirements
[oweals/finalsclub.git] / app.js
diff --git a/app.js b/app.js
index 40ab7087651ec6b8f2317b53e74f5802e047eef7..ebe2d0c1c84dd436a418251ea9a10970b5348726 100644 (file)
--- a/app.js
+++ b/app.js
@@ -24,45 +24,6 @@ var Session                  = connect.middleware.session.Session;
 var parseCookie = connect.utils.parseCookie;
 var Backchannel = require('./bc/backchannel');
 
-// ********************************
-// For facebook oauth and connect
-// ********************************
-var everyauth = require('everyauth');
-var FacebookClient = require('facebook-client').FacebookClient;
-var facebook = new FacebookClient();
-
-everyauth.debug = true;
-
-// configure facebook authentication
-everyauth.facebook
-    .appId('118001624986867')
-    .appSecret('c74910f00dea3d083a00572a445af3ae')
-    .scope('user_likes,user_photos,user_photo_video_tags,email')
-    .entryPath('/fbauth')
-    .redirectPath('/profile')
-    .findOrCreateUser(function(req) {
-        log3(req.user)
-        console.log(req.user);
-        User.findOne( { }, function( err, user ) {
-            log3(err)
-            log3(user)
-            // if a user exists with that email, call them logged in
-            // FIXME: change this to different query on 'fbid'
-            if(user) {
-                // save the fact that this cookie/session-id is right for this user
-                var sid = req.sessionID;
-                user.session = sid;
-                user.save( function() {
-                    //req.session.email = 'fuckles';
-                    sendJson(res, {status: 'ok', message:'Successfully logged in via Fb'});
-                });
-            }
-        });
-    });
-    //.callbackPath('/fbsucc')
-
-
-
 
 // Depracated
 // Used for initial testing
@@ -202,9 +163,6 @@ app.configure(function(){
   // Static files are loaded when no dynamic views match.
   app.use( express.static( __dirname + '/public' ) );
 
-  // EveryAuth fb connect
-  app.use( everyauth.middleware() );
-
   // Sets the routers middleware to load after everything set
   // before it, but before static files.
   app.use( app.router );
@@ -357,10 +315,11 @@ function loadUser( req, res, next ) {
 // loadSchool is used to load a school by it's id
 function loadSchool( req, res, next ) {
   var user                     = req.user;
-  var schoolId = req.params.id;
+  var schoolName       = req.params.name;
   console.log( 'loading a school by id' );
 
-  School.findById( schoolId, function( err, school ) {
+  School.findOne({'name': schoolName}).run( function( err, school ) {
+    //sys.puts(school);
     if( school ) {
       req.school = school;
 
@@ -368,9 +327,9 @@ function loadSchool( req, res, next ) {
       // authorized to see or interact with anything related to that
       // school.
       school.authorize( user, function( authorized ){
-        req.school.authorized = authorized;
-        next();
+          req.school.authorized = authorized;
       });
+      next();
     } else {
       // If no school is found, display an appropriate error.
       sendJson(res,  {status: 'not_found', message: 'Invalid school specified!'} );
@@ -556,6 +515,8 @@ app.get( '/', loadUser, function( req, res ) {
 // in those schools.
 // Public with some private information
 app.get( '/schools', checkAjax, loadUser, function( req, res ) {
+  sys.puts('loading schools');
+  console.log(req.user);
   var user = req.user;
 
   var schoolList = [];
@@ -565,13 +526,27 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
     if( schools ) {
       // If schools are found, loop through them gathering any courses that are
       // associated with them and then render the page with that information.
-      sendJson(res, { 'user': user.sanitized, 'schools' : schools.map(function(school) {
-        var s = school.sanitized;
-        s['courses'] = Course.find( { 'school' : s._id } ).sort( 'name', '1' ).run(function( err, courses) {
-            return courses.map( function(c) { return c.sanitized; } );
+      var schools_todo = schools.length;
+      schools.map(function (school) {
+        Course.find( { 'school': school.id } ).run(function (err, courses) {
+          school.courses_length = courses.length
+          schools_todo -= 1;
+          if (schools_todo <= 0) {
+            sendJson(res, { 'user': user.sanitized, 'schools': schools.map( function(s) {
+                var school = {
+                  _id: s._id,
+                  name: s.name,
+                  description: s.description,
+                  url: s.url,
+                  slug: s.slug,
+                  courses: s.courses_length
+                };
+                return school;
+              })
+            });
+          }
         });
-        return s
-      })})
+      });
     } else {
       // If no schools have been found, display none
       //res.render( 'schools', { 'schools' : [] } );
@@ -580,35 +555,46 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
   });
 });
 
-app.get( '/school/:slug', checkAjax, loadUser, loadSchoolSlug, function( req, res ) {
+app.get( '/school/:name', checkAjax, loadUser, loadSchool, function( req, res ) {
   var school = req.school;
   var user = req.user;
-  console.log( 'loading a school by school/:id now slug' );
+  var courses;
+  console.log( 'loading a school by school/:id now name' );
 
-  school.authorize( user, function( authorized ) {
+  //school.authorize( user, function( authorized ) {
     // This is used to display interface elements for those users
     // that are are allowed to see th)m, for instance a 'New Course' button.
-    var sanitizedSchool = school.sanitized;
-    sanitizedSchool.authorized = authorized;
+    //var sanitizedSchool = school.sanitized;
+    var sanitizedSchool = {
+      _id: school.id,
+      name: school.name,
+      description: school.description,
+      url: school.url
+    };
+    //sanitizedSchool.authorized = authorized;
     // Find all courses for school by it's id and sort by name
     Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
       // If any courses are found, set them to the appropriate school, otherwise
       // leave empty.
+      sys.puts(courses);
       if( courses.length > 0 ) {
-        sanitizedSchool.courses = courses.filter(function(course) {
+        courses = courses.filter(function(course) {
           if (!course.deleted) return course;
         }).map(function(course) {
           return course.sanitized;
         });
       } else {
-        sanitizedSchool.courses = [];
+        school.courses = [];
       }
+      sanitizedSchool.courses = courses;
+      sys.puts(courses);
+
       // This tells async (the module) that each iteration of forEach is
       // done and will continue to call the rest until they have all been
       // completed, at which time the last function below will be called.
       sendJson(res, { 'school': sanitizedSchool, 'user': user.sanitized })
     });
-  });
+  //});
 });
 
 // FIXME: version of the same using school slugs instead of ids
@@ -1484,6 +1470,7 @@ app.get( '/activate/:code', checkAjax, function( req, res ) {
 
 // Logut user
 app.get( '/logout', checkAjax, function( req, res ) {
+  sys.puts("logging out");
   var sid = req.sessionID;
 
   // Find user by session id
@@ -1809,6 +1796,7 @@ io.set('authorization', function ( handshake, next ) {
 });
 
 var backchannel = new Backchannel(app, io.of('/backchannel'), {
+  // TODO: if lecture belongs to course (find pinker's courseId) pass a 'no-answers' true/false
   subscribe: function(lecture, send) {
     Post.find({'lecture': lecture}, function(err, posts) {
       send(posts);
@@ -1933,8 +1921,6 @@ mongoose.connect( 'mongodb://localhost/fc' ); // FIXME: make relative to hostnam
 
 var mailer = new Mailer( app.set('awsAccessKey'), app.set('awsSecretKey') );
 
-everyauth.helpExpress(app);
-
 app.listen( serverPort, function() {
   console.log( "Express server listening on port %d in %s mode", app.address().port, app.settings.env );