From 5c273787575f2fce5d4d4fbcfad0fea8e07ae961 Mon Sep 17 00:00:00 2001 From: zysim <9867871+zysim@users.noreply.github.com> Date: Fri, 17 May 2024 21:31:47 +0800 Subject: [PATCH 1/5] Create composable and test --- composables/api/index.ts | 1 + .../api/useResendAccountConfirmation/index.ts | 25 +++++++++++++++++++ .../useResendAccountConfirmation.test.ts | 20 +++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 composables/api/useResendAccountConfirmation/index.ts create mode 100644 composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts diff --git a/composables/api/index.ts b/composables/api/index.ts index 93143d36..fff76ec6 100644 --- a/composables/api/index.ts +++ b/composables/api/index.ts @@ -7,3 +7,4 @@ export { default as useLogoutUser } from './useLogoutUser' export { default as useRecoverAccount } from './useRecoverAccount' export { default as useRegisterUser } from './useRegisterUser' export { default as useValidateRecoveryToken } from './useValidateRecoveryToken' +export { default as useResendAccountConfirmation } from './useResendAccountConfirmation' diff --git a/composables/api/useResendAccountConfirmation/index.ts b/composables/api/useResendAccountConfirmation/index.ts new file mode 100644 index 00000000..1971b2a8 --- /dev/null +++ b/composables/api/useResendAccountConfirmation/index.ts @@ -0,0 +1,25 @@ +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' +import { Account } from 'lib/api/Account' + +/** + * Resends the account confirmation email for a newly-registered user. + */ +export async function useResendAccountConfirmation( + opts: optionalParameters = {}, +): Promise> { + const { onError, onOkay } = opts + const account = new Account({ + baseUrl: useRuntimeConfig().public.BACKEND_BASE_URL, + }) + + return await useApi(async () => await account.confirmCreate(), { + onError, + onOkay, + }) +} + +export default useResendAccountConfirmation diff --git a/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts b/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts new file mode 100644 index 00000000..4dbcccc3 --- /dev/null +++ b/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts @@ -0,0 +1,20 @@ +import { useResendAccountConfirmation } from '.' + +const mockSuccessResendAccount = vi.fn(() => Promise.resolve({ ok: true })) + +describe('useResendAccountConfirmation', () => { + describe('when everything is successful', () => { + it('changes the password for the user', async () => { + vi.mock('lib/api/Account', () => ({ + Account: function Account() { + this.confirmCreate = mockSuccessResendAccount + }, + })) + + await useResendAccountConfirmation() + + expect(mockSuccessResendAccount).toBeCalledTimes(1) + expect(mockSuccessResendAccount).toBeCalledWith() + }) + }) +}) From d9284aa8118a070de20db2c8296aead547181e8c Mon Sep 17 00:00:00 2001 From: zysim <9867871+zysim@users.noreply.github.com> Date: Fri, 17 May 2024 21:37:17 +0800 Subject: [PATCH 2/5] Annotate type imports in composables/api --- composables/api/useChangePassword/index.ts | 6 +++++- composables/api/useConfirmAccount/index.ts | 6 +++++- composables/api/useGetUserDetail/index.ts | 6 +++++- composables/api/useLoginUser/index.ts | 6 +++++- composables/api/useRegisterUser/index.ts | 6 +++++- composables/api/useValidateRecoveryToken/index.ts | 6 +++++- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/composables/api/useChangePassword/index.ts b/composables/api/useChangePassword/index.ts index cfa2ea89..d1e171f1 100644 --- a/composables/api/useChangePassword/index.ts +++ b/composables/api/useChangePassword/index.ts @@ -1,4 +1,8 @@ -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { Account } from 'lib/api/Account' import type { ChangePasswordRequest } from 'lib/api/data-contracts' diff --git a/composables/api/useConfirmAccount/index.ts b/composables/api/useConfirmAccount/index.ts index eed04ce3..70c27f51 100644 --- a/composables/api/useConfirmAccount/index.ts +++ b/composables/api/useConfirmAccount/index.ts @@ -1,4 +1,8 @@ -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { Account } from 'lib/api/Account' export async function useConfirmAccount( diff --git a/composables/api/useGetUserDetail/index.ts b/composables/api/useGetUserDetail/index.ts index 3e7b2897..ae80d45e 100644 --- a/composables/api/useGetUserDetail/index.ts +++ b/composables/api/useGetUserDetail/index.ts @@ -1,5 +1,9 @@ import { ref } from 'vue' -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { Users } from 'lib/api/Users' import type { UserViewModel } from 'lib/api/data-contracts' diff --git a/composables/api/useLoginUser/index.ts b/composables/api/useLoginUser/index.ts index 8d47bee2..929cdba9 100644 --- a/composables/api/useLoginUser/index.ts +++ b/composables/api/useLoginUser/index.ts @@ -1,4 +1,8 @@ -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { useCurrentUser } from 'composables/useCurrentUser' import { useSessionToken } from 'composables/useSessionToken' import { Account } from 'lib/api/Account' diff --git a/composables/api/useRegisterUser/index.ts b/composables/api/useRegisterUser/index.ts index de9f1b91..b9ac9986 100644 --- a/composables/api/useRegisterUser/index.ts +++ b/composables/api/useRegisterUser/index.ts @@ -1,5 +1,9 @@ import { ref } from 'vue' -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { Account } from 'lib/api/Account' import type { RegisterRequest, UserViewModel } from 'lib/api/data-contracts' diff --git a/composables/api/useValidateRecoveryToken/index.ts b/composables/api/useValidateRecoveryToken/index.ts index 91fd3041..91263eb1 100644 --- a/composables/api/useValidateRecoveryToken/index.ts +++ b/composables/api/useValidateRecoveryToken/index.ts @@ -1,4 +1,8 @@ -import { ApiResponse, optionalParameters, useApi } from 'composables/useApi' +import { + type ApiResponse, + type optionalParameters, + useApi, +} from 'composables/useApi' import { Account } from 'lib/api/Account' export async function useValidateRecoveryToken( From 3a732a2b19950f6ec92d09ac1d5e5ee16223de7d Mon Sep 17 00:00:00 2001 From: zysim <9867871+zysim@users.noreply.github.com> Date: Sat, 18 May 2024 15:49:35 +0800 Subject: [PATCH 3/5] Add token to endpoint call and create component folder --- .../ResendConfirmationBar.vue | 35 +++++++++++++++++++ .../api/useResendAccountConfirmation/index.ts | 24 ++++++++----- .../useResendAccountConfirmation.test.ts | 20 +++++++++-- 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue diff --git a/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue b/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue new file mode 100644 index 00000000..5b751137 --- /dev/null +++ b/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue @@ -0,0 +1,35 @@ + + + diff --git a/composables/api/useResendAccountConfirmation/index.ts b/composables/api/useResendAccountConfirmation/index.ts index 1971b2a8..fdd2d5b9 100644 --- a/composables/api/useResendAccountConfirmation/index.ts +++ b/composables/api/useResendAccountConfirmation/index.ts @@ -1,25 +1,33 @@ import { + useApi, type ApiResponse, type optionalParameters, - useApi, } from 'composables/useApi' +import { useSessionToken } from 'composables/useSessionToken' import { Account } from 'lib/api/Account' /** * Resends the account confirmation email for a newly-registered user. */ -export async function useResendAccountConfirmation( +export default async function useResendAccountConfirmation( opts: optionalParameters = {}, ): Promise> { const { onError, onOkay } = opts + const authToken = useSessionToken() const account = new Account({ baseUrl: useRuntimeConfig().public.BACKEND_BASE_URL, }) - return await useApi(async () => await account.confirmCreate(), { - onError, - onOkay, - }) + return await useApi( + async () => + await account.confirmCreate({ + headers: { + Authorization: `Bearer ${authToken.value}`, + }, + }), + { + onError, + onOkay, + }, + ) } - -export default useResendAccountConfirmation diff --git a/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts b/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts index 4dbcccc3..c91fa4ce 100644 --- a/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts +++ b/composables/api/useResendAccountConfirmation/useResendAccountConfirmation.test.ts @@ -1,20 +1,36 @@ -import { useResendAccountConfirmation } from '.' +import { useLoginUser } from '../useLoginUser' +import useResendAccountConfirmation from '.' +const mockSuccessLogin = vi.fn(() => + Promise.resolve({ data: { token: 'token' }, ok: true }), +) const mockSuccessResendAccount = vi.fn(() => Promise.resolve({ ok: true })) +const onOkaySpy = vi.fn() +const email = 'test@lb.gg' +const password = 'Password1' + +afterEach(() => { + vi.restoreAllMocks() + vi.clearAllMocks() +}) describe('useResendAccountConfirmation', () => { describe('when everything is successful', () => { it('changes the password for the user', async () => { vi.mock('lib/api/Account', () => ({ Account: function Account() { + this.loginCreate = mockSuccessLogin this.confirmCreate = mockSuccessResendAccount }, })) + await useLoginUser({ email, password }, { onOkay: onOkaySpy }) await useResendAccountConfirmation() expect(mockSuccessResendAccount).toBeCalledTimes(1) - expect(mockSuccessResendAccount).toBeCalledWith() + expect(mockSuccessResendAccount).toBeCalledWith({ + headers: { Authorization: 'Bearer token' }, + }) }) }) }) From 6ac676a211d08451ef05a7e94b0e0562a9591f37 Mon Sep 17 00:00:00 2001 From: Edward Runkel Date: Tue, 25 Jun 2024 10:59:54 -0400 Subject: [PATCH 4/5] Style the resend banner --- .../ResendConfirmationBar.vue | 41 ++++++++++++++++--- .../buttons/LogoutButton/LogoutButton.vue | 4 +- i18n/en/index.ts | 3 ++ i18n/language.ts | 5 ++- layouts/default.vue | 2 + 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue b/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue index 5b751137..fecf6893 100644 --- a/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue +++ b/components/blocks/ResendConfirmationBar/ResendConfirmationBar.vue @@ -1,10 +1,9 @@ + + diff --git a/components/elements/buttons/LogoutButton/LogoutButton.vue b/components/elements/buttons/LogoutButton/LogoutButton.vue index b8f66e00..8819318e 100644 --- a/components/elements/buttons/LogoutButton/LogoutButton.vue +++ b/components/elements/buttons/LogoutButton/LogoutButton.vue @@ -3,7 +3,9 @@ import BaseButton from 'elements/buttons/BaseButton/BaseButton.vue'