pocketbase/ui/src/components/records/fields/AuthFields.svelte

156 lines
5.6 KiB
Svelte
Raw Normal View History

2022-10-30 16:28:14 +08:00
<script>
import { slide } from "svelte/transition";
import CommonHelper from "@/utils/CommonHelper";
import tooltip from "@/actions/tooltip";
import { confirm } from "@/stores/confirmation";
import { removeError } from "@/stores/errors";
import Field from "@/components/base/Field.svelte";
2023-08-15 02:20:49 +08:00
export let record;
export let collection;
2023-08-22 18:01:08 +08:00
export let isNew = !record.id;
2022-10-30 16:28:14 +08:00
let originalUsername = record.username || null;
let changePasswordToggle = false;
$: if (!record.username && record.username !== null) {
record.username = null;
}
$: if (!changePasswordToggle) {
record.password = null;
record.passwordConfirm = null;
removeError("password");
removeError("passwordConfirm");
}
</script>
<div class="grid m-b-base">
<div class="col-lg-6">
2023-04-17 18:28:41 +08:00
<Field class="form-field {!isNew ? 'required' : ''}" name="username" let:uniqueId>
2022-10-30 16:28:14 +08:00
<label for={uniqueId}>
<i class={CommonHelper.getFieldTypeIcon("user")} />
<span class="txt">Username</span>
</label>
<input
type="text"
2023-04-17 18:28:41 +08:00
requried={!isNew}
placeholder={isNew ? "Leave empty to auto generate..." : originalUsername}
2022-10-30 16:28:14 +08:00
id={uniqueId}
bind:value={record.username}
/>
</Field>
</div>
<div class="col-lg-6">
<Field
class="form-field {collection.options?.requireEmail ? 'required' : ''}"
name="email"
let:uniqueId
>
<label for={uniqueId}>
<i class={CommonHelper.getFieldTypeIcon("email")} />
<span class="txt">Email</span>
</label>
<div class="form-field-addon email-visibility-addon">
<button
type="button"
class="btn btn-sm btn-transparent {record.emailVisibility ? 'btn-success' : 'btn-hint'}"
2022-10-30 16:28:14 +08:00
use:tooltip={{
text: "Make email public or private",
position: "top-right",
}}
on:click={() => (record.emailVisibility = !record.emailVisibility)}
>
<span class="txt">Public: {record.emailVisibility ? "On" : "Off"}</span>
</button>
</div>
<!-- svelte-ignore a11y-autofocus -->
<input
type="email"
2023-04-17 18:28:41 +08:00
autofocus={isNew}
2022-10-30 16:28:14 +08:00
autocomplete="off"
id={uniqueId}
required={collection.options?.requireEmail}
bind:value={record.email}
/>
</Field>
</div>
<div class="col-lg-12">
2023-04-17 18:28:41 +08:00
{#if !isNew}
2022-10-30 16:28:14 +08:00
<Field class="form-field form-field-toggle" name="verified" let:uniqueId>
<input type="checkbox" id={uniqueId} bind:checked={changePasswordToggle} />
<label for={uniqueId}>Change password</label>
</Field>
{/if}
2023-04-17 18:28:41 +08:00
{#if isNew || changePasswordToggle}
2022-10-30 16:28:14 +08:00
<div class="block" transition:slide|local={{ duration: 150 }}>
<div class="grid" class:p-t-xs={changePasswordToggle}>
<div class="col-sm-6">
<Field class="form-field required" name="password" let:uniqueId>
<label for={uniqueId}>
<i class="ri-lock-line" />
<span class="txt">Password</span>
</label>
<input
type="password"
autocomplete="new-password"
id={uniqueId}
required
bind:value={record.password}
/>
</Field>
</div>
<div class="col-sm-6">
<Field class="form-field required" name="passwordConfirm" let:uniqueId>
<label for={uniqueId}>
<i class="ri-lock-line" />
<span class="txt">Password confirm</span>
</label>
<input
type="password"
autocomplete="new-password"
id={uniqueId}
required
bind:value={record.passwordConfirm}
/>
</Field>
</div>
</div>
</div>
{/if}
</div>
<div class="col-lg-12">
<Field class="form-field form-field-toggle" name="verified" let:uniqueId>
<input
type="checkbox"
id={uniqueId}
bind:checked={record.verified}
on:change|preventDefault={(e) => {
2023-04-17 18:28:41 +08:00
if (isNew) {
2022-10-30 16:28:14 +08:00
return; // no confirmation required
}
confirm(
`Do you really want to manually change the verified account state?`,
() => {},
() => {
record.verified = !e.target.checked;
}
);
}}
/>
<label for={uniqueId}>Verified</label>
</Field>
</div>
</div>
<style>
.email-visibility-addon ~ input {
padding-right: 100px;
}
</style>