Skip to content
chaofz  /   jquery-jwt-auth  /  
Type # for issues and pull requests, > for commands, and ? for help Type # for issues, pull requests, and projects, > for commands, and ? for help Type # for issues, pull requests, and projects, / for files, and > for commands
We’ve encountered an error and some results aren't available at this time. Type a new search or try again later.
No results matched your search
Search for issues and pull requests # Search for issues, pull requests, discussions, and projects # Search for organizations, repositories, and users @ Search for projects ! Search for files / Activate command mode > Search your issues, pull requests, and discussions # author:@me Search your issues, pull requests, and discussions # author:@me Filter to pull requests # is:pr Filter to issues # is:issue Filter to discussions # is:discussion Filter to projects # is:project Filter to open issues, pull requests, and discussions # is:open
  • Watch 4

    Notifications

  • Fork 17

    Fork jquery-jwt-auth

    If this dialog fails to load, you can visit the fork page directly.

Open in github.dev Open in a new github.dev tab
Permalink
master
Switch branches/tags
Go to file
1 contributor

Users who have contributed to this file

76 lines (73 sloc) 1.9 KB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Authentication</title>
</head>
<body>
<div>
<button id="test">Test Loggedin</button>
<button id="goodLogin">Good Login</button>
<button id="badLogin">Bad Login</button>
<button id="logout">Logout</button>
</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: '/api/profile',
beforeSend: function(xhr) {
if (localStorage.token) {
xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.token);
}
},
success: function(data) {
alert('Hello ' + data.name + '! You have successfully accessed to /api/profile.');
},
error: function() {
alert("Sorry, you are not logged in.");
}
});
});
$('#goodLogin').click(function() {
$.ajax({
type: "POST",
url: "/login",
data: {
username: "john.doe",
password: "foobar"
},
success: function(data) {
localStorage.token = data.token;
alert('Got a token from the server! Token: ' + data.token);
},
error: function() {
alert("Login Failed");
}
});
});
$('#badLogin').click(function() {
$.ajax({
type: "POST",
url: "/login",
data: {
username: "john.doe",
password: "foobarfoobar"
},
success: function(data) {
alert("ERROR: it is not supposed to alert.");
},
error: function() {
alert("Login Failed");
}
});
});
$('#logout').click(function() {
localStorage.clear();
});
});
</script>
</body>
</html>