bootstrap toasts

This commit is contained in:
creeper123123321 2021-03-14 12:13:54 -03:00
parent a3377bd69a
commit e3ef23158f
3 changed files with 32 additions and 10 deletions

View File

@ -7,7 +7,7 @@ window.location.hash.substr(1).split("?").map(it => new URLSearchParams(it).forE
var mcIdUsername = urlParams.get("username"); var mcIdUsername = urlParams.get("username");
var mcauth_code = urlParams.get("mcauth_code"); var mcauth_code = urlParams.get("mcauth_code");
if (urlParams.get("mcauth_success") == "false") { if (urlParams.get("mcauth_success") == "false") {
alert("Couldn't authenticate with Minecraft.ID: " + urlParams.get("mcauth_msg")); addToast("Couldn't authenticate with Minecraft.ID", urlParams.get("mcauth_msg"));
} }
// WS url // WS url
@ -123,7 +123,7 @@ function loginMc(user, pass) {
.then(r => r.json()) .then(r => r.json())
.then(data => { .then(data => {
storeMcAccount(data.accessToken, data.clientToken, data.selectedProfile.name, data.selectedProfile.id); storeMcAccount(data.accessToken, data.clientToken, data.selectedProfile.name, data.selectedProfile.id);
}).catch(e => alert("Failed to login: " + e)); }).catch(e => addToast("Failed to login", e));
$("#form_add_mc input").val(""); $("#form_add_mc input").val("");
} }
function logoutMojang(id) { function logoutMojang(id) {
@ -173,7 +173,7 @@ function getMcUserToken(account) {
} }
} }
return account; return account;
}).catch(e => alert("failed to refresh token! " + e)); }).catch(e => addToast("Failed to refresh token!", e));
} }
function validateToken(account) { function validateToken(account) {
return fetch(getCorsProxy() + "https://authserver.mojang.com/validate", {method: "post", return fetch(getCorsProxy() + "https://authserver.mojang.com/validate", {method: "post",
@ -276,6 +276,26 @@ function addListeningList(user) {
msg.innerText = "Listening to login: " + user; msg.innerText = "Listening to login: " + user;
listening.appendChild(msg); listening.appendChild(msg);
} }
function addToast(title, msg) {
let toast = document.createElement("div");
document.getElementById("toasts").prepend(toast);
$(toast)
.attr("class", "toast")
.attr("role", "alert")
.attr("aria-live", "assertive")
.attr("aria-atomic", "true") // todo sanitize date \/
.html(`
<div class="toast-header">
<strong class="me-auto toast_title_msg"></strong>
<small class="text-muted">${new Date().toLocaleString()}</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body"></div>
`);
$(toast).find(".toast_title_msg").text(title);
$(toast).find(".toast-body").text(msg);
new bootstrap.Toast(toast).show();
}
function resetHtml() { function resetHtml() {
listening.innerHTML = ""; listening.innerHTML = "";
listenVisible = false; listenVisible = false;
@ -337,10 +357,10 @@ function handleJoinRequest(parsed) {
}) })
.then(checkFetchSuccess("code")) .then(checkFetchSuccess("code"))
.finally(() => confirmJoin(parsed.session_hash)) .finally(() => confirmJoin(parsed.session_hash))
.catch((e) => alert("Couldn't contact session server for " + parsed.user + " account in browser. error: " + e)); .catch((e) => addToast("Couldn't contact session server", "error: " + e));
} else { } else {
confirmJoin(parsed.session_hash); confirmJoin(parsed.session_hash);
alert("Couldn't find " + parsed.user + " account in browser."); addToast("Couldn't find account", parsed.user);
} }
}, () => confirmJoin(parsed.session_hash)); }, () => confirmJoin(parsed.session_hash));
} }
@ -351,7 +371,7 @@ function onSocketMsg(event) {
renderActions(); renderActions();
} else if (parsed.action == "login_result") { } else if (parsed.action == "login_result") {
if (!parsed.success) { if (!parsed.success) {
alert("VIAaaS instance couldn't verify Minecraft account"); addToast("Couldn't verify Minecraft account", "VIAaaS returned failed response");
} else { } else {
listen(parsed.token); listen(parsed.token);
saveToken(parsed.token); saveToken(parsed.token);

View File

@ -27,7 +27,7 @@ function loginMs() {
$(() => myMSALObj.handleRedirectPromise().then((resp) => { $(() => myMSALObj.handleRedirectPromise().then((resp) => {
if (resp) { if (resp) {
refreshTokenMs(resp.account.username).catch(e => alert("failed to get mc token: " + e)); refreshTokenMs(resp.account.username).catch(e => addToast("Failed to get token", e));
refreshAccountList(); refreshAccountList();
} }
})); }));

View File

@ -58,7 +58,7 @@
</div> </div>
</nav> </nav>
<div class="container tab-content" id="content"> <div class="container tab-content" id="content">
<div id="home" class="tab-pane fade show active" role="tabpanel" aria-labelledby="home-tab"> <div id="home" class="tab-pane fade show active" aria-labelledby="home-tab">
<p>WebSocket connection status: <span id="connection_status" class="text-white bg-dark">?</span></p> <p>WebSocket connection status: <span id="connection_status" class="text-white bg-dark">?</span></p>
<p>CORS Proxy status: <span id="cors_status" class="text-white bg-dark">?</span></p> <p>CORS Proxy status: <span id="cors_status" class="text-white bg-dark">?</span></p>
<hr> <hr>
@ -67,7 +67,7 @@
<p><span id="listening"></span></p> <p><span id="listening"></span></p>
</div> </div>
<div id="settings" class="tab-pane fade" role="tabpanel" aria-labelledby="settings-tab"> <div id="settings" class="tab-pane fade" aria-labelledby="settings-tab">
<div class="mb-3"> <div class="mb-3">
<label for="cors-proxy" class="form-label">CORS Proxy URL Prefix</label> <label for="cors-proxy" class="form-label">CORS Proxy URL Prefix</label>
<p>See <a href="https://github.com/ViaVersion/VIAaaS#cors-proxy">README</a> <p>See <a href="https://github.com/ViaVersion/VIAaaS#cors-proxy">README</a>
@ -82,7 +82,7 @@
</div> </div>
</div> </div>
<div id="accounts" class="tab-pane fade" role="tabpanel" aria-labelledby="accounts-tab"> <div id="accounts" class="tab-pane fade" aria-labelledby="accounts-tab">
<div> <div>
<p>Browser Minecraft accounts:</p> <p>Browser Minecraft accounts:</p>
<div id="accounts-list"></div> <div id="accounts-list"></div>
@ -112,6 +112,8 @@
</div> </div>
</div> </div>
</div> </div>
<div class="toast-container position-absolute top-0 end-0 p-3" id="toasts"></div>
<script src="auth_ms.js"></script> <script src="auth_ms.js"></script>
<script src="auth.js"></script> <script src="auth.js"></script>
</body> </body>