-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseAuth.js
301 lines (287 loc) · 12.1 KB
/
BaseAuth.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @module BaseAuth
* Simple authentication services for login/logout <br />
*
* company.services.auth or easy access with BaseAuth <br />
* Requirements: Jquery
*
* //1) To authenticate on login <br />
* window.company.services.BaseAuth.login() <br />
* //2) To authenticate on logout <br />
* window.company.services.BaseAuth.logout() <br />
*
* //3) To authenticate only <br />
* window.company.services.BaseAuth.isAuthenticated() <br />
*
* //4) To getToken <br />
* var token = window.company.services.BaseAuth.getToken() <br />
*
* @desc can change 'company' to your domain name
*/
(function(ctx){// MODULE DESIGN PATTERN
ctx.services = ctx.services || {};
/**
* @lends module: BaseAuth
*/
ctx.services.BaseAuth = ctx.services.BaseAuth || function(opts){
return {
/**
* @type {Object}
* @description custom default options, can be extended.
*/
options: {
/**
* @type {String|URL}
* @description Your backend server URL to do authentication from.
*/
serverUrl: opts.serverUrl || "http://localhost:8081",
/**
* @type {String|URL}
* @description Your backend server URL to do authentication from.
*/
loginUrl: opts.loginUrl || "/api/auth/login",
/**
* @type {String|URL}
* @description Your backend server URL to do authentication from.
*/
logoutUrl: opts.logoutUrl || "/api/auth/logout"
},
/**
* @type {String}
* @description Token coming back from the server
*/
token: undefined,
/**
* @type {Boolean}
* @description Check user session is authenticated or not. Default to false.
*/
isAuth: false,
/**
*
* @param {Object} opt Options to override the default property
* @method
*/
init: function (opt) {
$.extend(this.options, options);//override options
},
/**
*
* @description reset all headers injection from subsequent Ajax call
* @example
* Auth.resetHeaders();
* $.ajax("http://something"); <-- won't have any headers pre-set
*/
resetHeaders: function () {
$.ajaxSetup({
headers: {}
});
},
/**
* @description remove any token in user's sessionStorage
*/
resetSession: function () {
if (sessionStorage !== undefined) {//remove from storage and set auth=false
sessionStorage.removeItem("x-auth-token");
}
this.isAuth = false;
this.token = undefined;
},
/**
* @description inject the token as header on all subsequent ajax call
* this method will be automatically invoke on successful login
*/
setHeaders: function () {
var parent = this;
$.ajaxSetup({
headers: {//Save token into the header
"X-AUTH-TOKEN": this.token
}
});
// make sure user get redirected when login is expired
$(document).ajaxComplete(function (event, xhr, settings) {
var data = xhr.responseJSON || xhr.responseText;
if (typeof (data) === "object" || xhr.status === 0) {
if ((data.hasOwnProperty("status") && data.status === 401) || xhr.status === 0) {
parent.resetHeaders();
parent.resetSession();
// this mean unauthorized, go back to login page
window.location.href = parent.redirectUrl;
}
}
});
},
/**
* @description pick up any session stored on sessionStorage, this allow
* multi spa to share same authentication token.
*/
setSession: function () {
// also need to keep this in sessionStorage or html5 web storage
//sessionstorage for now
if (sessionStorage !== undefined &&
typeof this.token !== "undefined") {//Create if now in sessionStorage yet, and set auth=true
sessionStorage.setItem("x-auth-token", this.token);
this.isAuth = true;
}
},
/**
*
* @description Login into the system which also involves adding the correct
* token into user's sessionStorage (on successful login)
* @param {String} username Username to login to the system
* @param {String} password Password
* @param {String} hostUrl (Optional) to give different server authentication URL
* @returns {jQuery.Deferred} It will gives back deferred object in which can be
* chain with any .done or .fail callback
*
* @example
*
window.company.services.BaseAuth
.login({
username: "longh",
password: "password"
})
.done(function (response) {
//OK
})
.fail(function (response) {
//FAILED
});
*/
login: function (data, hostUrl) {
var parent = this;
if (hostUrl !== undefined) {
this.options.serverUrl = hostUrl;
}
if (data === undefined) {
data = {};
}
// sending for login
var deferred = new jQuery.Deferred();
$.ajax({
type: "POST",//Access-Control-Request-Method:
url: this.options.serverUrl + this.options.loginUrl,
data: data,//
headers: {},//Access-Control-Request-Headers:
processData: true
}).done(function (response) {
// extract json w/ token (JWT) from response
if (response.hasOwnProperty("token")) {
parent.token = response.token;
}
console.log('setting up headers...');
// setup the on next upcoming access with correct header
parent.setHeaders();//set token to header
parent.setSession();//save token to sessionStorage and set auth to true
deferred.resolve(true);
}).fail(function (response) {
deferred.reject(response.responseJSON || response.responseText);
}).always(function () {
//something
});
/* UNCOMMENT TO TEST - COMMENT ALL $ajax above to
this.token = "JhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjEzODY4OTkxMzEsImlzcyI6ImppcmE6MTU0ODk1OTUiLCJxc2giOiI4MDYzZmY0Y2ExZTQxZGY3YmM5MGM4YWI2ZDBmNjIwN2Q0OTFjZjZkYWQ3YzY2ZWE3OTdiNDYxNGI3MTkyMmU5IiwiaWF0IjoxMzg2ODk4OTUxfQ.uKqU9dTB6gKwG6jQCuXYAiMNdfNRw98Hw_IWuA5MaMo";
parent.setHeaders();//set token to header
parent.setSession();//save token to sessionStorage and set auth to true
debugger;//check the SessionStorage on chrome debugger => Resource => Session Storage
*/
return deferred.promise();
},
/**
* @description Logout completely from the system and remove any token in
* sessionStorage
* @param {URL} hostUrl (Optional)
* @returns {jQuery.Deferred}
*
* @example
window.company.services.BaseAuth.logout();
*/
logout: function (hostUrl) {
var parent = this;
if (hostUrl !== undefined) {
this.options.serverUrl = hostUrl;
}
// do another query
var deferred = new jQuery.Deferred();
$.ajax({
type: "GET",//Access-Control-Request-Method:
url: this.options.serverUrl + this.options.logoutUrl,
processData: true,
}).done(function (resp) {
console.log('logout success...');
parent.resetHeaders();
parent.resetSession();
deferred.resolve();
}).fail(function (resp) {
console.log('logout fail...');
// should clear the session on unauthorized status code 401
if (resp && resp.status === 401) {
// 401 -- also means the token is invalid
parent.resetHeaders();//clear token from header
parent.resetSession();//clean the session
}
deferred.reject(resp.responseJSON || resp.responseText);
});
/* UNCOMMENT TO HARD TEST - COMMENT ALL $ajax above to
parent.resetHeaders();//clear token from header
parent.resetSession();//clean the session
debugger;//check the SessionStorage on chrome debugger => Resource => Session Storage
*/
return deferred;
},
/**
* @description Checks whether current user session has been authenticated or not
* by checking the existence of user token.
* @returns {Boolean}
*
* @example
*
*
//If not authenticated route to login page
if (!BaseAuth.isAuthenticated()) {
window.location.href = this.loginUrl;
return false;
}
*/
isAuthenticated: function () {
if (!this.authenticated && sessionStorage !== undefined &&
sessionStorage.getItem("x-auth-token") !== null) {
//retrieve token from sessionStorage and add into header
this.token = sessionStorage.getItem("x-auth-token");
this.setHeaders();
// temporary make it authenticated, any subsequent ajax request
// will re-validate for the token
this.isAuth = true;
}
return this.isAuth;
},
/**
* @description Returns current user token
* @returns {String}
*/
getToken: function () {
return this.token;
},
/**
* @description Returns current server url
* @returns {String}
*/
getServerUrl: function () {
return this.options.serverUrl;
}
};
};
})(window.company = window.company || {});
/*//UNCOMMENT BELOW TO TEST: for global access from window, and namespace access
var oneAuth = new window.company.services.BaseAuth({
serverUrl: "http://localhost:8081"
});
console.log(oneAuth.isAuthenticated());
console.log(oneAuth.getToken());
console.log(oneAuth.login({username: "longh",password: "password"}));//mock login
console.log(oneAuth.isAuthenticated());
console.log(oneAuth.getToken());
console.log(oneAuth.getServerUrl());
console.log(oneAuth.logout());//mock logout
console.log(oneAuth.isAuthenticated());
console.log(oneAuth.getToken());
*/