Improved login/registration message handling, display more useful error messages.

This commit is contained in:
Leo Vasanko 2025-07-10 14:16:09 -06:00
parent d4e5497406
commit 5a92c6a25f
3 changed files with 94 additions and 141 deletions

View File

@ -257,7 +257,7 @@ async function authenticate() {
const optionsJSON = JSON.parse(await ws.recv())
if (optionsJSON.error) throw new Error(optionsJSON.error)
showStatus('loginStatus', 'Please touch your authenticator...', 'info')
showStatus('loginStatus', 'Please use your authenticator...', 'info')
const authResponse = await startAuthentication({optionsJSON})
await ws.send(JSON.stringify(authResponse))
@ -472,63 +472,4 @@ function initializeApp() {
}
// Form event handlers
document.addEventListener('DOMContentLoaded', function() {
// Check for existing session on page load
initializeApp()
// Registration form
const regForm = document.getElementById('registrationForm')
if (regForm) {
const regSubmitBtn = regForm.querySelector('button[type="submit"]')
regForm.addEventListener('submit', async (ev) => {
ev.preventDefault()
regSubmitBtn.disabled = true
clearStatus('registerStatus')
const user_name = (new FormData(regForm)).get('username')
try {
showStatus('registerStatus', 'Starting registration...', 'info')
await register(user_name)
showStatus('registerStatus', `Registration successful for ${user_name}!`, 'success')
// Auto-login after successful registration
setTimeout(() => {
window.location.href = '/auth/profile'
}, 1500)
} catch (err) {
showStatus('registerStatus', `Registration failed: ${err.message}`, 'error')
} finally {
regSubmitBtn.disabled = false
}
})
}
// Authentication form
const authForm = document.getElementById('authenticationForm')
if (authForm) {
const authSubmitBtn = authForm.querySelector('button[type="submit"]')
authForm.addEventListener('submit', async (ev) => {
ev.preventDefault()
authSubmitBtn.disabled = true
clearStatus('loginStatus')
try {
showStatus('loginStatus', 'Starting authentication...', 'info')
await authenticate()
showStatus('loginStatus', 'Authentication successful!', 'success')
// Navigate to profile
setTimeout(() => {
window.location.href = '/auth/profile'
}, 1000)
} catch (err) {
showStatus('loginStatus', `Authentication failed: ${err.message}`, 'error')
} finally {
authSubmitBtn.disabled = false
}
})
}
})
document.addEventListener('DOMContentLoaded', initializeApp)

View File

@ -24,7 +24,12 @@ document.addEventListener('DOMContentLoaded', function() {
window.location.href = '/auth/profile'
}, 1000)
} catch (err) {
showStatus('loginStatus', `Authentication failed: ${err.message}`, 'error')
console.error('Login error:', err)
if (err.name === "NotAllowedError") {
showStatus('loginStatus', `Login cancelled`, 'error')
} else {
showStatus('loginStatus', `Login failed: ${err.message}`, 'error')
}
} finally {
authSubmitBtn.disabled = false
}

View File

@ -9,13 +9,13 @@ document.addEventListener('DOMContentLoaded', function() {
if (regForm) {
const regSubmitBtn = regForm.querySelector('button[type="submit"]')
regForm.addEventListener('submit', async (ev) => {
regForm.addEventListener('submit', ev => {
ev.preventDefault()
regSubmitBtn.disabled = true
clearStatus('registerStatus')
const user_name = (new FormData(regForm)).get('username')
regSubmitBtn.disabled = true
const ahandler = async () => {
try {
showStatus('registerStatus', 'Starting registration...', 'info')
await register(user_name)
@ -23,13 +23,20 @@ document.addEventListener('DOMContentLoaded', function() {
// Auto-login after successful registration
setTimeout(() => {
window.location.href = '/auth/profile'
window.location.href = '/'
}, 1500)
} catch (err) {
console.error('Registration error:', err)
if (err.name === "NotAllowedError") {
showStatus('registerStatus', `Registration cancelled`, 'error')
} else {
showStatus('registerStatus', `Registration failed: ${err.message}`, 'error')
}
} finally {
regSubmitBtn.disabled = false
}
}
ahandler()
})
}
})