From efa342dc7fe4054d69c5aefdbd42823ded9e275b Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Tue, 5 Feb 2019 01:05:04 -0800 Subject: [PATCH] Fix token authentication --- front/static/js/login.js | 5 +++-- front/static/js/logout.js | 6 +++--- front/static/js/viewHistory.js | 8 +++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/front/static/js/login.js b/front/static/js/login.js index 8269c99..7be086e 100644 --- a/front/static/js/login.js +++ b/front/static/js/login.js @@ -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 { diff --git a/front/static/js/logout.js b/front/static/js/logout.js index c10c381..b3bef0d 100644 --- a/front/static/js/logout.js +++ b/front/static/js/logout.js @@ -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); diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js index 7e7a88c..82c2f4d 100644 --- a/front/static/js/viewHistory.js +++ b/front/static/js/viewHistory.js @@ -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