From bc596fb4fd4425725fe311abb282245b176f0292 Mon Sep 17 00:00:00 2001
From: Andrew Buss <abuss@ucsd.edu>
Date: Mon, 1 Jun 2015 01:58:56 -0700
Subject: [PATCH] hopefully resolve user before visiting login

---
 config.js              | 24 ++++++++++++++----------
 scripts/UserService.js | 40 ++++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/config.js b/config.js
index 54f0361..f699a0f 100644
--- a/config.js
+++ b/config.js
@@ -16,7 +16,7 @@ angular.module('flashy', [
     'flashy.HelpController',
     'flashy.SettingsController',
     'ngCookies']).
-    config(function ($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) {
+    config(function($stateProvider, $urlRouterProvider, $resourceProvider, $httpProvider, $locationProvider) {
         'use strict';
         $httpProvider.defaults.withCredentials = true;
         $httpProvider.defaults.xsrfCookieName = 'csrftoken';
@@ -30,9 +30,9 @@ angular.module('flashy', [
             }
         }
 
-        $httpProvider.interceptors.push(function ($q, $rootScope) {
+        $httpProvider.interceptors.push(function($q, $rootScope) {
             return {
-                'responseError': function (rejection) { // need a better redirect
+                'responseError': function(rejection) { // need a better redirect
                     if (rejection.status >= 500) {
                         console.log('got error');
                         console.log(rejection);
@@ -52,12 +52,14 @@ angular.module('flashy', [
         $locationProvider.html5Mode(true);
         $urlRouterProvider.otherwise('/404');
         var auth_resolve = {
-            authorize: function ($q, $state, $stateParams, UserService) {
-                if (UserService.noAuthRequired($state)) {
-                    return console.log('no auth state ' + $state.name);
+            authorize: function($q, $rootScope, $state, $stateParams, UserService) {
+
+                console.log('do we need to authorize a user for', $rootScope.nextState.name);
+                if (UserService.noAuthRequired($rootScope.nextState)) {
+                    return console.log('no auth state', $rootScope.nextState.name);
                 }
                 console.log('resolving user before continuing for ' + $state.name);
-                var redirectAsNeeded = function () {
+                var redirectAsNeeded = function() {
                     if (!UserService.isLoggedIn()) {
                         console.log(UserService.getUserData());
                         console.log('making the user log in');
@@ -152,12 +154,14 @@ angular.module('flashy', [
                 controller: 'HelpController'
             });
     }).
-    run(function ($rootScope, $state, $stateParams, $location, UserService) {
-        $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
+    run(function($rootScope, $state, $stateParams, $location, UserService) {
+        $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
             console.log('failed to change state: ' + error);
             $state.go('login');
         });
-        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
+        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
+            $rootScope.nextState = toState;
+            console.log('changing state to', toState);
             if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) {
                 localStorage.setItem('last_state', toState.name);
                 localStorage.setItem('last_state_params', JSON.stringify(toParams));
diff --git a/scripts/UserService.js b/scripts/UserService.js
index 855faa5..4bf6545 100644
--- a/scripts/UserService.js
+++ b/scripts/UserService.js
@@ -1,8 +1,8 @@
 angular.module('flashy.UserService', ['ui.router']).
-    service('UserService', function ($rootScope, $http, $q) {
+    service('UserService', function($rootScope, $http, $q) {
         var deferred = $q.defer();
         var _user = false;
-        var login = function (data) {
+        var login = function(data) {
             if (data.locked) {
                 $rootScope.UserService.showLockedMessage();
                 return deferred.reject('account locked');
@@ -13,54 +13,54 @@ angular.module('flashy.UserService', ['ui.router']).
                     'Resend Verification Email</a>', 4000);
             }
             _user = data;
-            _user.sectionIdList = _user.sections.map(function (x) {
+            _user.sectionIdList = _user.sections.map(function(x) {
                 return x.id;
             });
             deferred.resolve(data);
         };
         this.login = login;
-        $http.get('/api/me/').success(function (data) {
+        $http.get('/api/me/').success(function(data) {
             console.log('user is logged in!');
             login(data);
-        }).error(function (data) {
+        }).error(function(data) {
             console.log(data);
             console.log('not logged in yet: ' + data.detail);
             _user = {email: false};
             deferred.resolve(_user);
         });
 
-        this.isResolved = function () {
+        this.isResolved = function() {
             return !!_user;
         };
-        this.getUserData = function () {
+        this.getUserData = function() {
             if (this.isResolved()) return _user;
             else return deferred.promise;
         };
-        this.hasVerifiedEmail = function () {
+        this.hasVerifiedEmail = function() {
             return this.isResolved() && _user.is_confirmed;
         };
-        this.logout = function ($state) {
-            $http.post('/api/logout/').success(function () {
+        this.logout = function($state) {
+            $http.post('/api/logout/').success(function() {
                 if (!_user.locked)Materialize.toast('Logged out!', 1000);
-            }).error(function () {
+            }).error(function() {
                 console.log('Problem logging out');
             });
             _user = false;
             deferred.resolve({});
             $state.go('login');
         };
-        this.addClass = function (section) {
+        this.addClass = function(section) {
             _user.sections.push(section);
             _user.sectionIdList.push(section.id);
         };
-        this.isLoggedIn = function () {
+        this.isLoggedIn = function() {
             rv = this.isResolved() && _user.email;
             return rv;
         };
-        this.isInSection = function (sectionId) {
+        this.isInSection = function(sectionId) {
             return (_user.sectionIdList.indexOf(sectionId) >= 0);
         };
-        this.redirectToDefaultState = function ($state) {
+        this.redirectToDefaultState = function($state) {
             console.log('redirecting user to their default state');
             if (!this.isLoggedIn()) return $state.go('login');
             if (!_user.sections.length) return $state.go('addclass');
@@ -73,7 +73,7 @@ angular.module('flashy.UserService', ['ui.router']).
             }
             $state.go('feed', {sectionId: _user.sections[0].id});
         };
-        this.authorizedFor = function (state, stateParams) {
+        this.authorizedFor = function(state, stateParams) {
             if (['feed', 'deck', 'cardlist'].indexOf(state.name) >= 0) {
                 if (_user.sectionIdList.indexOf(stateParams.sectionId) < 0) {
                     return false;
@@ -81,20 +81,20 @@ angular.module('flashy.UserService', ['ui.router']).
             }
             return true;
         };
-        this.showLockedMessage = function () {
+        this.showLockedMessage = function() {
             Materialize.toast('You must verify your email address before continuing.' +
                 '<a class="btn-flat cyan-text" onclick="rootscope.UserService.resendConfirmationEmail()">' +
                 'Resend Verification Email</a>', 4000);
         };
-        this.noAuthRequired = function (state) {
+        this.noAuthRequired = function(state) {
             if (['verifyemail', 'login'].indexOf(state.name) >= 0) {
                 return true;
             }
             return false;
         };
-        this.resendConfirmationEmail = function () {
+        this.resendConfirmationEmail = function() {
             console.log('Requesting resend of confirmation email');
-            $http.post('/api/resend_confirmation_email/').success(function () {
+            $http.post('/api/resend_confirmation_email/').success(function() {
                 Materialize.toast('Resent confirmation email! Check your spam folder too.', 4000);
             });
         };
-- 
1.9.1