From efa342dc7fe4054d69c5aefdbd42823ded9e275b Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Tue, 5 Feb 2019 01:05:04 -0800 Subject: [PATCH 1/4] 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 From 5d12cb64e4afe3eb53639e8465ffe4b7f46e5b37 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Tue, 5 Feb 2019 10:12:30 -0800 Subject: [PATCH 2/4] Remove error message display --- front/static/js/login.js | 6 ------ front/static/js/viewHistory.js | 6 +++--- front/static/login.html | 1 - 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/front/static/js/login.js b/front/static/js/login.js index 7be086e..cdef5f4 100644 --- a/front/static/js/login.js +++ b/front/static/js/login.js @@ -1,8 +1,3 @@ -function displayErrorMessage(errorMessage) { - const errorReport = document.querySelector("#errorReport"); - errorReport.innerHTML = JSON.parse(errorMessage).error; -} - function postToLoginEndpoint(event) { event.preventDefault(); @@ -30,7 +25,6 @@ function postToLoginEndpoint(event) { console.error("LOGIN FAILURE!"); console.error("Server status: " + this.status); console.error("Server response:\n" + this.response); - displayErrorMessage(this.response); } } }; diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js index 82c2f4d..4b2899b 100644 --- a/front/static/js/viewHistory.js +++ b/front/static/js/viewHistory.js @@ -224,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 submitted = reports[i].submitted; + let state = reports[i].submitted; let dateSubmitted; let rid = reports[i].report_pk; @@ -233,7 +233,7 @@ function displayListOfReports(parsedData) { bodyRow.insertCell(1).innerHTML = dateCreated; let stateCell = bodyRow.insertCell(2); - stateCell.innerHTML = submitted; + stateCell.innerHTML = state; stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays // Create edit/view button @@ -242,7 +242,7 @@ function displayListOfReports(parsedData) { actionButton.setAttribute("data-rid", rid); actionButton.classList.add("btn"); - if (submitted === false) { + if (state === false) { // Edit button dateSubmitted = "TBD"; actionButton.classList.add("btn-primary", "edit-report-button"); // Add event listener class diff --git a/front/static/login.html b/front/static/login.html index 5c60531..03938ea 100644 --- a/front/static/login.html +++ b/front/static/login.html @@ -41,7 +41,6 @@ -

From 15d50a7c50f04b8b451cdcf2904d030d24bde8fc Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 5 Feb 2019 11:28:58 -0800 Subject: [PATCH 3/4] Display error message when login fails --- front/static/js/login.js | 1 + front/static/login.html | 1 + 2 files changed, 2 insertions(+) diff --git a/front/static/js/login.js b/front/static/js/login.js index cdef5f4..20b411d 100644 --- a/front/static/js/login.js +++ b/front/static/js/login.js @@ -22,6 +22,7 @@ function postToLoginEndpoint(event) { localStorage.setItem("token", token); window.location.replace("home.html"); } else { + document.getElementById("errorLogin").innerHTML = "Incorrect user name or password"; console.error("LOGIN FAILURE!"); console.error("Server status: " + this.status); console.error("Server response:\n" + this.response); diff --git a/front/static/login.html b/front/static/login.html index 03938ea..9a8af2f 100644 --- a/front/static/login.html +++ b/front/static/login.html @@ -31,6 +31,7 @@ +

From 1ec02e1d224142570948a60136230188307a05df Mon Sep 17 00:00:00 2001 From: kououken Date: Tue, 5 Feb 2019 12:48:49 -0800 Subject: [PATCH 4/4] Added report_pk to reports api endpoint> --- back/backend/views.py | 1 + back/db.sqlite3 | Bin 59392 -> 59392 bytes 2 files changed, 1 insertion(+) diff --git a/back/backend/views.py b/back/backend/views.py index 21ab7d5..70e3f79 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -139,6 +139,7 @@ def reports(request): queryset = Report.objects.all() for i in queryset: data = { + "report_pk": i.id, "title": i.title, "date_created": i.date_created, "submitted": i.submitted, diff --git a/back/db.sqlite3 b/back/db.sqlite3 index 7c7ffb7bd3ce444e322cde768b99da9a7a1dc08c..2a1a5530382cc2affd37e357a256cd3fa46289dd 100644 GIT binary patch delta 325 zcmZp;z}#?wd4e=!??f4A#@>wyhmH7+46IBntc*DD zu|<+uRfXA?(a5N%*gVZVJ2@*mwKT2BG%-0VrOYt7)GRA4$09vHlYO(t_B|{DY|QQq z%#WCtFdt@aWlm#u-z+F&%se@5KfeIN7E22w69e|mY5V7iOlG;@63x!)+rsKd$O2!N zK+~vjuiVJ0NK?NMM*}~Xpqxk}znsYMu&l_cY*XJ5r<^F4M`WXcRO^Y=04M{ffbq_R( Z3JEmv4W8_?QC-kd0nOQ)Tdvj!0Ra8DW?%pS delta 106 zcmV-w0G0oM&;x+b1CSd5ijf>c0gAC;!!i#rF*-9bIxsmdG&D9aH88U>Gtz(r1p`O` zvkJ5s1CgL7vq-nR1P%oQW&i`?1Ed4P1BC--vk@Fr1CvL;53_Z@j}!<30fYbo!U2S{ MAqaQ@vxL=r673fubN~PV