<!--
	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>
