diff options
Diffstat (limited to 'apps/accounts/src/app/pages/reset-password.svelte')
| -rw-r--r-- | apps/accounts/src/app/pages/reset-password.svelte | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/apps/accounts/src/app/pages/reset-password.svelte b/apps/accounts/src/app/pages/reset-password.svelte new file mode 100644 index 0000000..56c4f62 --- /dev/null +++ b/apps/accounts/src/app/pages/reset-password.svelte @@ -0,0 +1,135 @@ +<script> + import {querystring, link} from "svelte-spa-router"; + import {check_forgot_password_request, fulfill_forgot_password_request} from "$shared/lib/api/user"; + import Alert from "$shared/components/alert.svelte"; + import Button from "$shared/components/button.svelte"; + import Layout from "./_layout.svelte"; + + const requestId = new URLSearchParams($querystring).get("id"); + let isLoading = false; + let newPassword; + let newPasswordError; + let alert = { + title: "", + type: "", + message: "", + isVisible: false, + show(type, obj) { + alert.title = obj.title; + alert.message = obj.text; + alert.type = type; + alert.isVisible = true; + isLoading = false; + }, + hide() { + alert.isVisible = false; + alert.title = ""; + alert.message = ""; + alert.type = ""; + isLoading = false; + }, + }; + + function is_valid() { + let isValid = true; + if (!newPassword.length > 5) { + newPasswordError = "The new password must be at least 5 characters"; + isValid = false; + } + return isValid; + } + + async function submit() { + if (isLoading) { + return; + } + if (is_valid()) { + isLoading = true; + const response = await fulfill_forgot_password_request(requestId, newPassword); + if (response.ok) { + alert.show("success", { + title: "Your new password is set", + text: "<a href='/#/login'>Click here to log in</a>", + }); + } else { + console.error(response.data); + alert.show("error", { + title: response.data?.title ?? "An error occured", + text: response.data?.text ?? "Please try again soon", + }); + } + } + } + + async function is_valid_password_reset_request() { + const response = await check_forgot_password_request(requestId); + if (response.ok) { + return response.data === true; + } + return false; + } +</script> + +<Layout> + <form on:submit|preventDefault={submit} + class="margin-bottom-md max-width-xxs {isLoading ? 'c-disabled loading' : ''}"> + {#if requestId} + {#await is_valid_password_reset_request()} + <p>Checking your request...</p> + <a href="/login" + use:link>cancel</a> + {:then isActive} + {#if isActive === true} + <fieldset> + <legend class="form-legend"> + <span class="margin-bottom-xs">Set your new password</span> <br/> + <span class="text-sm"> + ... or + <a href="/login" + use:link> log in </a> + </span> + </legend> + <div class="margin-bottom-xxs max-width-xxs"> + <Alert visible={alert.isVisible} + title={alert.title} + message={alert.message} + type={alert.type}/> + </div> + <div class="margin-bottom-xs"> + <input + type="password" + id="new-password" + placeholder="New password" + class="form-control width-100%" + bind:value={newPassword} + /> + {#if newPasswordError} + <small class="color-danger">{newPasswordError}</small> + {/if} + </div> + <div class="flex justify-end"> + <Button text="Set new password" + type="primary" + loading={isLoading} + on:click={submit}/> + </div> + </fieldset> + {:else} + <Alert title="This request is expired" + message="Please submit the forgot password form again" + type="warning"/> + <div class="flex justify-between width-100% margin-y-sm"> + <a href="/forgot" + use:link>Go to forgot form</a> + <a href="/login" + use:link>Go to login form</a> + </div> + {/if} + {:catch _} + <Alert title="An error occured" + message="Please try again soon" + type="error"/> + {/await} + {/if} + </form> +</Layout> |
