Fix token authentication
This commit is contained in:
parent
1af7c357e9
commit
efa342dc7f
3 changed files with 11 additions and 8 deletions
|
@ -10,9 +10,10 @@ function postToLoginEndpoint(event) {
|
|||
"username" : this.elements.username.value,
|
||||
"password" : this.elements.password.value
|
||||
}
|
||||
const url = "https://reqres.in/api/login" // mock api service
|
||||
const url = "https://" + window.location.hostname + ":8444/api/v1/account/login/";
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
console.log("Attempting a connection to the following endpoint: " + url);
|
||||
console.log("User credentials:\n" + JSON.stringify(credentials));
|
||||
|
||||
xhr.open("POST", url, true);
|
||||
|
@ -22,7 +23,7 @@ function postToLoginEndpoint(event) {
|
|||
if (this.status === 200) {
|
||||
console.log("LOGIN SUCCESS!");
|
||||
console.log("Server response:\n" + this.response);
|
||||
token = JSON.parse(this.response).token;
|
||||
token = JSON.parse(this.response).key;
|
||||
localStorage.setItem("token", token);
|
||||
window.location.replace("home.html");
|
||||
} else {
|
||||
|
|
|
@ -2,18 +2,18 @@ function postToLogoutEndpoint(event) {
|
|||
event.preventDefault();
|
||||
|
||||
const token = localStorage.getItem("token");
|
||||
const url = "https://reqres.in/api/logout" // mock api service
|
||||
const url = "https://" + window.location.hostname + ":8444/api/v1/account/logout/";
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.open("POST", url, true);
|
||||
xhr.setRequestHeader("Authorization", "Token " + token);
|
||||
xhr.setRequestHeader("Authorization", "Bearer " + token);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (this.readyState === 4) {
|
||||
if (this.status === 200) {
|
||||
console.log("LOGOUT SUCCESS!");
|
||||
console.log("Server response:\n" + this.response);
|
||||
localStorage.removeItem("token");
|
||||
window.location.replace("index.html");
|
||||
window.location.replace("/");
|
||||
} else {
|
||||
console.error("LOGOUT FAILURE!");
|
||||
console.error("Server status: " + this.status);
|
||||
|
|
|
@ -10,7 +10,9 @@ function getDataFromEndpoint(url, callback) {
|
|||
|
||||
console.log("Attempting a connection to the following endpoint: " + url);
|
||||
|
||||
|
||||
xhr.open("GET", url, true);
|
||||
xhr.setRequestHeader("Authorization", "Bearer " + token);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (this.readyState === 4) {
|
||||
if (this.status === 200) {
|
||||
|
@ -222,7 +224,7 @@ function displayListOfReports(parsedData) {
|
|||
for (let i = 0; i < reports.length; i++) {
|
||||
let title = reports[i].title;
|
||||
let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US");
|
||||
let state = reports[i].state;
|
||||
let submitted = reports[i].submitted;
|
||||
let dateSubmitted;
|
||||
let rid = reports[i].report_pk;
|
||||
|
||||
|
@ -231,7 +233,7 @@ function displayListOfReports(parsedData) {
|
|||
bodyRow.insertCell(1).innerHTML = dateCreated;
|
||||
|
||||
let stateCell = bodyRow.insertCell(2);
|
||||
stateCell.innerHTML = state;
|
||||
stateCell.innerHTML = submitted;
|
||||
stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
|
||||
|
||||
// Create edit/view button
|
||||
|
@ -240,7 +242,7 @@ function displayListOfReports(parsedData) {
|
|||
actionButton.setAttribute("data-rid", rid);
|
||||
actionButton.classList.add("btn");
|
||||
|
||||
if (state === "created") {
|
||||
if (submitted === false) {
|
||||
// Edit button
|
||||
dateSubmitted = "TBD";
|
||||
actionButton.classList.add("btn-primary", "edit-report-button"); // Add event listener class
|
||||
|
|
Loading…
Reference in a new issue