-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
136 lines (126 loc) · 3.8 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>JWT Example</title>
</head>
<body>
<div>
<button id="test">Test if Logged In</button>
<button id="goodLogin">Good Login</button>
<button id="badLogin">Bad Login</button>
<button id="logout">Logout and Clear Token</button>
</div>
<div>
<form id="registerSubmit" method="post" enctype="multipart/form-data" action="./app_client.php">
<label>
<span>Username</span>
<input type="text" placeholder="Username" value="" name="username">
</label>
<label>
<span>Password</span>
<input type="password" placeholder="Password" value="" name="password">
</label>
<input type="submit" name="submit" value="Enter">
</form>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function() {
$.ajax({
type: 'GET',
url: 'app_client.php',
dataType: "json",
data: {
token: localStorage.token
},
success: function(data) {
if (typeof data['userId'] !== 'undefined') {
var alertMessage = 'You have a valid token! Here is your user Id: ' + data['userId'];
if (typeof data['exp'] !== 'undefined') {
alertMessage = alertMessage + ' and your token expires: ' + data['exp'];
}
alert(alertMessage);
}
else if (typeof data['error'] !== 'undefined') {
alert('Error: ' + data['error']);
}
else {
alert('Error: Your request has failed.');
}
}
});
});
$('#goodLogin').click(function() {
$.ajax({
type: "POST",
url: "app_client.php",
dataType: "json",
data: {
username: "fernando",
password: "12345678"
},
success: function(data) {
localStorage.token = data['token'];
alert('Successfully retrieved token from the server! Token: ' + data['token']);
},
error: function() {
alert("Error: Login Failed");
}
});
});
$('#badLogin').click(function() {
$.ajax({
type: "POST",
url: "app_client.php",
dataType: "json",
data: {
username: "john.doe",
password: "foobarfoobar"
},
success: function(data) {
if (typeof data['error'] !== 'undefined') {
alert('Error: ' + data['error']);
localStorage.clear();
}
},
error: function() {
alert('Error: Your request has failed.');
}
});
});
$('#logout').click(function() {
localStorage.clear();
});
});
</script>
<script>
$(document).ready(function(){
$("#registerSubmit").submit(function( e ) {
e.preventDefault();
$.ajax({
url: "app_client.php",
method: "post",
data: $(this).serialize(),
dataType: "json",
success: function(data) {
if (typeof data['error'] !== 'undefined') {
alert('Error: ' + data['error']);
localStorage.clear();
}
else{
localStorage.token = data['token'];
alert('Successfully retrieved token from the server! Token: ' + data['token']);
}
},
error: function() {
alert("Error: Login Failed");
localStorage.clear();
}
})
});
});
</script>
</body>
</html>