-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix forgot password functionality (#217)
* adjust forgot password page, add backend email sending function * add backend connection and email sending * finish implementation * add tests for password reset * remove some old debug output
- Loading branch information
Showing
8 changed files
with
202 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<svelte:options runes={true} /> | ||
<script lang="ts"> | ||
import { page } from "$app/stores"; | ||
import { resetResetPassword } from "$lib/client/services.gen"; | ||
import AlertMessage from "$lib/components/AlertMessage.svelte"; | ||
import DataInput from "$lib/components/DataInput/DataInput.svelte"; | ||
import { preventDefault } from "$lib/util"; | ||
import { Button, Card, Heading, Input } from "flowbite-svelte"; | ||
import { CheckCircleOutline } from "flowbite-svelte-icons"; | ||
import { onMount } from "svelte"; | ||
import { _ } from "svelte-i18n"; | ||
let pw = $state(""); | ||
let confirmPw = $state(""); | ||
let showAlert = $state(false); | ||
let alertMessage = $state($_("forgotPw.confirmError")); | ||
let success: boolean = $state(false); | ||
onMount(() => { | ||
if ( | ||
$page.params.code === undefined || | ||
$page.params.code === null || | ||
$page.params.code === "" | ||
) { | ||
alertMessage = $_("forgotPw.codeError"); | ||
showAlert = true; | ||
} | ||
}); | ||
async function submitData(): Promise<void> { | ||
if (pw !== confirmPw) { | ||
showAlert = true; | ||
return; | ||
} | ||
const { data, error } = await resetResetPassword({ | ||
body: { token: $page.params.code, password: pw }, | ||
}); | ||
if ((!error && data) || error?.detail === "VERIFY_USER_ALREADY_VERIFIED") { | ||
success = true; | ||
return; | ||
} | ||
console.log(error); | ||
alertMessage = $_("forgotPw.sendError"); | ||
showAlert = true; | ||
success = false; | ||
} | ||
</script> | ||
|
||
{#if showAlert === true} | ||
<AlertMessage title={$_('forgotPw.Error')} message={alertMessage} onclick={() => { | ||
showAlert = false; | ||
}}/> | ||
{:else} | ||
{#if success === true} | ||
<div class="flex flex-row"> | ||
<CheckCircleOutline size="xl" color="green" class="m-2"/> | ||
<div class="m-2 p-2"> | ||
{$_('forgotPw.successReset')} | ||
</div> | ||
</div> | ||
<Button href="/userLand/userLogin" size="md">{$_('forgotPw.goToLogin')}</Button> | ||
{:else} | ||
<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" | ||
>{$_('forgotPw.resetHeading')}</Heading> | ||
|
||
<form onsubmit={preventDefault(submitData)} class = "space-y-4"> | ||
<div class="m-2 mx-auto w-full flex-col space-y-6 p-2"> | ||
|
||
<DataInput component = {Input} bind:value={pw} required={true} id="restPw" kwargs={{type: "password"}} label={$_("forgotPw.inputlabelPw")}/> | ||
|
||
<DataInput component = {Input} bind:value={confirmPw} required={true} id="restConfirmPw" kwargs={{type: "password"}} label={$_("forgotPw.inputlabelPwConfirm")}/> | ||
</div> | ||
<div class="m-2 flex w-full items-center justify-center p-2"> | ||
<Button size="md" type="submit">{$_('forgotPw.pending')}</Button> | ||
</div> | ||
</form> | ||
</Card> | ||
{/if} | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,18 @@ def send_email_validation_link(email: str, token: str) -> None: | |
s.send_message(msg) | ||
|
||
|
||
def send_reset_password_link(email: str, token: str) -> None: | ||
msg = EmailMessage() | ||
msg["From"] = "[email protected]" | ||
msg["To"] = email | ||
msg["Subject"] = "MONDEY Passwort zurücksetzen" | ||
msg.set_content( | ||
f"Bitte klicken Sie hier, um Ihr MONDEY Passwort zurückzusetzen:\n\nhttps://mondey.lkeegan.dev/resetPassword/{token}\n\n-----\n\nPlease click here to reset your MONDEY password:\n\nhttps://mondey.lkeegan.dev/resetPassword/{token}" | ||
) | ||
with smtplib.SMTP(app_settings.SMTP_HOST) as s: | ||
s.send_message(msg) | ||
|
||
|
||
class UserManager(IntegerIDMixin, BaseUserManager[User, int]): | ||
reset_password_token_secret = app_settings.SECRET | ||
verification_token_secret = app_settings.SECRET | ||
|
@@ -60,7 +72,8 @@ async def on_after_register(self, user: User, request: Request | None = None): | |
async def on_after_forgot_password( | ||
self, user: User, token: str, request: Request | None = None | ||
): | ||
print(f"User {user.id} has forgot their password. Reset token: {token}") | ||
logging.info(f"User {user.id} has forgot their password. Reset token: {token}") | ||
send_reset_password_link(user.email, token) | ||
|
||
async def on_after_request_verify( | ||
self, user: User, token: str, request: Request | None = None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,62 @@ def test_register_new_user_valid_research_code( | |
new_user = admin_client.get("/admin/users/").json()[-1] | ||
assert new_user["email"] == email | ||
assert new_user["research_group_id"] == 123451 | ||
|
||
|
||
def test_user_reset_password(user_client: TestClient, smtp_mock: SMTPMock): | ||
assert smtp_mock.last_message is None | ||
email = "[email protected]" | ||
response = user_client.post("/auth/forgot-password", json={"email": email}) | ||
assert response.status_code == 202 | ||
|
||
msg = smtp_mock.last_message | ||
assert msg is not None | ||
assert msg.get("To") == email | ||
token = msg.get_content().split("\n\n")[1].rsplit("/")[-1] | ||
new_password = "new_password" | ||
response = user_client.post( | ||
"/auth/reset-password", json={"token": token, "password": new_password} | ||
) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_user_reset_password_invalid_token( | ||
user_client: TestClient, smtp_mock: SMTPMock | ||
): | ||
assert smtp_mock.last_message is None | ||
email = "[email protected]" | ||
response = user_client.post("/auth/forgot-password", json={"email": email}) | ||
assert response.status_code == 202 | ||
|
||
msg = smtp_mock.last_message | ||
assert msg is not None | ||
assert msg.get("To") == email | ||
token = msg.get_content().split("\n\n")[1].rsplit("/")[-1] + "invalid" | ||
new_password = "new_password" | ||
response = user_client.post( | ||
"/auth/reset-password", json={"token": token, "password": new_password} | ||
) | ||
assert response.status_code == 400 | ||
|
||
|
||
def test_user_forgot_password( | ||
user_client: TestClient, active_user, smtp_mock: SMTPMock | ||
): | ||
assert smtp_mock.last_message is None | ||
response = user_client.post( | ||
"/auth/forgot-password", json={"email": active_user.email} | ||
) | ||
assert response.status_code == 202 | ||
|
||
|
||
def test_user_forgot_password_invalid_email( | ||
user_client: TestClient, smtp_mock: SMTPMock | ||
): | ||
assert smtp_mock.last_message is None | ||
email = "invalid-email" | ||
response = user_client.post("/auth/forgot-password", json={"email": email}) | ||
assert ( | ||
response.json()["detail"][0]["msg"] | ||
== "value is not a valid email address: An email address must have an @-sign." | ||
) | ||
assert response.json()["detail"][0]["type"] == "value_error" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters