From 1c489ed2af7ac7ffe22b14c3a106b38ae1cb3df0 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Thu, 28 Aug 2025 20:07:49 +0200 Subject: [PATCH] webapp: Simplify authentication code --- webapp/src/utils/authentication.ts | 40 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/webapp/src/utils/authentication.ts b/webapp/src/utils/authentication.ts index 74080596..03494ea1 100644 --- a/webapp/src/utils/authentication.ts +++ b/webapp/src/utils/authentication.ts @@ -1,33 +1,29 @@ import type { Emitter, EventType } from 'mitt'; import type { Router } from 'vue-router'; +function getUserAuthData(): string | null { + try { + const user = JSON.parse(localStorage.getItem('user') || ''); + return user?.authdata || null; + } catch { + return null; + } +} + export function authHeader(): Headers { // return authorization header with basic auth credentials - let user = null; - try { - user = JSON.parse(localStorage.getItem('user') || ''); - } catch { - // continue regardless of error + const authdata = getUserAuthData(); + const headers = new Headers({ 'X-Requested-With': 'XMLHttpRequest' }); + if (authdata) { + headers.append('Authorization', 'Basic ' + authdata); } - - const headers = new Headers(); - headers.append('X-Requested-With', 'XMLHttpRequest'); - if (user && user.authdata) { - headers.append('Authorization', 'Basic ' + user.authdata); - } - return new Headers(headers); + return headers; } export function authUrl(): string { - let user = null; - try { - user = JSON.parse(localStorage.getItem('user') || ''); - } catch { - // continue regardless of error - } - - if (user && user.authdata) { - return encodeURIComponent(atob(user.authdata)).replace('%3A', ':') + '@'; + const authdata = getUserAuthData(); + if (authdata) { + return encodeURIComponent(atob(authdata)).replace('%3A', ':') + '@'; } return ''; } @@ -38,7 +34,7 @@ export function logout() { } export function isLoggedIn(): boolean { - return localStorage.getItem('user') != null; + return getUserAuthData() != null; } export function login(username: string, password: string) {