-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/LBGG-565: Password recovery flow (#585)
* Create `PasswordInput` component * Create `ForgotPasswordCard` and add it to the `SiteNavBar` * Create `useRecoverAccount` composable * Create `<BasicAlert />` and `useModalAlert` composable. One test failing for some reason * Use modal composable in confirm composable * WIP: Use new composables in the `<ForgotPasswordCard />` component * Fix the `<BasicAlert/>` component and tests * Fix test for `<ForgotPasswordCard/>` * Setup middleware and composables to validate the reset token and redirect to the account recovery page * Layout account recovery page * Refactor sign up/password related form work * Enable resetting the password * Rename `/recover-account` -> `/reset-password` * Add basic missing composable tests * Add disabled state to the submit button on the reset page * Disabled reset password button if the email or username are invalid * Redirect to the home page, when password change success modal is closed * Rewrite middleware as async function. Fixes redirect bug. * Use switch statement for readability
- Loading branch information
Showing
29 changed files
with
1,022 additions
and
148 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
77 changes: 77 additions & 0 deletions
77
components/blocks/cards/ForgotPasswordCard/ForgotPasswordCard.test.ts
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,77 @@ | ||
import { mount, enableAutoUnmount, flushPromises } from '@vue/test-utils' | ||
import { getByTestId } from 'root/testUtils' | ||
import ForgotPasswordCard from './ForgotPasswordCard.vue' | ||
|
||
function getForgotPasswordCardWrapper() { | ||
return mount(ForgotPasswordCard) | ||
} | ||
|
||
const mockSuccessRecoverCreate = vi.fn(() => Promise.resolve({ ok: true })) | ||
|
||
enableAutoUnmount(afterEach) | ||
|
||
describe('<ForgotPasswordCard />', () => { | ||
it('should render without crashing', () => { | ||
const wrapper = getForgotPasswordCardWrapper() | ||
|
||
expect(wrapper.isVisible()).toBe(true) | ||
}) | ||
|
||
describe('when the close button is clicked', () => { | ||
it('should emit the close event', async () => { | ||
const wrapper = getForgotPasswordCardWrapper() | ||
|
||
await getByTestId(wrapper, 'close-button').trigger('click') | ||
|
||
expect(wrapper.emitted().close).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('when the cancel button is clicked', () => { | ||
it('should emit the cancelClick event', async () => { | ||
const wrapper = getForgotPasswordCardWrapper() | ||
|
||
await getByTestId(wrapper, 'cancel-button').trigger('click') | ||
|
||
expect(wrapper.emitted().cancelClick).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('when the reset password button is clicked', () => { | ||
describe('when everything is successful', () => { | ||
const username = 'strongbad' | ||
const emailAddress = `${username}@homestarrunner.com` | ||
|
||
it('should emit the close event', async () => { | ||
vi.mock('lib/api/Account', () => ({ | ||
Account: function Account() { | ||
this.recoverCreate = mockSuccessRecoverCreate | ||
}, | ||
})) | ||
|
||
const wrapper = getForgotPasswordCardWrapper() | ||
|
||
const emailInput = getByTestId(wrapper, 'email-input') | ||
await emailInput.setValue(emailAddress) | ||
|
||
const usernameInput = getByTestId(wrapper, 'username-input') | ||
await usernameInput.setValue(username) | ||
|
||
await getByTestId(wrapper, 'reset-password-button').trigger('click') | ||
await flushPromises() | ||
|
||
expect(wrapper.emitted().close).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('when one or more fields are invalid', () => { | ||
it('should not emit the close event', async () => { | ||
const wrapper = getForgotPasswordCardWrapper() | ||
|
||
await getByTestId(wrapper, 'reset-password-button').trigger('click') | ||
|
||
expect(wrapper.emitted().close).toBeFalsy() | ||
}) | ||
}) | ||
}) | ||
}) |
178 changes: 178 additions & 0 deletions
178
components/blocks/cards/ForgotPasswordCard/ForgotPasswordCard.vue
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,178 @@ | ||
<script setup lang="ts"> | ||
import { type Ref, ref } from 'vue' | ||
import { isEmailValid, isUsernameValid } from 'lib/form_helpers' | ||
import BaseButton from 'elements/buttons/BaseButton/BaseButton.vue' | ||
import BaseInput from 'elements/inputs/BaseInput/BaseInput.vue' | ||
import CloseButton from 'elements/buttons/CloseButton/CloseButton.vue' | ||
import CardBody from 'elements/cards/CardBody/CardBody.vue' | ||
import CardHeader from 'elements/cards/CardHeader/CardHeader.vue' | ||
import Card from 'elements/cards/Card/Card.vue' | ||
import { useRecoverAccount } from 'composables/api' | ||
import { useModalAlert } from 'composables/useModalAlert' | ||
interface ForgotPasswordCardPops { | ||
modal?: boolean | ||
} | ||
interface ForgotPasswordCardState { | ||
email: Ref<string> | ||
username: Ref<string> | ||
} | ||
const emit = defineEmits<{ | ||
(event: 'close'): void | ||
(event: 'cancelClick'): void | ||
}>() | ||
const props = withDefaults(defineProps<ForgotPasswordCardPops>(), { | ||
modal: false, | ||
}) | ||
const state: ForgotPasswordCardState = { | ||
email: ref(''), | ||
username: ref(''), | ||
} | ||
const emailValid = ref(true) | ||
const usernameValid = ref(true) | ||
const { showAlert } = useModalAlert() | ||
function clearState() { | ||
state.email.value = '' | ||
state.username.value = '' | ||
} | ||
function cancel() { | ||
clearState() | ||
emit('cancelClick') | ||
} | ||
function resetPassword() { | ||
useRecoverAccount( | ||
{ | ||
email: state.email.value, | ||
username: state.username.value, | ||
}, | ||
{ | ||
onOkay: () => { | ||
clearState() | ||
emit('close') | ||
showAlert({ | ||
body: 'If an account with that email and username exists, we will send you an email with a link to reset your password.', | ||
title: 'Success!', | ||
type: 'success', | ||
}) | ||
}, | ||
}, | ||
) | ||
} | ||
</script> | ||
|
||
<template> | ||
<Card | ||
id="forgotPasswordCard" | ||
data-testid="forgot-password-card" | ||
class="forgot-password-card" | ||
> | ||
<CardHeader class="forgot-password-card__header"> | ||
<div class="forgot-password-card__title">Forgot Password</div> | ||
<CloseButton | ||
v-show="props.modal" | ||
data-testid="close-button" | ||
@click.prevent="emit('close')" | ||
/> | ||
</CardHeader> | ||
<CardBody> | ||
<div class="forgot-password-card__body-wrapper"> | ||
<p class="instructions"> | ||
Enter the email and username associated with your account, and we'll | ||
send you a link to reset your password. | ||
</p> | ||
<BaseInput | ||
:model="state.email" | ||
name="email" | ||
type="text" | ||
:style="{ | ||
'border-color': !emailValid ? 'rgb(185 28 28 / 1)' : '', | ||
}" | ||
placeholder="Email" | ||
autocomplete="email" | ||
data-testid="email-input" | ||
@change="emailValid = isEmailValid(state.email)" | ||
/> | ||
<BaseInput | ||
:model="state.username" | ||
name="username" | ||
type="text" | ||
:style="{ | ||
'border-color': !usernameValid ? 'rgb(185 28 28 / 1)' : '', | ||
}" | ||
placeholder="Username" | ||
autocomplete="username" | ||
data-testid="username-input" | ||
@change="usernameValid = isUsernameValid(state.username)" | ||
/> | ||
<BaseButton | ||
id="reset-password-button" | ||
class="reset-password-button" | ||
data-testid="reset-password-button" | ||
:disabled=" | ||
!( | ||
state.email.value && | ||
state.username.value && | ||
emailValid && | ||
usernameValid | ||
) | ||
" | ||
@click="resetPassword" | ||
> | ||
Reset Password | ||
</BaseButton> | ||
<BaseButton | ||
class="cancel-button" | ||
data-testid="cancel-button" | ||
@click="cancel" | ||
> | ||
Cancel | ||
</BaseButton> | ||
</div> | ||
</CardBody> | ||
</Card> | ||
</template> | ||
|
||
<style lang="postcss" scoped> | ||
.forgot-password-card { | ||
@apply bg-white w-full max-w-xl; | ||
& .forgot-password-card__header { | ||
@apply flex flex-row space-x-3; | ||
} | ||
& .forgot-password-card__title { | ||
@apply flex flex-1 justify-center px-3 py-2 rounded text-gray-900; | ||
} | ||
& .forgot-password-card__body-wrapper { | ||
@apply flex flex-col space-y-3 pb-3 mb-3; | ||
& .instructions { | ||
@apply px-4 py-2; | ||
} | ||
} | ||
.reset-password-button, | ||
.cancel-button { | ||
@apply flex flex-1 items-center justify-center fill-current bg-gray-100 text-gray-900 hover:bg-gray-200; | ||
} | ||
.reset-password-button:disabled { | ||
@apply bg-gray-300 text-gray-500 cursor-not-allowed; | ||
} | ||
.cancel-button { | ||
@apply bg-transparent border border-gray-300; | ||
} | ||
} | ||
</style> |
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
Oops, something went wrong.