Skip to content

Commit cc0ae8f

Browse files
authored
Merge pull request #1253 from AtCoder-NoviSteps/#1252
✨ Enable one-click login to guest account (#1252)
2 parents 6b3a81f + 9e453f0 commit cc0ae8f

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/lib/components/AuthForm.svelte

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script lang="ts">
2-
import { Card, Button, Label, Input } from 'flowbite-svelte';
2+
import { tick } from 'svelte';
3+
import { writable } from 'svelte/store';
4+
import { Card, Button, Label, Input, Hr } from 'flowbite-svelte';
35
46
// 必要なコンポーネントだけを読み込んで、コンパイルを時間を短縮
5-
import Checkbox from 'flowbite-svelte/Checkbox.svelte';
67
import UserOutlineSolid from 'flowbite-svelte-icons/UserCircleSolid.svelte';
78
import EyeOutline from 'flowbite-svelte-icons/EyeOutline.svelte';
89
import EyeSlashOutline from 'flowbite-svelte-icons/EyeSlashOutline.svelte';
@@ -25,10 +26,17 @@
2526
export let alternativePageLink: string;
2627
2728
const { form, message, errors, submitting, enhance } = formProperties;
29+
let isSubmitting = writable(false);
2830
29-
let isGuest: boolean = false;
31+
async function handleLoginAsGuest(event: Event) {
32+
if (!event || (event instanceof KeyboardEvent && event.key !== 'Enter')) {
33+
event.preventDefault();
34+
return;
35+
}
36+
37+
event.preventDefault();
38+
$isSubmitting = true;
3039
31-
$: if (isGuest) {
3240
$form.username = GUEST_USER_NAME;
3341
3442
// HACK: ローカル環境のパスワードは一時的に書き換えて対応している
@@ -37,9 +45,18 @@
3745
// src/lib/constants/forms.ts
3846
// $form.password = GUEST_USER_PASSWORD_FOR_LOCAL;
3947
$form.password = GUEST_USER_PASSWORD;
40-
} else {
41-
$form.username = '';
42-
$form.password = '';
48+
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');
57+
}
58+
59+
$isSubmitting = false;
4360
}
4461
4562
const UNFOCUSABLE = -1;
@@ -53,15 +70,27 @@
5370
<!-- https://github.com/themesberg/flowbite-svelte-icons/tree/main/src/lib -->
5471
<div class="container mx-auto py-8 w-5/6 flex flex-col items-center">
5572
<Card class="w-full max-w-md">
56-
<form method="post" use:enhance class="flex flex-col space-y-6">
73+
<form id="auth-form" method="post" use:enhance class="flex flex-col space-y-6">
5774
<h3 class="text-xl font-medium text-gray-900 dark:text-white">{title}</h3>
5875

5976
<MessageHelperWrapper message={$message} />
6077

6178
{#if title === LOGIN_LABEL}
62-
<Checkbox bind:checked={isGuest}>お試し用のアカウントを使う</Checkbox>
79+
<Button
80+
type="button"
81+
class="w-full"
82+
on:click={handleLoginAsGuest}
83+
on:keydown={handleLoginAsGuest}
84+
disabled={$submitting || $isSubmitting}
85+
>
86+
<div class="text-md">お試し用のアカウントでログイン</div>
87+
</Button>
6388
{/if}
6489

90+
<div>
91+
<Hr classHr="my-2 h-0.5 bg-gray-400 dark:bg-gray-200" />
92+
</div>
93+
6594
<!-- User name -->
6695
<div class="space-y-2">
6796
<Label for="username-in-auth-form">

0 commit comments

Comments
 (0)