alerts and logging in

This commit is contained in:
SpikeHD 2022-04-26 22:24:45 -07:00
parent 5730a9700f
commit 7ef3d516fa
3 changed files with 40 additions and 13 deletions

View File

@ -1,6 +1,6 @@
let alertTimeout, alertCooldown = 3000 let alertTimeout, alertCooldown = 3000
async function displayLoginAlert(message, type) { async function displayLoginAlert(message, type, cooldown = null) {
const elm = document.getElementById('loginAlert'); const elm = document.getElementById('loginAlert');
const text = document.getElementById('loginAlertText'); const text = document.getElementById('loginAlertText');
@ -11,6 +11,10 @@ async function displayLoginAlert(message, type) {
elm.classList.add('error'); elm.classList.add('error');
break; break;
case 'success':
elm.classList.add('success');
break;
case 'warn': case 'warn':
default: default:
elm.classList.add('warn'); elm.classList.add('warn');
@ -19,14 +23,15 @@ async function displayLoginAlert(message, type) {
text.innerText = message; text.innerText = message;
// Disappear after 5 seconds // Disappear after cooldown
alertTimeout = setTimeout(() => { alertTimeout = setTimeout(() => {
elm.style.display = 'none'; elm.style.display = 'none';
}, alertCooldown) }, cooldown || alertCooldown)
} }
async function displayRegisterAlert(message, type) { async function displayRegisterAlert(message, type, cooldown = null) {
const elm = document.getElementById('registerAlert'); const elm = document.getElementById('registerAlert');
const text = document.getElementById('registerAlertText');
elm.style.removeProperty('display'); elm.style.removeProperty('display');
@ -35,6 +40,10 @@ async function displayRegisterAlert(message, type) {
elm.classList.add('error'); elm.classList.add('error');
break; break;
case 'success':
elm.classList.add('success');
break;
case 'warn': case 'warn':
default: default:
elm.classList.add('warn'); elm.classList.add('warn');
@ -43,8 +52,8 @@ async function displayRegisterAlert(message, type) {
text.innerText = message; text.innerText = message;
// Disappear after 5 seconds // Disappear after cooldown
alertTimeout = setTimeout(() => { alertTimeout = setTimeout(() => {
elm.style.display = 'none'; elm.style.display = 'none';
}, alertCooldown) }, cooldown || alertCooldown)
} }

View File

@ -64,8 +64,6 @@ async function login() {
const { data } = await axios.post(url + '/grasscutter/login', reqBody) const { data } = await axios.post(url + '/grasscutter/login', reqBody)
console.log(data)
switch(data.message) { switch(data.message) {
case 'INVALID_ACCOUNT': case 'INVALID_ACCOUNT':
displayLoginAlert('Invalid username or password', 'error'); displayLoginAlert('Invalid username or password', 'error');
@ -73,20 +71,25 @@ async function login() {
case 'NO_PASSWORD': case 'NO_PASSWORD':
// No account password, create one with change password // No account password, create one with change password
displayLoginAlert('No password set, please change password', 'warn');
break; break;
case 'UNKNOWN': case 'UNKNOWN':
// Unknown error, contact server owner // Unknown error, contact server owner
displayLoginAlert('Unknown error, contact server owner', 'error');
break; break;
case 'AUTH_DISABLED': case 'AUTH_DISABLED':
// Authentication is disabled, we can just connect the user // Authentication is disabled, we can just connect the user
displayLoginAlert('Authentication is disabled, no need to log in!', 'warn');
break; break;
default: default:
// Success! Copy the JWT token to their clipboard // Success! Copy the JWT token to their clipboard
const tkData = parseJwt(data.jwt) const tkData = parseJwt(data.jwt)
await Neutralino.clipboard.writeText(tkData.token) await Neutralino.clipboard.writeText(tkData.token)
displayLoginAlert('Login successful! Token copied to clipboard. Paste this token into the username field of the game to log in.', 'success', 8000);
break; break;
} }
} }
@ -112,23 +115,34 @@ async function register() {
const { data } = await axios.post(url + '/grasscutter/register', reqBody) const { data } = await axios.post(url + '/grasscutter/register', reqBody)
console.log(data)
switch(data.message) { switch(data.message) {
case 'USERNAME_TAKEN': case 'USERNAME_TAKEN':
// Username is taken // Username is taken
displayRegisterAlert('Username is taken', 'error');
break; break;
case 'PASSWORD_MISMATCH': case 'PASSWORD_MISMATCH':
// The password and password confirmation do not match // The password and password confirmation do not match
displayRegisterAlert('Password and password confirmation do not match', 'error');
break; break;
case 'UNKNOWN': case 'UNKNOWN':
// Unknown error, contact server owner // Unknown error, contact server owner
displayRegisterAlert('Unknown error, contact server owner', 'error');
break; break;
case 'AUTH_DISABLED': case 'AUTH_DISABLED':
// Authentication is disabled, we can just connect the user // Authentication is disabled, we can just connect the user
displayRegisterAlert('Authentication is disabled, no need to register!', 'warn');
break;
default:
// Success!! Bring them to the login screen and auto-input their username
const loginUsername = document.getElementById('loginUsername');
loginUsername.value = username;
setLoginSection();
displayLoginAlert('Registration successful!', 'success', 5000);
break; break;
} }
} }

View File

@ -34,7 +34,7 @@ body {
} }
#loginPanel { #loginPanel {
height: 40%; height: 50%;
width: 32%; width: 32%;
} }
@ -121,9 +121,12 @@ body {
background: #e90000; background: #e90000;
} }
#registerAlert .warn, .warn {
#loginAlert .warn { background: #ffc61e;
}
.success {
background: #00c200;
} }
#registerAlert, #registerAlert,
@ -133,6 +136,7 @@ body {
align-items: center; align-items: center;
border-radius: 5px; border-radius: 5px;
color: #fff; color: #fff;
text-align: center;
} }
#registerAlert img, #registerAlert img,