1
1
2
2
jQuery ( document ) . ready ( function ( ) {
3
3
4
- // /Start ## Global AJAX Sender function ##################################
5
- var AjaxHttpSender = function ( ) { } ;
6
-
7
- AjaxHttpSender . prototype . sendAjax = function ( url , type , token , callback ) {
8
- $ . ajax ( {
9
- url : url ,
10
- type : type ,
11
- contentType : 'application/json; charset=utf-8' ,
12
- cache : false ,
13
- beforeSend : function ( request ) {
14
- callback . beforeSend ( request , token ) ;
15
- } ,
16
- error : function ( XMLHttpRequest , textStatus , errorThrown ) {
17
- callback . error ( XMLHttpRequest , errorThrown ) ;
18
- } ,
19
- success : function ( responseData , textStatus ) {
20
- callback . success ( responseData ) ;
21
- } ,
22
- complete : function ( XMLHttpRequest , textStatus ) {
23
- callback . complete ( ) ;
24
- }
25
- } ) ;
26
- }
27
- // /Stop ## Global AJAX Sender function ##################################
28
-
29
- // /Start ## Cookies functions ###########################################
30
- function setCookie ( name , value ) {
31
- var expiry = new Date ( new Date ( ) . getTime ( ) + 1800 * 1000 ) ; // plus 30 min
32
- if ( window . location . protocol == "https:" ) {
33
- document . cookie = name + "=" + escape ( value ) + "; path=/; expires=" + expiry . toGMTString ( ) + "; secure; HttpOnly" ;
34
- } else {
35
- document . cookie = name + "=" + escape ( value ) + "; path=/; expires=" + expiry . toGMTString ( ) ;
36
- }
37
- }
38
-
39
- function getCookie ( name ) {
40
- var re = new RegExp ( name + "=([^;]+)" ) ;
41
- var value = re . exec ( document . cookie ) ;
42
- return ( value != null ) ? unescape ( value [ 1 ] ) : null ;
43
- }
44
- // /Stop ## Cookies functions ############################################
45
-
46
4
// /Start ## Knockout ####################################################
47
5
function loginModel ( ) {
48
6
this . userState = {
@@ -52,28 +10,6 @@ jQuery(document).ready(function() {
52
10
this . remember = ko . observable ( false ) ;
53
11
54
12
this . login = function ( userState , remember ) {
55
- var callback = {
56
- beforeSend : function ( xhr , data ) {
57
- xhr . setRequestHeader ( "Authorization" , "Basic " + token ) ;
58
- } ,
59
- success : function ( data ) {
60
- var currentUser = JSON . parse ( ko . toJSON ( data ) ) . user ;
61
- $ . jGrowl ( "Welcome " + currentUser , {
62
- sticky : false ,
63
- theme : 'Notify'
64
- } ) ;
65
- doIfUserLoggedIn ( currentUser ) ;
66
- $ ( "#mainFrame" ) . load ( "subscriptionpage.html" ) ;
67
- } ,
68
- error : function ( XMLHttpRequest , errorThrown ) {
69
- $ . jGrowl ( "Bad credentials" , {
70
- sticky : false ,
71
- theme : 'Error'
72
- } ) ;
73
- } ,
74
- complete : function ( ) { }
75
- } ;
76
-
77
13
var dataJSON = ko . toJSON ( userState ) ;
78
14
if ( JSON . parse ( dataJSON ) . username == "" || JSON . parse ( dataJSON ) . password == "" ) {
79
15
$ . jGrowl ( "Username and password fields cannot be empty" , {
@@ -82,12 +18,33 @@ jQuery(document).ready(function() {
82
18
} ) ;
83
19
} else {
84
20
var token = window . btoa ( JSON . parse ( dataJSON ) . username + ":" + JSON . parse ( dataJSON ) . password ) ;
85
- var ajaxHttpSender = new AjaxHttpSender ( ) ;
86
- ajaxHttpSender . sendAjax ( "/auth/login" , "GET" , token , callback ) ;
21
+ sendLoginRequest ( "/auth/login" , "GET" , token ) ;
87
22
}
88
23
}
89
24
}
90
25
26
+ function sendLoginRequest ( url , type , token ) {
27
+ $ . ajax ( {
28
+ url : url ,
29
+ type : type ,
30
+ contentType : 'application/json; charset=utf-8' ,
31
+ cache : false ,
32
+ beforeSend : function ( request ) {
33
+ request . setRequestHeader ( "Authorization" , "Basic " + token ) ;
34
+ } ,
35
+ error : function ( request , textStatus , errorThrown ) {
36
+ $ . jGrowl ( "Bad credentials" , { sticky : false , theme : 'Error' } ) ;
37
+ } ,
38
+ success : function ( responseData , textStatus ) {
39
+ var currentUser = JSON . parse ( ko . toJSON ( responseData ) ) . user ;
40
+ $ . jGrowl ( "Welcome " + currentUser , { sticky : false , theme : 'Notify' } ) ;
41
+ doIfUserLoggedIn ( currentUser ) ;
42
+ $ ( "#mainFrame" ) . load ( "subscriptionpage.html" ) ;
43
+ } ,
44
+ complete : function ( request , textStatus ) { }
45
+ } ) ;
46
+ }
47
+
91
48
function doIfUserLoggedIn ( name ) {
92
49
localStorage . removeItem ( "currentUser" ) ;
93
50
localStorage . setItem ( "currentUser" , name ) ;
@@ -102,4 +59,21 @@ jQuery(document).ready(function() {
102
59
ko . applyBindings ( model , observableObject ) ;
103
60
// /Stop ## Knockout #####################################################
104
61
62
+ // /Start ## Cookies functions ###########################################
63
+ function setCookie ( name , value ) {
64
+ var expiry = new Date ( new Date ( ) . getTime ( ) + 1800 * 1000 ) ; // plus 30 min
65
+ if ( window . location . protocol == "https:" ) {
66
+ document . cookie = name + "=" + escape ( value ) + "; path=/; expires=" + expiry . toGMTString ( ) + "; secure; HttpOnly" ;
67
+ } else {
68
+ document . cookie = name + "=" + escape ( value ) + "; path=/; expires=" + expiry . toGMTString ( ) ;
69
+ }
70
+ }
71
+
72
+ function getCookie ( name ) {
73
+ var re = new RegExp ( name + "=([^;]+)" ) ;
74
+ var value = re . exec ( document . cookie ) ;
75
+ return ( value != null ) ? unescape ( value [ 1 ] ) : null ;
76
+ }
77
+ // /Stop ## Cookies functions ############################################
78
+
105
79
} ) ;
0 commit comments