From c19fe9f1672d3f7021b54298794f1e5fd9786cff Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 16 Sep 2022 10:42:05 -0500 Subject: [PATCH] Strip leading @ from password reset input --- app/soapbox/actions/auth.ts | 11 +---------- app/soapbox/actions/security.ts | 8 +++++--- app/soapbox/utils/__tests__/input.test.ts | 7 +++++++ app/soapbox/utils/input.ts | 13 +++++++++++++ 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 app/soapbox/utils/__tests__/input.test.ts create mode 100644 app/soapbox/utils/input.ts diff --git a/app/soapbox/actions/auth.ts b/app/soapbox/actions/auth.ts index d3252e7fb0..5a686d0457 100644 --- a/app/soapbox/actions/auth.ts +++ b/app/soapbox/actions/auth.ts @@ -20,6 +20,7 @@ import KVStore from 'soapbox/storage/kv_store'; import { getLoggedInAccount, parseBaseURL } from 'soapbox/utils/auth'; import sourceCode from 'soapbox/utils/code'; import { getFeatures } from 'soapbox/utils/features'; +import { normalizeUsername } from 'soapbox/utils/input'; import { isStandalone } from 'soapbox/utils/state'; import api, { baseClient } from '../api'; @@ -207,16 +208,6 @@ export const loadCredentials = (token: string, accountUrl: string) => }) .catch(() => dispatch(verifyCredentials(token, accountUrl))); -/** Trim the username and strip the leading @. */ -const normalizeUsername = (username: string): string => { - const trimmed = username.trim(); - if (trimmed[0] === '@') { - return trimmed.slice(1); - } else { - return trimmed; - } -}; - export const logIn = (username: string, password: string) => (dispatch: AppDispatch) => dispatch(getAuthApp()).then(() => { return dispatch(createUserToken(normalizeUsername(username), password)); diff --git a/app/soapbox/actions/security.ts b/app/soapbox/actions/security.ts index 430691a061..196e54dcbf 100644 --- a/app/soapbox/actions/security.ts +++ b/app/soapbox/actions/security.ts @@ -7,6 +7,7 @@ import snackbar from 'soapbox/actions/snackbar'; import { getLoggedInAccount } from 'soapbox/utils/auth'; import { parseVersion, TRUTHSOCIAL } from 'soapbox/utils/features'; +import { normalizeUsername } from 'soapbox/utils/input'; import api from '../api'; @@ -84,15 +85,16 @@ const changePassword = (oldPassword: string, newPassword: string, confirmation: const resetPassword = (usernameOrEmail: string) => (dispatch: AppDispatch, getState: () => RootState) => { + const input = normalizeUsername(usernameOrEmail); const state = getState(); const v = parseVersion(state.instance.version); dispatch({ type: RESET_PASSWORD_REQUEST }); const params = - usernameOrEmail.includes('@') - ? { email: usernameOrEmail } - : { nickname: usernameOrEmail, username: usernameOrEmail }; + input.includes('@') + ? { email: input } + : { nickname: input, username: input }; const endpoint = v.software === TRUTHSOCIAL diff --git a/app/soapbox/utils/__tests__/input.test.ts b/app/soapbox/utils/__tests__/input.test.ts new file mode 100644 index 0000000000..819a8fd828 --- /dev/null +++ b/app/soapbox/utils/__tests__/input.test.ts @@ -0,0 +1,7 @@ +import { normalizeUsername } from '../input'; + +test('normalizeUsername', () => { + expect(normalizeUsername('@alex')).toBe('alex'); + expect(normalizeUsername('alex@alexgleason.me')).toBe('alex@alexgleason.me'); + expect(normalizeUsername('@alex@gleasonator.com')).toBe('alex@gleasonator.com'); +}); \ No newline at end of file diff --git a/app/soapbox/utils/input.ts b/app/soapbox/utils/input.ts new file mode 100644 index 0000000000..e9d8c2d851 --- /dev/null +++ b/app/soapbox/utils/input.ts @@ -0,0 +1,13 @@ +/** Trim the username and strip the leading @. */ +const normalizeUsername = (username: string): string => { + const trimmed = username.trim(); + if (trimmed[0] === '@') { + return trimmed.slice(1); + } else { + return trimmed; + } +}; + +export { + normalizeUsername, +}; \ No newline at end of file