Open in github.dev
Open in a new github.dev tab
Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
76 lines (73 sloc)
1.9 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |