-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (64 loc) · 2.62 KB
/
Copy pathapp.js
File metadata and controls
84 lines (64 loc) · 2.62 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
/**
* Created by bushan on 6/5/16.
*/
'use strict';
angular
.module('authApp', ['auth0', 'angular-storage', 'angular-jwt', 'ngMaterial', 'ui.router'])
.config(function ($provide, authProvider, $urlRouterProvider, $stateProvider, $httpProvider, jwtInterceptorProvider) {
$urlRouterProvider.otherwise('/home');
//jwtInterseptor adds autheriazation header to all hhtp request
jwtInterceptorProvider.tokenGetter = function (store) {
return store.get('id_token');
}
// passing the config object to auth provider
authProvider.init({
domain: 'testjcs.auth0.com',
clientId: 'SWvArQDueSGeLIY5j1oVQAgZy6Jg6fYt'
});
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'components/home/home.tpl.html'
})
.state('profile', {
url: '/profile',
templateUrl: 'components/profile/profile.tpl.html',
controller: 'profileController as user'
});
// handling redirect when we get 401 unauthrized error form the backend
function redirect($q,$injector,auth,store,$location){
return {
responseError: function(rejection){
if(rejection.status==401){
auth.signout();
store.remove('profile');
store.remove('id_token');
$location.path('/home');
}
return $q.reject(rejection);
}
}
}
$provide.factory('redirect',redirect);
// $httpProvider.interceptors.push('redirect');
// for some reason redirect on http 401 response in not working
//error is
// we have to push the jwt interseptor to the http request
// jwtInterceptor is the name of the interceptor
$httpProvider.interceptors.push('jwtInterceptor');
}) // we capture the change in url by rootscope every time the url changes we check if the token is present and it is not expired
.run(function ($rootScope, auth, jwtHelper, $location, store) {
$rootScope.$on('$locationChangeStart', function () {
var token = store.get('id_token');
if (token) {
if (!jwtHelper.isTokenExpired(token)) {
if (!auth.isAuthenticated) {
auth.authenticate(store.get('profile'), token);
}
}
}
else {
$location.path('/home');
}
})
});