Skip to content

Commit c1058f1

Browse files
committed
🐛 Enable one-click login to guest account (#1258)
1 parent ac62fc4 commit c1058f1

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/lib/components/AuthForm.svelte

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { tick } from 'svelte';
3-
import { writable } from 'svelte/store';
3+
import { goto } from '$app/navigation';
44
import { Card, Button, Label, Input, Hr } from 'flowbite-svelte';
55
66
// 必要なコンポーネントだけを読み込んで、コンパイルを時間を短縮
@@ -26,7 +26,8 @@
2626
export let alternativePageLink: string;
2727
2828
const { form, message, errors, submitting, enhance } = formProperties;
29-
let isSubmitting = writable(false);
29+
30+
$: isSubmitting = false;
3031
3132
async function handleLoginAsGuest(event: Event) {
3233
if (!event || (event instanceof KeyboardEvent && event.key !== 'Enter')) {
@@ -35,28 +36,41 @@
3536
}
3637
3738
event.preventDefault();
38-
$isSubmitting = true;
39-
40-
$form.username = GUEST_USER_NAME;
4139
4240
// HACK: ローカル環境のパスワードは一時的に書き換えて対応している
4341
//
4442
// See:
4543
// src/lib/constants/forms.ts
44+
$form.username = GUEST_USER_NAME;
4645
// $form.password = GUEST_USER_PASSWORD_FOR_LOCAL;
4746
$form.password = GUEST_USER_PASSWORD;
4847
49-
// $formの更新後にフォームを送信
50-
await tick();
51-
const authForm = document.getElementById('auth-form') as HTMLFormElement;
52-
53-
if (authForm instanceof HTMLFormElement) {
54-
authForm.submit();
55-
} else {
56-
console.error('Failed to submit the form');
48+
try {
49+
// $formの更新後にフォームを送信
50+
await tick();
51+
isSubmitting = true;
52+
const formElement = event.target instanceof Element ? event.target.closest('form') : null;
53+
54+
if (!formElement || !(formElement instanceof HTMLFormElement)) {
55+
throw new Error('Not found form element or HTMLFormElement');
56+
}
57+
58+
const response = await fetch('/login', {
59+
method: 'POST',
60+
body: new FormData(formElement),
61+
});
62+
63+
if (!response.ok) {
64+
console.error('Failed to login as a guest: ', response);
65+
return;
66+
}
67+
68+
await goto('/');
69+
} catch (error) {
70+
console.error('Failed to login as a guest: ', error);
71+
} finally {
72+
isSubmitting = false;
5773
}
58-
59-
$isSubmitting = false;
6074
}
6175
6276
const UNFOCUSABLE = -1;
@@ -72,7 +86,6 @@
7286
<Card class="w-full max-w-md">
7387
<form id="auth-form" method="post" use:enhance class="flex flex-col space-y-6">
7488
<h3 class="text-xl font-medium text-gray-900 dark:text-white">{title}</h3>
75-
7689
<MessageHelperWrapper message={$message} />
7790

7891
{#if title === LOGIN_LABEL}
@@ -81,7 +94,7 @@
8194
class="w-full"
8295
on:click={handleLoginAsGuest}
8396
on:keydown={handleLoginAsGuest}
84-
disabled={$submitting || $isSubmitting}
97+
disabled={isSubmitting || $submitting}
8598
>
8699
<div class="text-md">お試し用のアカウントでログイン</div>
87100
</Button>
@@ -104,7 +117,7 @@
104117
placeholder="chokudai"
105118
aria-invalid={$errors.username ? 'true' : undefined}
106119
bind:value={$form.username}
107-
disabled={$submitting}
120+
disabled={$submitting || isSubmitting}
108121
required
109122
>
110123
<UserOutlineSolid slot="left" class="w-5 h-5" tabindex={UNFOCUSABLE} />
@@ -129,7 +142,7 @@
129142
placeholder="•••••••"
130143
aria-invalid={$errors.password ? 'true' : undefined}
131144
bind:value={$form.password}
132-
disabled={$submitting}
145+
disabled={$submitting || isSubmitting}
133146
required
134147
>
135148
<!-- Show / hide password -->
@@ -160,7 +173,7 @@
160173
</a>
161174
</div> -->
162175

163-
<Button type="submit" class="w-full" disabled={$submitting}>
176+
<Button type="submit" class="w-full" disabled={$submitting || isSubmitting}>
164177
{submitButtonLabel}
165178
</Button>
166179

0 commit comments

Comments
 (0)