Commit 9e9dd40af4427b1001d4a64b827c4f65db03c45d

Authored by Andrew Buss
1 parent bd83bd63c5

save a user's last state to show them when they log in next

Showing 6 changed files with 54 additions and 46 deletions Side-by-side Diff

... ... @@ -13,7 +13,7 @@
13 13 'flashy.VerifyEmailController',
14 14 'flashy.CardListController',
15 15 'ngCookies']).
16   - config(function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {
  16 + config(function ($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {
17 17 'use strict';
18 18 $httpProvider.defaults.withCredentials = true;
19 19 $httpProvider.defaults.xsrfCookieName = 'csrftoken';
20 20  
... ... @@ -27,9 +27,9 @@
27 27 }
28 28 };
29 29  
30   - $httpProvider.interceptors.push(function($q) {
  30 + $httpProvider.interceptors.push(function ($q) {
31 31 return {
32   - 'responseError': function(rejection) {
  32 + 'responseError': function (rejection) {
33 33 if (rejection.status >= 500) {
34 34 console.log('got error');
35 35 console.log(rejection);
36 36  
... ... @@ -42,9 +42,9 @@
42 42 $locationProvider.html5Mode(true);
43 43 $urlRouterProvider.otherwise('/404');
44 44 var auth_resolve = {
45   - authorize: function($q, $state, UserService) {
  45 + authorize: function ($q, $state, UserService) {
46 46 console.log('resolving user before continuing');
47   - var redirectAsNeeded = function() {
  47 + var redirectAsNeeded = function () {
48 48 if (!UserService.isLoggedIn()) {
49 49 console.log(UserService.getUserData());
50 50 console.log('making the user log in');
... ... @@ -69,8 +69,7 @@
69 69 }).
70 70 state('root', {
71 71 resolve: auth_resolve,
72   - url: '',
73   - templateUrl: 'templates/root.html',
  72 + url: '/',
74 73 controller: 'RootController'
75 74 }).
76 75 state('feed', {
77 76  
... ... @@ -130,10 +129,16 @@
130 129 template: "<h1>This page doesn't exist!</h1>"
131 130 });
132 131 }).
133   - run(function($rootScope, $state, $stateParams, $location, UserService) {
134   - $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
  132 + run(function ($rootScope, $state, $stateParams, $location, UserService) {
  133 + $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
135 134 console.log('failed to change state: ' + error);
136 135 $state.go('login');
  136 + });
  137 + $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
  138 + if (['feed', 'deck', 'cardlist'].indexOf(toState.name) >= 0) {
  139 + localStorage.setItem('last_state', toState.name);
  140 + localStorage.setItem('last_state_params', JSON.stringify(toParams));
  141 + }
137 142 });
138 143 });
scripts/FeedController.js View file @ 9e9dd40
1 1 angular.module('flashy.FeedController', ['ui.router']).
2 2 controller('FeedController', function($scope, $rootScope, $stateParams, $state, $http, UserService) {
3   - console.log('Hello from feed');
4 3 sectionId = $stateParams.sectionId;
5 4 $rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId});
6 5 $scope.cards = false;
... ... @@ -35,7 +34,6 @@
35 34 success(function(data) {
36 35 console.log(data);
37 36 $scope.cards = data;
38   -
39 37 }).
40 38 error(function(err) {
41 39 console.log('pulling feed failed');
... ... @@ -59,7 +57,6 @@
59 57 i += node.data.length;
60 58 }
61 59 });
62   - console.log(blanks);
63 60 text = $('#new-card-input').text();
64 61 var myCard = {
65 62 'text': text,
... ... @@ -79,16 +76,6 @@
79 76 listenForC = true;
80 77 $('#new-card-input').html('');
81 78 };
82   -
83   - $http.get('/api/sections/' + sectionId + '/feed/').
84   - success(function(data) {
85   - console.log(data);
86   - $scope.cards = data;
87   - }).
88   - error(function(err) {
89   - console.log("couldn't get cards!");
90   - });
91   -
92 79 /* Key bindings for the whole feed window. Hotkey it up! */
93 80 var listenForC = true;
94 81  
scripts/FlashcardDirective.js View file @ 9e9dd40
... ... @@ -23,7 +23,6 @@
23 23 angular.element($window).bind('resize', refresh_width);
24 24 refresh_width();
25 25 scope.textPieces = [];
26   - console.log(scope.flashcard);
27 26 scope.flashcard.mask.sort(function(a, b) {
28 27 return a[0] - b[0];
29 28 });
scripts/LoginController.js View file @ 9e9dd40
1 1 angular.module('flashy.LoginController', ['ui.router']).
2 2  
3 3 controller('LoginController', ['$rootScope', '$scope', '$state', '$http', 'UserService',
4   - function($rootScope, $scope, $state, $http, UserService) {
  4 + function ($rootScope, $scope, $state, $http, UserService) {
5 5 'use strict';
6 6 // If we're logged in, there's nothing to do here
7 7 if (UserService.isLoggedIn()) $state.go('addclass');
8 8 $scope.uniqueError = false;
9 9 $scope.loginError = false;
10   - $scope.login = function(email, password) {
  10 + $scope.login = function (email, password) {
11 11 $http.post('/api/login/', JSON.stringify({
12 12 'email': email,
13 13 'password': password
14 14 })).
15   - success(function(data) {
  15 + success(function (data) {
16 16 UserService.login(data);
17 17 if (angular.isDefined($scope.returnToState))
18 18 $state.go($scope.returnToState.name, $scope.returnToStateParams);
19   - else $state.go('addclass');
  19 + else
  20 + UserService.redirectToDefaultState($state);
20 21 }).
21   - error(function(data, status, header, config) {
  22 + error(function (data, status, header, config) {
22 23 if (data.detail) { // assume 'invalid email or pass'
23 24 $scope.loginError = true;
24 25 }
25 26 console.log(data);
26 27 });
27 28 };
28   - $scope.signUp = function(email, password) {
  29 + $scope.signUp = function (email, password) {
29 30 $http.post('/api/register/', JSON.stringify({
30 31 'email': email,
31 32 'password': password
32 33 })).
33   - success(function(data) {
  34 + success(function (data) {
34 35 UserService.login(data);
35 36 if (angular.isDefined($scope.returnToState))
36 37 $state.go($scope.returnToState.name, $scope.returnToStateParams);
37 38 else $state.go('addclass');
38 39  
39 40 }).
40   - error(function(data, status, headers, config) {
  41 + error(function (data, status, headers, config) {
41 42 console.log(data.email);
42 43 if (data.email == 'This field is required.') {
43 44 $scope.invalid = true;
44 45  
... ... @@ -51,10 +52,10 @@
51 52 });
52 53  
53 54 };
54   - $scope.triggerPasswordReset = function() {
  55 + $scope.triggerPasswordReset = function () {
55 56 $state.go('requestpasswordreset');
56 57 };
57   - $(document).ready(function() {
  58 + $(document).ready(function () {
58 59 $('ul.tabs').tabs();
59 60 });
60 61 }
scripts/RootController.js View file @ 9e9dd40
1 1 angular.module('flashy.RootController', ['ui.router', 'ngResource']).
2 2  
3   - controller('RootController', function($rootScope, $resource, $scope, $state, UserService) {
  3 + controller('RootController', function ($rootScope, $resource, $scope, $state, UserService) {
4 4 $rootScope.SectionResource = $resource('/api/sections/:sectionId/');
5 5 window.rootscope = $rootScope;
6 6 window.userservice = UserService;
7 7 $rootScope.currentSection = {};
8 8 $scope.UserService = UserService;
9   - UserService.getUserData().then(function(data) {
10   - $scope.user = data;
11   - });
  9 +
  10 +
12 11 //UserService.getUserData().then(function(data) {
13 12 // console.log(data);
14 13 // $rootScope.user = data;
... ... @@ -19,5 +18,15 @@
19 18 closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor
20 19 }
21 20 );
  21 + var postlogin = function (data) {
  22 + $scope.user = data;
  23 + UserService.redirectToDefaultState($state);
  24 + };
  25 + if (UserService.isLoggedIn()) {
  26 + postlogin(UserService.getUserData());
  27 + } else {
  28 + UserService.getUserData().then(postlogin);
  29 + }
  30 +
22 31 });
scripts/UserService.js View file @ 9e9dd40
1 1 angular.module('flashy.UserService', ['ui.router']).
2   - service('UserService', function($rootScope, $http, $q) {
  2 + service('UserService', function ($rootScope, $http, $q) {
3 3 var deferred = $q.defer();
4 4 var _user = false;
5   - $http.get('/api/me/').success(function(data) {
  5 + $http.get('/api/me/').success(function (data) {
6 6 console.log('user is logged in!');
7 7 _user = data;
8 8 deferred.resolve(data);
9   - }).error(function(data) {
  9 + }).error(function (data) {
10 10 console.log(data);
11 11 console.log('not logged in yet: ' + data.detail);
12 12 _user = {email: false};
13 13 deferred.resolve(_user);
14 14 });
15 15  
16   - this.isResolved = function() {
  16 + this.isResolved = function () {
17 17 return !!_user;
18 18 };
19   - this.getUserData = function() {
  19 + this.getUserData = function () {
20 20 if (this.isResolved()) return _user;
21 21 else return deferred.promise;
22 22 };
23   - this.login = function(data) {
  23 + this.login = function (data) {
24 24 _user = data;
25 25 if (!data.is_confirmed) {
26 26 Materialize.toast('Please verify your email address!', 4000);
27 27 }
28 28 deferred.resolve(data);
29 29 };
30   - this.hasVerifiedEmail = function() {
  30 + this.hasVerifiedEmail = function () {
31 31 return this.isResolved() && _user.is_confirmed;
32 32 };
33   - this.logout = function() {
  33 + this.logout = function () {
34 34 _user = false;
35 35 deferred.resolve({});
36 36 };
37   - this.isLoggedIn = function() {
  37 + this.isLoggedIn = function () {
38 38 rv = this.isResolved() && _user.email;
39 39 return rv;
  40 + };
  41 + this.redirectToDefaultState = function ($state) {
  42 + if (!this.isLoggedIn()) return $state.go('login');
  43 + if (!_user.sections.length) return $state.go('addclass');
  44 + last_state = localStorage.getItem('last_state');
  45 + if (last_state) return $state.go(last_state, JSON.parse(localStorage.getItem('last_state_params')));
  46 + $state.go('feed', {sectionId: _user.sections[0].id});
40 47 };
41 48 });