Skip to content

Commit 62c276c

Browse files
Adding support for enabling/disabling authentication
1 parent a2017bb commit 62c276c

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

src/Frontend/src/App.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { onMounted } from "vue";
2+
import { computed, onMounted } from "vue";
33
import { RouterView } from "vue-router";
44
import PageFooter from "./components/PageFooter.vue";
55
import PageHeader from "./components/PageHeader.vue";
@@ -12,16 +12,24 @@ import { useAuthStore } from "@/stores/AuthStore";
1212
1313
const { authenticate } = useAuth();
1414
const authStore = useAuthStore();
15-
const { isAuthenticating, isAuthenticated } = storeToRefs(authStore);
15+
const { isAuthenticating, isAuthenticated, authEnabled } = storeToRefs(authStore);
16+
const shouldShowApp = computed(() => !authEnabled.value || isAuthenticated.value);
1617
1718
onMounted(async () => {
1819
try {
1920
// Attempt to authenticate when the app first loads
2021
await authStore.refresh();
2122
23+
// If authentication is not enabled, skip authentication
24+
if (!authStore.authEnabled) {
25+
console.debug("Authentication is disabled");
26+
authStore.setAuthenticating(false);
27+
return;
28+
}
29+
2230
// Check if auth config is available
2331
if (!authStore.authConfig) {
24-
console.debug("Auth configuration not available, skipping authentication");
32+
console.debug("Authentication is enabled but configuration not available");
2533
authStore.setAuthenticating(false);
2634
return;
2735
}
@@ -53,7 +61,7 @@ onMounted(async () => {
5361
<p class="auth-loading-text">Authenticating...</p>
5462
</div>
5563
</div>
56-
<template v-else-if="isAuthenticated">
64+
<template v-if="shouldShowApp">
5765
<page-header />
5866
<div class="container-fluid" id="main-content">
5967
<RouterView />
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
import { useAuthStore } from "@/stores/AuthStore";
22

3+
const UNAUTHENTICATED_ENDPOINTS = ["/api/authentication/configuration"];
4+
5+
function isUnauthenticatedEndpoint(url: string): boolean {
6+
return UNAUTHENTICATED_ENDPOINTS.some((endpoint) => url.includes(endpoint));
7+
}
8+
39
/**
410
* Authenticated fetch wrapper that automatically includes JWT token
5-
* in the Authorization header for all requests
11+
* in the Authorization header when authentication is enabled.
612
*/
7-
export async function authFetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
13+
export function authFetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
814
const authStore = useAuthStore();
9-
const token = authStore.token;
10-
11-
// Allow unauthenticated requests to the auth configuration endpoint
1215
const url = typeof input === "string" ? input : input.url;
13-
if (url.includes("/api/authentication/configuration")) {
14-
return await fetch(input, init);
16+
17+
// Allow unauthenticated requests to specific endpoints
18+
if (isUnauthenticatedEndpoint(url)) {
19+
return fetch(input, init);
20+
}
21+
22+
// If authentication is disabled, make request without token
23+
if (!authStore.authEnabled) {
24+
return fetch(input, init);
1525
}
1626

17-
// todo: potentially handle token refresh here if expired, however it shouldnt be required due to silent renew
27+
// If authentication is enabled, require a token
28+
// TODO: potentially handle token refresh here if expired, however it shouldnt be required due to silent renew
29+
const token = authStore.token;
1830
if (!token) {
1931
throw new Error("No authentication token available. Please authenticate first.");
2032
}
2133

2234
const headers = new Headers(init?.headers);
2335
headers.set("Authorization", `Bearer ${token}`);
2436

25-
return await fetch(input, { ...init, headers });
37+
return fetch(input, { ...init, headers });
2638
}

src/Frontend/src/stores/AuthStore.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineStore } from "pinia";
1+
import { acceptHMRUpdate, defineStore } from "pinia";
22
import { ref } from "vue";
33
import { useServiceControlStore } from "./ServiceControlStore";
44
import type { AuthConfig } from "@/types/auth";
@@ -19,14 +19,16 @@ export const useAuthStore = defineStore("auth", () => {
1919
const isAuthenticating = ref(false);
2020
const authError = ref<string | null>(null);
2121
const authConfig = ref<AuthConfig | null>(null);
22+
const authEnabled = ref(true);
2223
const loading = ref(false);
2324

2425
async function refresh() {
2526
loading.value = true;
2627
try {
2728
const config = await getAuthConfig();
2829
if (config) {
29-
authConfig.value = transformToAuthConfig(config);
30+
authEnabled.value = config.enabled;
31+
authConfig.value = config.enabled ? transformToAuthConfig(config) : null;
3032
}
3133
} finally {
3234
loading.value = false;
@@ -38,7 +40,7 @@ export const useAuthStore = defineStore("auth", () => {
3840
const [, data] = await serviceControlStore.fetchTypedFromServiceControl<AuthConfigResponse>("authentication/configuration");
3941
return data;
4042
} catch (err) {
41-
console.error("Error fetching auth config information", err);
43+
console.error("Error fetching auth configuration", err);
4244
return null;
4345
}
4446
}
@@ -65,7 +67,6 @@ export const useAuthStore = defineStore("auth", () => {
6567
isAuthenticated.value = !!newToken;
6668

6769
if (newToken) {
68-
// Store token in sessionStorage for persistence across page reloads
6970
sessionStorage.setItem("auth_token", newToken);
7071
} else {
7172
sessionStorage.removeItem("auth_token");
@@ -99,6 +100,7 @@ export const useAuthStore = defineStore("auth", () => {
99100
isAuthenticating,
100101
authError,
101102
authConfig,
103+
authEnabled,
102104
loading,
103105
refresh,
104106
setToken,
@@ -108,3 +110,9 @@ export const useAuthStore = defineStore("auth", () => {
108110
setAuthError,
109111
};
110112
});
113+
114+
if (import.meta.hot) {
115+
import.meta.hot.accept(acceptHMRUpdate(useAuthStore, import.meta.hot));
116+
}
117+
118+
export type AuthStore = ReturnType<typeof useAuthStore>;

0 commit comments

Comments
 (0)