Commit 35387f3081af4278d6a9566867b4ec7170285b30

Authored by Andrew Buss
1 parent 5132f0e87c

cleaned up userservice stuff

Showing 5 changed files with 48 additions and 41 deletions Side-by-side Diff

... ... @@ -40,11 +40,19 @@
40 40 };
41 41 });
42 42 $locationProvider.html5Mode(true);
43   - $urlRouterProvider.otherwise('/login');
  43 + $urlRouterProvider.otherwise('/404');
44 44 var auth_resolve = {
45   - authorize: function($q, UserService) {
  45 + authorize: function($q, $state, UserService) {
46 46 console.log('resolving user before continuing');
47   - return UserService.getUserData();
  47 + var redirectAsNeeded = function() {
  48 + if (!UserService.isLoggedIn()) {
  49 + console.log(UserService.getUserData());
  50 + console.log('making the user log in');
  51 + $state.go('login');
  52 + }
  53 + };
  54 + if (UserService.isResolved()) return redirectAsNeeded();
  55 + return UserService.getUserData().then(redirectAsNeeded);
48 56 }
49 57 };
50 58 $stateProvider.
51 59  
... ... @@ -116,22 +124,13 @@
116 124 url: '/verifyemail/{key}',
117 125 templateUrl: 'templates/verifyemail.html',
118 126 controller: 'VerifyEmailController'
  127 + }).
  128 + state('404', {
  129 + url: '/404',
  130 + template: "<h1>This page doesn't exist!</h1>"
119 131 });
120 132 }).
121 133 run(function($rootScope, $state, $stateParams, $location, UserService) {
122   - $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) {
123   - if (toState.name == 'login') {
124   - if (UserService.isLoggedIn()) return console.log('already logged in') + $state.go('addclass');
125   - else return console.log('we are going to login anyway');
126   - }
127   - if (UserService.isLoggedIn()) return console.log('no login required; going straight to ' + toState.name);
128   - $rootScope.returnToState = toState;
129   - $rootScope.returnToStateParams = toStateParams;
130   - console.log('going to ' + toState.name + ' after login');
131   - event.preventDefault();
132   - $state.go('login');
133   -
134   - });
135 134 $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
136 135 console.log('failed to change state: ' + error);
137 136 $state.go('login');
... ... @@ -50,19 +50,20 @@
50 50 </ul>
51 51  
52 52 <!-- Slide-in side-nav for small screens -->
53   - <ul ng-show="currentSection.id && UserService.isLoggedIn()" class="side-nav" id="mobile-demo">
54   - <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped"
55   - ><i
56   - class="mdi-action-view-module left"></i>Feed</a></li>
57   - <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped"
58   - ><i
59   - class="mdi-action-view-carousel left"></i>Deck</a></li>
60   - <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped"
61   - ><i
62   - class="mdi-action-view-list left"></i>Card List</a>
63   - </li>
64   - <hr>
65   -
  53 + <ul class="side-nav" id="mobile-demo">
  54 + <span ng-show="currentSection.id && UserService.isLoggedIn()">
  55 + <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped"
  56 + ><i
  57 + class="mdi-action-view-module left"></i>Feed</a></li>
  58 + <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped"
  59 + ><i
  60 + class="mdi-action-view-carousel left"></i>Deck</a></li>
  61 + <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped"
  62 + ><i
  63 + class="mdi-action-view-list left"></i>Card List</a>
  64 + </li>
  65 + <hr>
  66 + </span>
66 67 <!-- Collapsible menu for all the User's classes -->
67 68 <ul class="collapsible" data-collapsible="accordion">
68 69 <li class="bold">
scripts/UserService.js View file @ 35387f3
1 1 angular.module('flashy.UserService', ['ui.router']).
2 2 service('UserService', function($rootScope, $http, $q) {
3 3 var deferred = $q.defer();
4   -
  4 + var _user = false;
5 5 $http.get('/api/me/').success(function(data) {
6 6 console.log('user is logged in!');
7 7 _user = data;
8 8  
9 9  
10 10  
11 11  
12 12  
... ... @@ -9,22 +9,26 @@
9 9 }).error(function(data) {
10 10 console.log(data);
11 11 console.log('not logged in yet: ' + data.detail);
12   - deferred.resolve({});
  12 + deferred.resolve({email: false});
13 13 });
14 14  
15   - var _user = deferred.promise;
16   -
  15 + this.isResolved = function() {
  16 + return !!_user;
  17 + };
17 18 this.getUserData = function() {
18   - return _user;
  19 + if (this.isResolved()) return _user;
  20 + else return deferred.promise;
19 21 };
20 22 this.setUserData = function(data) {
21 23 _user = data;
  24 + deferred.resolve(data);
22 25 };
23 26 this.logout = function() {
24   - _user = {};
  27 + _user = false;
  28 + deferred.resolve({});
25 29 };
26 30 this.isLoggedIn = function() {
27   - rv = angular.isDefined(_user.email);
  31 + rv = this.isResolved() && _user.email;
28 32 return rv;
29 33 };
30 34 });
templates/flashcard.html View file @ 35387f3
1 1 <div class="card flashy smallify" ng-init="startShrink = false"
2 2 ng-class="{'shrinky': startShrink}">
3   - <div class="card-content">
4   - <p>{{flashcard.text}}</p>
  3 + <div class="valign-wrapper">
  4 + <div class="card-content valign">
  5 + <p>{{flashcard.text}}</p>
  6 + </div>
5 7 </div>
6 8 <div class="card-overlay">
7 9 <div class="top-box no-user-select"
templates/login.html View file @ 35387f3
1   -<div class="row" style="margin-top:64px;">
2   - <div class="offset-m4 col m4">
  1 +<div class="" style="margin-top:32px;">
  2 + <div class="row" style="max-width:500px; width:50%;margin: 0 auto">
3 3 <ul class="tabs">
4 4 <li class="tab col s6"><a href="#register-tab">Sign Up</a></li>
5 5 <li class="tab col s6"><a class="active" href="#login-tab">Login</a></li>
... ... @@ -14,7 +14,8 @@
14 14 </div>
15 15  
16 16 <div class="input-field">
17   - <input id="email" type="email" name="login" class="validate" ng-model="loginEmail" required autofocus autocomplete/>
  17 + <input id="email" type="email" name="login" class="validate" ng-model="loginEmail" required autofocus
  18 + autocomplete/>
18 19 <label for="email">Email</label>
19 20 </div>
20 21 <div class="input-field">