-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-jwt.js
More file actions
112 lines (99 loc) · 3.21 KB
/
angular-jwt.js
File metadata and controls
112 lines (99 loc) · 3.21 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
(function(){
'use strict';
// Prepare the 'ngJwt' module for subsequent registration of controllers and delegates
angular.module('authJwt',
[
'ngRoute',
'ngResource',
'ngSanitize',
'ngBase64'
])
.value('apiDomain', '')
.value('apiAuthTokenUrl', '')
.value('apiAuthTokenRefreshUrl', '');
})();
(function(){
'use strict';
angular.module('authJwt')
.component('jwtComponent', {
bindings: {email: '<', password: '<'},
controller: JwtController,
controllerAs: 'auth',
template:'<div> <alert> <strong>{{ auth.ErrorMessage }}</strong></alert></div><div><button ng-click="auth.login(auth.email,auth.password)">Sign in</button></div>'
});
function JwtController($log, JwtModel, JwtService){
var self = this;
self.jwtservice = new JwtService();
self.jwtmodel = JwtModel;
self.login = function(email, password){
self.jwtservice.token(email, password)
.then(
function(d) {self.jwtmodel.hash = d.token;},
function(d) {self.ErrorMessage = "Sorry, we didn't recognize the email or password you entered. Please try again."}
)
.then(
function(d) {$log.log(self.jwtmodel.body);}
)
.finally(function (df) {$log.log('end of promise');});;
}
}
})();
(function(){
'use strict';
angular.module('authJwt')
.factory('JwtModel', ['$log','base64', JwtModel])
/**
* JWT Model
*
*
* @returns {{jwtModel: object}
* @constructor
*/
function JwtModel($log, base64){
var JwtModel = {
_token : '',
_array : '',
get hash(){return this._token},
set hash(value){
this._token = value;
this._array = value.split('.');
},
get header(){return base64.decode(this._array[0]);},
get body(){return base64.decode(this._array[1]);}
};
return JwtModel
}
})();
(function(){
'use strict';
angular.module('authJwt')
.service('JwtService', ['apiDomain','apiAuthTokenUrl','apiAuthTokenRefreshUrl','$http', '$q', '$log', JwtService]);
/**
* JWT Request Service
*
*
* @returns {{jwtAuthService: object}
* @constructor
*/
function JwtService(apiDomain,apiAuthTokenUrl,apiAuthTokenRefreshUrl,$http, $q, $log){
var JwtService = function(){
var self = this;
self.token = function(email, pass) {
var promise = $http.post(apiDomain + apiAuthTokenUrl, {email: email, password: pass}).then(function (response) {
$log.log(response)
return response.data;
});
return promise;
};
self.token_refresh = function(token) {
var promise = $http.post(apiDomain + apiAuthTokenRefreshUrl, {token: token}).then(function (response) {
return response.data;
});
return promise;
};
self.init = function(){};
return self.init();
}
return JwtService;
}
})();