-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add forgot password component #104
Changes from 11 commits
76c7fd4
22a4e01
60f042d
7e49063
4c7d653
a7af4fa
c6fef41
46735f0
22376cd
0eff1dd
752349d
30c990c
69faaf3
20069f5
83eb2d9
d5a996d
e7e098b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||||
<script lang="ts"> | ||||||
import { goto } from '$app/navigation'; | ||||||
import { base } from '$app/paths'; | ||||||
import { resetForgotPassword } from '$lib/client/services.gen'; | ||||||
import AlertMessage from '$lib/components/AlertMessage.svelte'; | ||||||
import DataInput from '$lib/components/DataInput/DataInput.svelte'; | ||||||
import { Button, Card, Heading, Input } from 'flowbite-svelte'; | ||||||
import { _ } from 'svelte-i18n'; | ||||||
|
||||||
const maildata = { | ||||||
component: Input, | ||||||
type: 'text', | ||||||
value: null, | ||||||
props: { | ||||||
placeholder: $_('user.forgotPw.placeholder') | ||||||
} | ||||||
}; | ||||||
|
||||||
let alertMessage: string = $_('user.forgotPw.formatError'); | ||||||
let valid: boolean = true; | ||||||
let showAlert: boolean = !valid; | ||||||
let showSuccess: boolean = false; | ||||||
|
||||||
function validateEmail(value: string | null): boolean { | ||||||
if (value === null) { | ||||||
return false; | ||||||
} else { | ||||||
const mailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||||||
return mailRegex.test(value); | ||||||
} | ||||||
} | ||||||
|
||||||
async function submitData(mailstring: string): Promise<void> { | ||||||
const data = { | ||||||
body: { | ||||||
email: mailstring | ||||||
}, | ||||||
throwOnError: true | ||||||
}; | ||||||
|
||||||
await resetForgotPassword(data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not recommended to mix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. With respect to the linked blog post I don´t think it's a problem in this specific case because it has a catch block at the end, but as a general rule it's certainly valid... |
||||||
.then((returned) => { | ||||||
console.log('successful transmission, response status: ', returned.response.status); | ||||||
showSuccess = true; | ||||||
}) | ||||||
.catch((error) => { | ||||||
console.log(error); | ||||||
showAlert = true; | ||||||
alertMessage = $_('user.forgotPw.sendError'); | ||||||
}); | ||||||
} | ||||||
</script> | ||||||
|
||||||
{#if showAlert} | ||||||
<AlertMessage | ||||||
title={$_('user.forgotPw.alertTitle')} | ||||||
message={alertMessage} | ||||||
lastpage={`${base}/userLand/lostPassword`} | ||||||
onclick={() => { | ||||||
showAlert = false; | ||||||
}} | ||||||
/> | ||||||
{/if} | ||||||
|
||||||
<Card class="container m-2 mx-auto w-full max-w-xl items-center justify-center p-2"> | ||||||
<Heading | ||||||
tag="h3" | ||||||
class="m-2 p-2 text-center font-bold tracking-tight text-gray-700 dark:text-gray-400" | ||||||
>{$_('user.forgotPw.heading')}</Heading | ||||||
> | ||||||
{#if showSuccess === false} | ||||||
<div class="m-2 mx-auto w-full flex-col space-y-6 p-2"> | ||||||
<DataInput | ||||||
component={maildata.component} | ||||||
bind:value={maildata.value} | ||||||
properties={maildata.props} | ||||||
/> | ||||||
</div> | ||||||
|
||||||
<div class="m-2 flex w-full items-center justify-center p-2"> | ||||||
<Button | ||||||
size="md" | ||||||
on:click={(event) => { | ||||||
valid = validateEmail(maildata.value); | ||||||
showAlert = !valid; | ||||||
if (valid) { | ||||||
submitData(maildata.value); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wrap the inputs in a form and make the button of type "submit" then the user pressing enter will press the button, e.g.
|
||||||
} | ||||||
}}>{$_('user.forgotPw.pending')}</Button | ||||||
> | ||||||
</div> | ||||||
{:else} | ||||||
<div class="m-2 flex w-full items-center justify-center p-2"> | ||||||
<p>{$_('user.forgotPw.mailSentMessage')}</p> | ||||||
</div> | ||||||
<div class="m-2 flex w-full items-center justify-center p-2"> | ||||||
<Button | ||||||
size="md" | ||||||
on:click={(event) => { | ||||||
goto(`/${base}`); | ||||||
}}>{$_('user.forgotPw.success')}</Button | ||||||
> | ||||||
</div> | ||||||
{/if} | ||||||
</Card> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
import UserForgotPassword from '$lib/components/UserForgotPassword.svelte'; | ||
</script> | ||
|
||
<UserForgotPassword /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you make the type 'email' and make it required then you get validation of the email automatically: see e.g. https://github.com/ssciwr/mondey/blob/main/frontend/src/lib/components/Admin/Login.svelte#L24-L31