/
app.js
121 lines (101 loc) · 3.85 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
// Declare app level module which depends on filters, and services
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(function($routeProvider, $httpProvider) {
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: LoginCtrl});
$routeProvider.when('/content', {templateUrl: 'partials/content.html', controller: ContentCtrl});
$routeProvider.when('/create', {templateUrl: 'partials/createaccount.html', controller: CreateAccountCtrl});
$routeProvider.otherwise({redirectTo: '/login'});
// ==== CODE TO DO 401 NOT LOGGED IN CHECKING
//This code will intercept 401 unauthorized errors returned from web requests.
//On default any 401 will make the app think it is not logged in.
var interceptor = ['$rootScope','$q', function(scope, $q) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 401) {
var deferred = $q.defer();
var req = {
config: response.config,
deferred: deferred
}
scope.$broadcast('event:loginRequired');
return deferred.promise;
}
// otherwise
return $q.reject(response);
}
return function(promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}).run(['$rootScope', '$http', '$location', function(scope, $http, $location) {
/**
* Holds page you were on when 401 occured.
* This is good because, for example:
* User goes to protected content page, for example in a bookmark.
* 401 triggers relog, this will send them back where they wanted to go in the first place.
*/
scope.pageWhen401 = "";
scope.loggedIn = false;
scope.logout = function(){
console.log("Logout request.");
$http.get('php/logout.php').success(function(data){
scope.$broadcast('event:doCheckLogin');
}).error(function(data){
scope.$broadcast('event:doCheckLogin');
});
}
scope.$on('event:loginRequired', function(){
scope.loggedIn = false;
//Only redirect if we aren't on create or login pages.
if($location.path() == "/create" || $location.path() == "/login")
return;
scope.pageWhen401 = $location.path();
//go to the login page
$location.path('/login').replace();
});
/**
* On 'event:loginConfirmed', return to the page.
*/
scope.$on('event:loginConfirmed', function() {
scope.loggedIn = true;
console.log("Login confirmed!");
if($location.path() == "/create" || $location.path() == "/login"){
//return to the page
if(scope.pageWhen401 != ""){
$location.path(scope.pageWhen401).replace();
scope.pageWhen401 = "";
}else{
$location.path('/content').replace();
}
}
});
/**
* On 'logoutRequest' invoke logout on the server and broadcast 'event:loginRequired'.
*/
scope.$on('event:logoutRequest', function() {
scope.logout();
});
scope.$on("$locationChangeSuccess", function(event){
//event.preventDefault();
ping();
});
scope.$on('event:doCheckLogin', function(){
ping();
});
/**
* Ping server to figure out if user is already logged in.
*/
function ping() {
$http.get('php/util/checkSession.php').success(function() {
scope.$broadcast('event:loginConfirmed');
}); //If it fails the interceptor will automatically redirect you.
}
//Finally check the logged in state on every load
ping();
}
]);