mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2025-12-13 02:09:58 +01:00
webapp: Simplify authentication code
This commit is contained in:
@@ -1,33 +1,29 @@
|
|||||||
import type { Emitter, EventType } from 'mitt';
|
import type { Emitter, EventType } from 'mitt';
|
||||||
import type { Router } from 'vue-router';
|
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 {
|
export function authHeader(): Headers {
|
||||||
// return authorization header with basic auth credentials
|
// return authorization header with basic auth credentials
|
||||||
let user = null;
|
const authdata = getUserAuthData();
|
||||||
try {
|
const headers = new Headers({ 'X-Requested-With': 'XMLHttpRequest' });
|
||||||
user = JSON.parse(localStorage.getItem('user') || '');
|
if (authdata) {
|
||||||
} catch {
|
headers.append('Authorization', 'Basic ' + authdata);
|
||||||
// continue regardless of error
|
|
||||||
}
|
}
|
||||||
|
return headers;
|
||||||
const headers = new Headers();
|
|
||||||
headers.append('X-Requested-With', 'XMLHttpRequest');
|
|
||||||
if (user && user.authdata) {
|
|
||||||
headers.append('Authorization', 'Basic ' + user.authdata);
|
|
||||||
}
|
|
||||||
return new Headers(headers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function authUrl(): string {
|
export function authUrl(): string {
|
||||||
let user = null;
|
const authdata = getUserAuthData();
|
||||||
try {
|
if (authdata) {
|
||||||
user = JSON.parse(localStorage.getItem('user') || '');
|
return encodeURIComponent(atob(authdata)).replace('%3A', ':') + '@';
|
||||||
} catch {
|
|
||||||
// continue regardless of error
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user && user.authdata) {
|
|
||||||
return encodeURIComponent(atob(user.authdata)).replace('%3A', ':') + '@';
|
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -38,7 +34,7 @@ export function logout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isLoggedIn(): boolean {
|
export function isLoggedIn(): boolean {
|
||||||
return localStorage.getItem('user') != null;
|
return getUserAuthData() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function login(username: string, password: string) {
|
export function login(username: string, password: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user