2025-06-30 18:58:24 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
-->
|
2025-03-07 12:41:57 +01:00
|
|
|
<template>
|
|
|
|
<main>
|
|
|
|
<h1>Log In</h1>
|
|
|
|
<form @submit.prevent="logIn">
|
|
|
|
<input v-model="name" type="text" placeholder="Name" required>
|
|
|
|
<button type="submit">Log In</button>
|
|
|
|
</form>
|
2025-03-07 23:53:57 +01:00
|
|
|
<h2 id="create-account">Create Account</h2>
|
|
|
|
<p>If you don't have an account you may create one</p>
|
|
|
|
<form @submit.prevent="createAccount">
|
|
|
|
<fieldset>
|
|
|
|
<legend>Regular Account</legend>
|
|
|
|
<label>
|
|
|
|
Name:
|
|
|
|
<input v-model="createName" type="text" placeholder="Name" required />
|
|
|
|
</label>
|
|
|
|
<button type="submit">Create account</button>
|
|
|
|
</fieldset>
|
|
|
|
</form>
|
|
|
|
<p>
|
|
|
|
If you do not wish to deal with logins you may create an anonymous account tied to this device.
|
|
|
|
</p>
|
|
|
|
<fieldset>
|
|
|
|
<legend>Anonymous Account</legend>
|
|
|
|
<button type="button" @click="createAnonymousAccount">Create anonymous account</button>
|
|
|
|
</fieldset>
|
2025-03-07 12:41:57 +01:00
|
|
|
<pre><code>{{ result }}</code></pre>
|
2025-05-24 17:53:33 +02:00
|
|
|
<pre><code>Session: {{ ({ id: sessionStore.id, account: sessionStore.account, push: sessionStore.push }) }}</code></pre>
|
2025-03-07 12:41:57 +01:00
|
|
|
</main>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2025-05-24 17:53:33 +02:00
|
|
|
const sessionStore = useSessionStore();
|
2025-03-07 16:08:45 +01:00
|
|
|
const { getSubscription, subscribe } = usePushNotification();
|
2025-03-07 12:41:57 +01:00
|
|
|
|
|
|
|
const name = ref("");
|
|
|
|
const result = ref("")
|
|
|
|
async function logIn() {
|
|
|
|
try {
|
2025-05-24 17:53:33 +02:00
|
|
|
result.value = await sessionStore.logIn(name.value);
|
2025-03-07 16:08:45 +01:00
|
|
|
// Resubscribe push notifications if the user was subscribed before.
|
|
|
|
const subscription = await getSubscription();
|
|
|
|
if (subscription) {
|
|
|
|
await subscribe();
|
|
|
|
}
|
2025-05-24 17:53:33 +02:00
|
|
|
// XXX Remove the need for this.
|
|
|
|
await sessionStore.fetch();
|
2025-03-07 12:41:57 +01:00
|
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
console.log(err);
|
|
|
|
console.log(err.data);
|
|
|
|
result.value = `Server replied: ${err.statusCode} ${err.statusMessage}`;
|
|
|
|
}
|
|
|
|
}
|
2025-03-07 23:53:57 +01:00
|
|
|
|
|
|
|
const createName = ref("");
|
|
|
|
async function createAccount() {
|
|
|
|
try {
|
2025-05-31 21:44:19 +02:00
|
|
|
const res = await $fetch.raw("/api/auth/account", {
|
2025-03-07 23:53:57 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
},
|
|
|
|
body: new URLSearchParams({ name: createName.value })
|
|
|
|
});
|
|
|
|
result.value = `Server replied: ${res.status} ${res.statusText}`;
|
2025-05-24 17:53:33 +02:00
|
|
|
await sessionStore.fetch();
|
2025-03-07 23:53:57 +01:00
|
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
console.log(err);
|
|
|
|
console.log(err.data);
|
|
|
|
result.value = `Server replied: ${err.statusCode} ${err.statusMessage}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function createAnonymousAccount() {
|
|
|
|
try {
|
2025-05-31 21:44:19 +02:00
|
|
|
const res = await $fetch.raw("/api/auth/account", {
|
2025-03-07 23:53:57 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
result.value = `Server replied: ${res.status} ${res.statusText}`;
|
2025-05-24 17:53:33 +02:00
|
|
|
await sessionStore.fetch();
|
2025-03-07 23:53:57 +01:00
|
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
console.log(err);
|
|
|
|
console.log(err.data);
|
|
|
|
result.value = `Server replied: ${err.statusCode} ${err.statusMessage}`;
|
|
|
|
}
|
|
|
|
}
|
2025-03-07 12:41:57 +01:00
|
|
|
</script>
|