-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.js
More file actions
206 lines (180 loc) · 6.23 KB
/
authentication.js
File metadata and controls
206 lines (180 loc) · 6.23 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(function(angular) {
var module = angular.module('dispatchbot.authentication', ['ngResource', 'ngCookies']);
/**
* Our authentication controllers.
*/
module.controller('LoginController', ['$scope', '$rootScope', '$window', '$location', 'Session', 'SessionStore', 'ShuttleBotConfig', function ($scope, $rootScope, $window, $location, Session, SessionStore, ShuttleBotConfig) {
var redirect = function($location, $window) {
var redirectTo = '/';
if ($window.sessionStorage.redirectAfterAuth) {
redirectTo = $window.sessionStorage.redirectAfterAuth;
delete $window.sessionStorage.redirectAfterAuth;
}
$location.path(redirectTo);
};
if (SessionStore.isLoggedIn()) {
redirect($location, $window);
return;
}
$scope.$on('dispatchbot.authentication.success', function (event, data) {
$rootScope.parentMessage = "You have logged in successfully.";
redirect($location, $window);
});
var host = function() {
var parts = ShuttleBotConfig.host.split('.');
if (parts.length > 2) {
return parts[1] + '.' + parts[2]
} else {
return ShuttleBotConfig.host
}
}
if (document.referrer == ShuttleBotConfig.protocol + host() + '/' && !$rootScope.redirectFromResolve) {
$rootScope.redirectFromResolve = true;
$rootScope.withOrganization.promise.then(function() {
$scope.message = "We identified that you are using ShuttleBot with " + $rootScope.organization.name + " and redirected you to their homepage. Please try to login again.";
});
}
$scope.$on('dispatchbot.authentication.failure', function (event, data) {
var desiredOrganizationKey = data.data.organization_key
if (desiredOrganizationKey) {
$window.location.href = ShuttleBotConfig.protocol + desiredOrganizationKey + "." + ShuttleBotConfig.host + "/#/login"
} else {
$scope.message = 'Error: Invalid user or password';
}
});
}])
.controller('LogoutController', ['$scope', '$window', '$location', 'Session', 'SessionStore', function($scope, $window, $location, Session, SessionStore) {
Session.logout(function() {
SessionStore.destroy();
})
}])
.controller('UnauthorizedController', ['$scope', function($scope) {
// Just render a view
}]);
/**
* The service.
*/
module.factory('SessionStore', ['$cookies', function($cookies) {
var SessionStore = {};
SessionStore.getUserId = function() {
return $cookies['user_id'];
};
SessionStore.getToken = function() {
return $cookies['token'];
};
SessionStore.getLogin = function() {
return $cookies['login'];
};
SessionStore.store = function(data) {
$cookies['token'] = data.token;
$cookies['login'] = data.login;
$cookies['user_id'] = data.user_id;
};
SessionStore.destroy = function() {
delete $cookies["user_id"]
delete $cookies["token"]
delete $cookies["login"]
};
SessionStore.isLoggedIn = function() {
return !!$cookies['token'];
};
return SessionStore;
}]);
module.factory('Session', ['$resource', 'DispatchBotConfig', '$window', function($resource, DispatchBotConfig, $window) {
return $resource(DispatchBotConfig.api_host + '/users/:action.json', {}, {
login: {
method: 'POST',
params: {
action: 'sign_in'
}
},
logout: {
method: 'DELETE',
params: {
action: 'sign_out'
}
}
});
}]);
module.factory('Organization', ['$resource', 'DispatchBotConfig', function($resource, DispatchBotConfig) {
return $resource(DispatchBotConfig.api_host + '/organizations', {}, {
lookup: {
method: 'GET',
url: DispatchBotConfig.api_host + '/organizations/key/:key.json'
},
providers: {
method: 'GET',
isArray: true,
url: DispatchBotConfig.api_host + '/organizations/:id/providers.json'
}
})
}]);
/**
* The interceptor.
*/
module.factory('authInterceptor', ['$rootScope', '$q', '$window', '$location', 'SessionStore', function ($rootScope, $q, $window, $location, SessionStore) {
var loginPath = '/login'; // TODO: This should not be hard-coded
var handle401 = function(response) {
SessionStore.destroy();
if ($location.path().toLowerCase() != loginPath) {
$window.sessionStorage.redirectAfterAuth = $location.path();
}
$rootScope.$broadcast('dispatchbot:authentication:unauthenticated', response);
};
var handle403 = function(response) {
$rootScope.$broadcast('dispatchbot:authorization:failure');
};
return {
request: function (config) {
config.headers = config.headers || {};
if (SessionStore.isLoggedIn()) {
config.headers['X-User-Email'] = SessionStore.getLogin();
config.headers['X-User-Token'] = SessionStore.getToken();
}
return config;
},
responseError: function (response) {
switch (response.status) {
case 401:
handle401(response);
break;
case 403:
handle403(response);
break;
}
return $q.reject(response);
}
};
}]);
module.directive('dbUserLogin', ['Session', 'SessionStore' , function(Session, SessionStore) {
return {
restrict: 'E',
templateUrl: function(elem,attrs) {
return attrs.dbTemplateUrl
},
scope: {
organization: "=dbOrganization",
},
link: function(scope, element, attributes, ngModel) {
scope.user = {};
scope.submit = function() {
if (scope.organization) {
scope.user.organization_id = scope.organization.id;
}
Session.login(
{ user: scope.user},
function (data, status, headers, config) {
SessionStore.store(data);
scope.$emit("dispatchbot.authentication.success", data);
},
function (data, status, headers, config) {
// Erase the token if the user fails to log in
SessionStore.destroy();
scope.$emit("dispatchbot.authentication.failure", data);
}
);
}
}
};
}]);
})(angular);