refactored installer and removed RequireSuperuserAuthOnlyIfAny

This commit is contained in:
Gani Georgiev
2024-11-05 21:12:17 +02:00
parent 4f67dba6cb
commit 9506669095
61 changed files with 4722 additions and 4937 deletions
+1 -30
View File
@@ -1,24 +1,10 @@
<script>
import FullPage from "@/components/base/FullPage.svelte";
import Installer from "@/components/base/Installer.svelte";
import ApiClient from "@/utils/ApiClient";
import { tick } from "svelte";
import { replace } from "svelte-spa-router";
let showInstaller = false;
import ApiClient from "@/utils/ApiClient";
handler();
function handler() {
showInstaller = false;
const realQueryParams = new URLSearchParams(window.location.search);
if (realQueryParams.has(import.meta.env.PB_INSTALLER_PARAM)) {
ApiClient.logout(false);
showInstaller = true;
return;
}
if (ApiClient.authStore.isValid) {
replace("/collections");
} else {
@@ -26,18 +12,3 @@
}
}
</script>
{#if showInstaller}
<FullPage>
<Installer
on:submit={async () => {
showInstaller = false;
await tick();
// clear the installer param
window.location.search = "";
}}
/>
</FullPage>
{/if}
-157
View File
@@ -1,157 +0,0 @@
<script>
import { createEventDispatcher } from "svelte";
import ApiClient from "@/utils/ApiClient";
import { addInfoToast } from "@/stores/toasts";
import { confirm } from "@/stores/confirmation";
import Field from "@/components/base/Field.svelte";
const dispatch = createEventDispatcher();
let email = "";
let password = "";
let passwordConfirm = "";
let isLoading = false;
let isUploading = false;
let backupFileInput;
$: isBusy = isLoading || isUploading;
async function submit() {
if (isBusy) {
return;
}
isLoading = true;
try {
await ApiClient.collection("_superusers").create({
email,
password,
passwordConfirm,
});
await ApiClient.collection("_superusers").authWithPassword(email, password);
dispatch("submit");
} catch (err) {
ApiClient.error(err);
}
isLoading = false;
}
function resetSelectedBackupFile() {
if (backupFileInput) {
backupFileInput.value = "";
}
}
function uploadBackupConfirm(file) {
if (!file) {
return;
}
confirm(
`Note that we don't perform validations for the uploaded backup files. Proceed with caution and only if you trust the file source.\n\n` +
`Do you really want to upload and initialize "${file.name}"?`,
() => {
uploadBackup(file);
},
() => {
resetSelectedBackupFile();
},
);
}
async function uploadBackup(file) {
if (!file || isBusy) {
return;
}
isUploading = true;
try {
await ApiClient.backups.upload({ file: file });
await ApiClient.backups.restore(file.name);
addInfoToast("Please wait while extracting the uploaded archive!");
// optimistic restore completion
await new Promise((r) => setTimeout(r, 2000));
dispatch("submit");
} catch (err) {
ApiClient.error(err);
}
resetSelectedBackupFile();
isUploading = false;
}
</script>
<form class="block" autocomplete="off" on:submit|preventDefault={submit}>
<div class="content txt-center m-b-base">
<h4>Create your first superuser account in order to continue</h4>
</div>
<Field class="form-field required" name="email" let:uniqueId>
<label for={uniqueId}>Email</label>
<!-- svelte-ignore a11y-autofocus -->
<input type="email" autocomplete="off" id={uniqueId} bind:value={email} required autofocus />
</Field>
<Field class="form-field required" name="password" let:uniqueId>
<label for={uniqueId}>Password</label>
<input
type="password"
autocomplete="new-password"
minlength="10"
id={uniqueId}
bind:value={password}
required
/>
<div class="help-block">Recommended at least 10 characters.</div>
</Field>
<Field class="form-field required" name="passwordConfirm" let:uniqueId>
<label for={uniqueId}>Password confirm</label>
<input type="password" minlength="10" id={uniqueId} bind:value={passwordConfirm} required />
</Field>
<button
type="submit"
class="btn btn-lg btn-block btn-next"
class:btn-disabled={isBusy}
class:btn-loading={isLoading}
>
<span class="txt">Create superuser and login</span>
<i class="ri-arrow-right-line" />
</button>
</form>
<hr />
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<label
for="backupFileInput"
class="btn btn-lg btn-hint btn-transparent btn-block"
class:btn-disabled={isBusy}
class:btn-loading={isUploading}
>
<i class="ri-upload-cloud-line" />
<span class="txt">Or initialize from backup</span>
</label>
<input
bind:this={backupFileInput}
id="backupFileInput"
type="file"
class="hidden"
accept=".zip"
on:change={(e) => {
uploadBackupConfirm(e.target?.files?.[0]);
}}
/>
+197
View File
@@ -0,0 +1,197 @@
<script>
import { createEventDispatcher } from "svelte";
import { replace } from "svelte-spa-router";
import { getTokenPayload } from "pocketbase";
import ApiClient from "@/utils/ApiClient";
import { addInfoToast, addErrorToast } from "@/stores/toasts";
import { confirm } from "@/stores/confirmation";
import Field from "@/components/base/Field.svelte";
import FullPage from "@/components/base/FullPage.svelte";
export let params;
let email = "";
let password = "";
let passwordConfirm = "";
let isLoading = false;
let isUploading = false;
let backupFileInput;
$: isBusy = isLoading || isUploading;
checkToken();
async function checkToken() {
if (!params?.token) {
return replace("/");
}
isLoading = true;
try {
const payload = getTokenPayload(params?.token);
await ApiClient.collection("_superusers").getOne(payload.id, {
requestKey: "installer_token_check",
headers: { Authorization: params?.token },
});
} catch (err) {
if (!err?.isAbort) {
addErrorToast("The installer token is invalid or has expired.");
replace("/");
}
}
isLoading = false;
}
async function submit() {
if (isBusy) {
return;
}
isLoading = true;
try {
await ApiClient.collection("_superusers").create({
email,
password,
passwordConfirm,
}, {
headers: { Authorization: params?.token },
});
await ApiClient.collection("_superusers").authWithPassword(email, password);
replace("/");
} catch (err) {
ApiClient.error(err);
}
isLoading = false;
}
function resetSelectedBackupFile() {
if (backupFileInput) {
backupFileInput.value = "";
}
}
function uploadBackupConfirm(file) {
if (!file) {
return;
}
confirm(
`Note that we don't perform validations for the uploaded backup files. Proceed with caution and only if you trust the file source.\n\n` +
`Do you really want to upload and initialize "${file.name}"?`,
() => {
uploadBackup(file);
},
() => {
resetSelectedBackupFile();
},
);
}
async function uploadBackup(file) {
if (!file || isBusy) {
return;
}
isUploading = true;
try {
await ApiClient.backups.upload({ file: file }, {
headers: { Authorization: params?.token },
});
await ApiClient.backups.restore(file.name, {
headers: { Authorization: params?.token },
});
addInfoToast("Please wait while extracting the uploaded archive!");
// optimistic restore completion
await new Promise((r) => setTimeout(r, 2000));
replace("/");
} catch (err) {
ApiClient.error(err);
}
resetSelectedBackupFile();
isUploading = false;
}
</script>
<FullPage>
<form class="block" autocomplete="off" on:submit|preventDefault={submit}>
<div class="content txt-center m-b-base">
<h4>Create your first superuser account in order to continue</h4>
</div>
<Field class="form-field required" name="email" let:uniqueId>
<label for={uniqueId}>Email</label>
<!-- svelte-ignore a11y-autofocus -->
<input type="email" autocomplete="off" id={uniqueId} disabled={isBusy} bind:value={email} required autofocus />
</Field>
<Field class="form-field required" name="password" let:uniqueId>
<label for={uniqueId}>Password</label>
<input
type="password"
autocomplete="new-password"
minlength="10"
id={uniqueId}
disabled={isBusy}
bind:value={password}
required
/>
<div class="help-block">Recommended at least 10 characters.</div>
</Field>
<Field class="form-field required" name="passwordConfirm" let:uniqueId>
<label for={uniqueId}>Password confirm</label>
<input type="password" minlength="10" id={uniqueId} disabled={isBusy} bind:value={passwordConfirm} required />
</Field>
<button
type="submit"
class="btn btn-lg btn-block btn-next"
class:btn-disabled={isBusy}
class:btn-loading={isLoading}
>
<span class="txt">Create superuser and login</span>
<i class="ri-arrow-right-line" />
</button>
</form>
<hr />
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<label
for="backupFileInput"
class="btn btn-lg btn-hint btn-transparent btn-block"
class:btn-disabled={isBusy}
class:btn-loading={isUploading}
>
<i class="ri-upload-cloud-line" />
<span class="txt">Or initialize from backup</span>
</label>
<input
bind:this={backupFileInput}
id="backupFileInput"
type="file"
class="hidden"
accept=".zip"
on:change={(e) => {
uploadBackupConfirm(e.target?.files?.[0]);
}}
/>
</FullPage>