Refactor demo login as an authentication method

Use the authentication method system for the demo login and the
generated accounts.  This makes it possible to toggle it off on
production systems as these shouldn't have it enabled at all.
This commit is contained in:
Hornwitser 2025-07-09 17:57:49 +02:00
parent a33c8e9dac
commit 0d0e38e4b6
14 changed files with 212 additions and 141 deletions

29
components/LogIn.vue Normal file
View file

@ -0,0 +1,29 @@
<!--
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div>
<LogInDemo v-if="authDemoEnabled" />
<LogInTelegram v-if="authTelegramEnabled" />
<template v-if="noAuth">No authentication method has been enabled in the configuration.</template>
</div>
</template>
<script lang="ts" setup>
const runtimeConfig = useRuntimeConfig();
const authDemoEnabled = runtimeConfig.public.authDemoEnabled;
const authTelegramEnabled = runtimeConfig.public.authTelegramEnabled;
const noAuth =
!authDemoEnabled
&& !authTelegramEnabled
;
</script>
<style lang="css" scoped>
div>* + *::before {
content: "\2013 or \2013 ";
display: block;
margin-block: 0.5rem;
}
</style>

41
components/LogInDemo.vue Normal file
View file

@ -0,0 +1,41 @@
<!--
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<form @submit.prevent="logIn">
<label>
Demo:
<input v-model="name" type="text" placeholder="Name" required>
<button type="submit">Log In</button>
</label>
</form>
</template>
<script lang="ts" setup>
const sessionStore = useSessionStore();
const route = useRoute();
const name = ref("");
async function logIn() {
try {
const session = await $fetch("/api/auth/ap/demo-login", {
method: "POST",
body: { name: name.value },
});
await sessionStore.update(session);
if (!session.account) {
await navigateTo("/register");
} else if (route.path === "/register") {
await navigateTo("/");
}
} catch (err: any) {
alert(err.data?.message ?? err.message);
}
}
</script>
<style>
</style>