'use strict';

var passport = require('passport');
var NinjaStrategy = require('passport-ninja-blocks').Strategy;

exports.requireUser = function(options) {
	return function(req, res, next) {
			if (!req.user) {
				if (options.api) {
					// API users want a really obvious 403, not a redirect
					return res.send(403, {code: 403, message: 'This API call requires authentication'});
				} else {
					return res.redirect('/auth/ninja');
				}
			} else {
				next();
			}
	};
};

exports.setup = function(app, oauth_config){
	// Passport
	app.use(passport.initialize());
	app.use(passport.session());

	passport.serializeUser(function (user, done) {
		done(null, user);
	});

	passport.deserializeUser(function (obj, done) {
		done(null, obj);
	});

	passport.use(new NinjaStrategy(oauth_config,
		function (accessToken, refreshToken, profile, done) {

			// add the access token for saving
			profile.accessToken = accessToken;

			var user = {id:profile.id, name: profile.displayName, email: profile.username, accessToken: accessToken};

			done(null, user);

		}
	));

	app.get('/auth/ninja', passport.authenticate('ninja-blocks'));

	app.get('/auth/ninja/callback', passport.authenticate('ninja-blocks', {
		failureRedirect: '/'
	}), function (req, res) {
		res.redirect('/');
	});

	app.get('/logout', function (req, res) {
		req.logOut();
		res.redirect('/');
	});

};

@@ -150,17 +150,18 @@
 ions) %7B%0A
-%09
+  
 return f
@@ -186,19 +186,22 @@
 next) %7B%0A
-%09%09%09
+      
 if (!req
@@ -209,20 +209,24 @@
 user) %7B%0A
-%09%09%09%09
+        
 if (opti
@@ -236,21 +236,26 @@
 .api) %7B%0A
-%09%09%09%09%09
+          
 // API u
@@ -301,21 +301,26 @@
 edirect%0A
-%09%09%09%09%09
+          
 return r
@@ -396,20 +396,24 @@
 ion'%7D);%0A
-%09%09%09%09
+        
 %7D else %7B
@@ -417,13 +417,18 @@
 e %7B%0A
-%09%09%09%09%09
+          
 retu
@@ -463,44 +463,59 @@
 ');%0A
-%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09next();%0A%09%09%09%7D%0A%09
+        %7D%0A      %7D else %7B%0A        next();%0A      %7D%0A  
 %7D;%0A%7D
@@ -562,17 +562,18 @@
 onfig)%7B%0A
-%09
+  
 // Passp
@@ -576,17 +576,18 @@
 assport%0A
-%09
+  
 app.use(
@@ -610,17 +610,18 @@
 ize());%0A
-%09
+  
 app.use(
@@ -638,25 +638,26 @@
 ession());%0A%0A
-%09
+  
 passport.ser
@@ -683,34 +683,36 @@
  (user, done) %7B%0A
-%09%09
+    
 done(null, user)
@@ -713,23 +713,25 @@
  user);%0A
-%09
+  
 %7D);%0A%0A
-%09
+  
 passport
@@ -770,18 +770,20 @@
 done) %7B%0A
-%09%09
+    
 done(nul
@@ -791,23 +791,25 @@
 , obj);%0A
-%09
+  
 %7D);%0A%0A
-%09
+  
 passport
@@ -845,18 +845,20 @@
 config,%0A
-%09%09
+    
 function
@@ -904,19 +904,22 @@
 one) %7B%0A%0A
-%09%09%09
+      
 // add t
@@ -945,19 +945,22 @@
  saving%0A
-%09%09%09
+      
 profile.
@@ -987,19 +987,22 @@
 Token;%0A%0A
-%09%09%09
+      
 var user
@@ -1104,11 +1104,14 @@
 %7D;%0A%0A
-%09%09%09
+      
 done
@@ -1129,19 +1129,23 @@
 );%0A%0A
-%09%09%7D%0A%09
+    %7D%0A  
 ));%0A%0A
-%09
+  
 app.
@@ -1200,25 +1200,26 @@
 blocks'));%0A%0A
-%09
+  
 app.get('/au
@@ -1278,18 +1278,20 @@
 cks', %7B%0A
-%09%09
+    
 failureR
@@ -1303,17 +1303,18 @@
 ct: '/'%0A
-%09
+  
 %7D), func
@@ -1331,18 +1331,20 @@
  res) %7B%0A
-%09%09
+    
 res.redi
@@ -1358,15 +1358,17 @@
 ');%0A
-%09
+  
 %7D);%0A%0A
-%09
+  
 app.
@@ -1404,18 +1404,20 @@
  res) %7B%0A
-%09%09
+    
 req.logO
@@ -1426,10 +1426,12 @@
 ();%0A
-%09%09
+    
 res.
@@ -1449,9 +1449,10 @@
 ');%0A
-%09
+  
 %7D);%0A

Fix identation.