1
1
2
2
jQuery ( document ) . ready ( function ( ) {
3
-
4
- // document.getElementById("loginBtn").onclick = function() {
5
- //
6
- // var iframe = document.getElementById("mainFrame");
7
- // iframe.src = "http://localhost:8080/subscriptionpage.html";
8
- // }
9
-
10
-
11
-
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
+ // /Start ## Knockout ####################################################
47
+ function loginModel ( ) {
48
+ this . userState = {
49
+ username : ko . observable ( "" ) ,
50
+ password : ko . observable ( "" )
51
+ } ;
52
+ this . remember = ko . observable ( false ) ;
53
+
54
+ 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
+ var dataJSON = ko . toJSON ( userState ) ;
78
+ if ( JSON . parse ( dataJSON ) . username == "" || JSON . parse ( dataJSON ) . password == "" ) {
79
+ $ . jGrowl ( "Username and password fields cannot be empty" , {
80
+ sticky : false ,
81
+ theme : 'Error'
82
+ } ) ;
83
+ } else {
84
+ 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 ) ;
87
+ }
88
+ }
89
+ }
90
+
91
+ function doIfUserLoggedIn ( name ) {
92
+ localStorage . removeItem ( "currentUser" ) ;
93
+ localStorage . setItem ( "currentUser" , name ) ;
94
+ $ ( "#userName" ) . text ( name ) ;
95
+ $ ( "#loginBlock" ) . hide ( ) ;
96
+ $ ( "#logoutBlock" ) . show ( ) ;
97
+ }
98
+
99
+ var observableObject = $ ( "#viewModelDOMObject" ) [ 0 ] ;
100
+ ko . cleanNode ( observableObject ) ;
101
+ var model = new loginModel ( ) ;
102
+ ko . applyBindings ( model , observableObject ) ;
103
+ // /Stop ## Knockout #####################################################
12
104
13
105
} ) ;
0 commit comments