Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebUI: use Fetch API to login #21744

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/webui/www/public/css/login.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ body {

#error_msg {
color: #f00;
white-space: pre;
}

#loginButton {
Expand Down
42 changes: 21 additions & 21 deletions src/webui/www/public/scripts/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@

const submitLoginForm = (event) => {
event.preventDefault();
const errorMsgElement = document.getElementById("error_msg");

const xhr = new XMLHttpRequest();
xhr.open("POST", "api/v2/auth/login", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState === 4) { // DONE state
if ((xhr.status === 200) && (xhr.responseText === "Ok."))
location.replace(location);
else
errorMsgElement.textContent = "QBT_TR(Invalid Username or Password.)QBT_TR[CONTEXT=Login]";
}
});
xhr.addEventListener("error", () => {
errorMsgElement.textContent = (xhr.responseText !== "")
? xhr.responseText
: "QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]";
});
const errorMsgElement = document.getElementById("error_msg");
errorMsgElement.textContent = ""; // clear previous error

const usernameElement = document.getElementById("username");
const passwordElement = document.getElementById("password");
const queryString = "username=" + encodeURIComponent(usernameElement.value) + "&password=" + encodeURIComponent(passwordElement.value);
xhr.send(queryString);
const queryString = `username=${encodeURIComponent(usernameElement.value)}&password=${encodeURIComponent(passwordElement.value)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLSearchParams will handle this complexity for you.

const query = new URLSearchParams();
query.append("username", usernameElement.value);
query.append("password", passwordElement.value);
// body: query.toString()

passwordElement.value = ""; // clear previous value

// clear the field
passwordElement.value = "";
fetch("api/v2/auth/login", {
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: queryString
})
.then(async (response) => {
const responseText = await response.text();
if (response.ok && (responseText === "Ok."))
location.replace(location); // redirect
else
errorMsgElement.textContent = `QBT_TR(Invalid Username or Password.)QBT_TR[CONTEXT=Login]\nQBT_TR(Server response:)QBT_TR[CONTEXT=Login] ${responseText}`;
},
(error) => {
errorMsgElement.textContent = `QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]\n${error}`;
});
Comment on lines +42 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be preferable to use await here to avoid nesting.

Suggested change
fetch("api/v2/auth/login", {
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: queryString
})
.then(async (response) => {
const responseText = await response.text();
if (response.ok && (responseText === "Ok."))
location.replace(location); // redirect
else
errorMsgElement.textContent = `QBT_TR(Invalid Username or Password.)QBT_TR[CONTEXT=Login]\nQBT_TR(Server response:)QBT_TR[CONTEXT=Login] ${responseText}`;
},
(error) => {
errorMsgElement.textContent = `QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]\n${error}`;
});
try {
const response = await fetch("api/v2/auth/login", {
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: queryString
});
const responseText = await response.text();
if (response.ok && (responseText === "Ok."))
location.replace(location); // redirect
else
errorMsgElement.textContent = `QBT_TR(Invalid Username or Password.)QBT_TR[CONTEXT=Login]\nQBT_TR(Server response:)QBT_TR[CONTEXT=Login] ${responseText}`;
}
catch(error) {
errorMsgElement.textContent = `QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]\n${error}`;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine he did it this way to avoid declaring the parent function as async and having to await on every callsite of the parent function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I just checked and submitLoginForm is only passed to addEventListener, so there shouldn't be any impact.

};

document.addEventListener("DOMContentLoaded", () => {
Expand Down
Loading