Cleaner error message on aborted Passkey operations.

This commit is contained in:
Leo Vasanko 2025-08-06 12:00:23 -06:00
parent 3c6c9b29f6
commit f96668b135
5 changed files with 30 additions and 25 deletions

View File

@ -37,7 +37,7 @@ const handleLogin = async () => {
location.reload() location.reload()
} }
} catch (error) { } catch (error) {
authStore.showMessage(`Authentication failed: ${error.message}`, 'error') authStore.showMessage(error.message, 'error')
} }
} }
</script> </script>

View File

@ -124,7 +124,7 @@ const addNewCredential = async () => {
authStore.showMessage('New passkey added successfully!', 'success', 3000) authStore.showMessage('New passkey added successfully!', 'success', 3000)
} catch (error) { } catch (error) {
console.error('Failed to add new passkey:', error) console.error('Failed to add new passkey:', error)
authStore.showMessage(`Failed to add passkey: ${error.message}`, 'error') authStore.showMessage(error.message, 'error')
} finally { } finally {
authStore.isLoading = false authStore.isLoading = false
} }

View File

@ -32,14 +32,9 @@ async function register() {
await authStore.setSessionCookie(result.session_token) await authStore.setSessionCookie(result.session_token)
authStore.showMessage('Passkey registered successfully!', 'success', 2000) authStore.showMessage('Passkey registered successfully!', 'success', 2000)
authStore.currentView = 'profile' authStore.loadUserInfo().then(authStore.selectView)
} catch (error) { } catch (error) {
console.error('Registration error:', error) authStore.showMessage(`Registration failed: ${error.message}`, 'error')
if (error.name === "NotAllowedError") {
authStore.showMessage('Registration cancelled', 'error')
} else {
authStore.showMessage(`Registration failed: ${error.message}`, 'error')
}
} finally { } finally {
authStore.isLoading = false authStore.isLoading = false
} }

View File

@ -75,8 +75,8 @@ export const useAuthStore = defineStore('auth', {
} }
}, },
selectView() { selectView() {
if (!store.userInfo) this.currentView = 'login' if (!this.userInfo) this.currentView = 'login'
else if (store.userInfo?.authenticated) this.currentView = 'profile' else if (this.userInfo.authenticated) this.currentView = 'profile'
else this.currentView = 'reset' else this.currentView = 'reset'
}, },
async loadUserInfo() { async loadUserInfo() {

View File

@ -4,13 +4,18 @@ import aWebSocket from '@/utils/awaitable-websocket'
export async function register(url, options) { export async function register(url, options) {
if (options) url += `?${new URLSearchParams(options).toString()}` if (options) url += `?${new URLSearchParams(options).toString()}`
const ws = await aWebSocket(url) const ws = await aWebSocket(url)
try {
const optionsJSON = await ws.receive_json() const optionsJSON = await ws.receive_json()
const registrationResponse = await startRegistration({ optionsJSON }) const registrationResponse = await startRegistration({ optionsJSON })
ws.send_json(registrationResponse) ws.send_json(registrationResponse)
const result = await ws.receive_json() const result = await ws.receive_json()
ws.close() } catch (error) {
return result; console.error('Registration error:', error)
// Replace useless and ugly error message from startRegistration
throw Error(error.name === "NotAllowedError" ? 'Passkey registration cancelled' : error.message)
} finally {
ws.close()
}
} }
export async function registerUser(user_name) { export async function registerUser(user_name) {
@ -26,11 +31,16 @@ export async function registerWithToken(token) {
export async function authenticateUser() { export async function authenticateUser() {
const ws = await aWebSocket('/auth/ws/authenticate') const ws = await aWebSocket('/auth/ws/authenticate')
try {
const optionsJSON = await ws.receive_json() const optionsJSON = await ws.receive_json()
const authResponse = await startAuthentication({ optionsJSON }) const authResponse = await startAuthentication({ optionsJSON })
ws.send_json(authResponse) ws.send_json(authResponse)
const result = await ws.receive_json() const result = await ws.receive_json()
ws.close() return result
return result } catch (error) {
console.error('Authentication error:', error)
throw Error(error.name === "NotAllowedError" ? 'Passkey authentication cancelled' : error.message)
} finally {
ws.close()
}
} }