-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
121 lines (103 loc) · 2.78 KB
/
app.js
File metadata and controls
121 lines (103 loc) · 2.78 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
;(function(){
function authInterceptor(API, auth) {
return {
// automatically attach Authorization header
request: function(config) {
var token=auth.getToken();
if(config.url.indexOf(API) === 0 && token){
config.headers.Authorization='Bearer '+token;
}
return config;
},
// If a token was sent back, save it
response: function(res) {
if(res.config.url.indexOf(API) === 0 && res.data.token){
auth.saveToken(res.data.token);
}
return res;
},
}
}
function authService($window) {
var self = this;
self.parseJwt=function(token){
var base64Url=token.split('.')[1];
var base64=base64Url.replace('-','+').replace('_','/');
return JSON.parse($window.atob(base64));
}
self.saveToken = function(token) {
$window.localStorage['jwtToken'] = token;
}
self.getToken = function(token){
return $window.localStorage['jwtToken'];
}
self.isAuthed=function(){
var token=self.getToken();
if(token){
var params=self.parseJwt(token);
return Math.round(new Date().getTime()/1000) <= params.exp;
}else{
return false;
}
}
self.logout = function() {
$window.localStorage.removeItem('jwtToken');
}
// Add JWT methods here
}
function userService($http, API, auth) {
var self = this;
self.getQuote = function() {
return $http.get(API + '/auth/quote')
}
self.register = function(username, password) {
return $http.post(API + '/auth/register', {
username: username,
password: password
})
}
self.login = function(username, password) {
return $http.post(API + '/auth/login', {
username: username,
password: password
})
};
// add authentication methods here
}
// We won't touch anything in here
function MainCtrl(user, auth) {
var self = this;
function handleRequest(res) {
var token = res.data ? res.data.token : null;
if(token) { console.log('JWT:', token); }
self.message = res.data.message;
}
self.login = function() {
user.login(self.username, self.password)
.then(handleRequest, handleRequest)
}
self.register = function() {
user.register(self.username, self.password)
.then(handleRequest, handleRequest)
}
self.getQuote = function() {
user.getQuote()
.then(handleRequest, handleRequest)
}
self.logout = function() {
auth.logout && auth.logout()
}
self.isAuthed = function() {
return auth.isAuthed ? auth.isAuthed() : false
}
}
angular.module('app', [])
.factory('authInterceptor', authInterceptor)
.service('user', userService)
.service('auth', authService)
.constant('API', 'http://test-routes.herokuapp.com')
.config(function($httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
})
.controller('Main', MainCtrl)
})();