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