-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
374 additions
and
207 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,3 @@ | ||
const API_STORAGE_KEY = "rtnlapistoragekey"; | ||
|
||
(function() { | ||
|
||
// Handle responses from the login form. | ||
document.body.addEventListener("htmx:afterRequest", function(e) { | ||
if (e.detail.failed) { | ||
// Handle error from the server | ||
let message = "could not complete request, please try again later" | ||
if (e.detail.xhr.response) { | ||
const data = JSON.parse(e.detail.xhr.response); | ||
message = data.error; | ||
} | ||
|
||
let elem = document.getElementById("login-error") | ||
elem.innerText = message; | ||
elem.classList.remove("hidden"); | ||
return | ||
} | ||
|
||
// Otherwise the API key is valid and can be set on local storage. | ||
const data = JSON.parse(e.detail.xhr.response); | ||
window.localStorage.setItem(API_STORAGE_KEY, data.apikey); | ||
window.location.href = "/" | ||
}) | ||
|
||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,4 @@ | ||
const API_STORAGE_KEY = "rtnlapistoragekey"; | ||
|
||
function APIKey() { | ||
return window.localStorage.getItem(API_STORAGE_KEY); | ||
} | ||
|
||
// Adds additional headers to the requests made by htmx | ||
document.body.addEventListener('htmx:configRequest', function(e) { | ||
e.detail.headers['Accept'] = "text/html"; | ||
|
||
let apikey = APIKey() | ||
if (apikey) { | ||
e.detail.headers['Authorization'] = 'Bearer ' + APIKey(); | ||
} | ||
}); | ||
|
||
// Handle clicks to the logout button in the header | ||
document.getElementById("logout").addEventListener("click", function(e) { | ||
e.preventDefault() | ||
window.localStorage.removeItem(API_STORAGE_KEY) | ||
window.location.href = "/login" | ||
return false; | ||
}); | ||
|
||
// Checks if there is an API key, and if not, redirects to the login page. | ||
(function() { | ||
let apikey = APIKey(); | ||
if (!apikey) { | ||
window.location.href = "/login" | ||
} else { | ||
console.info("link shortening application logged in and ready") | ||
} | ||
})(); | ||
|
||
console.info("link shortening application logged in and ready"); | ||
})(); |
Oops, something went wrong.