Merge branch 'pw-reset-at' into 'develop'

Strip leading @ from password reset input

See merge request soapbox-pub/soapbox!1793
This commit is contained in:
Alex Gleason 2022-09-16 16:23:26 +00:00
commit 8e021d4e2c
4 changed files with 26 additions and 13 deletions

View File

@ -20,6 +20,7 @@ import KVStore from 'soapbox/storage/kv_store';
import { getLoggedInAccount, parseBaseURL } from 'soapbox/utils/auth'; import { getLoggedInAccount, parseBaseURL } from 'soapbox/utils/auth';
import sourceCode from 'soapbox/utils/code'; import sourceCode from 'soapbox/utils/code';
import { getFeatures } from 'soapbox/utils/features'; import { getFeatures } from 'soapbox/utils/features';
import { normalizeUsername } from 'soapbox/utils/input';
import { isStandalone } from 'soapbox/utils/state'; import { isStandalone } from 'soapbox/utils/state';
import api, { baseClient } from '../api'; import api, { baseClient } from '../api';
@ -207,16 +208,6 @@ export const loadCredentials = (token: string, accountUrl: string) =>
}) })
.catch(() => dispatch(verifyCredentials(token, accountUrl))); .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) => export const logIn = (username: string, password: string) =>
(dispatch: AppDispatch) => dispatch(getAuthApp()).then(() => { (dispatch: AppDispatch) => dispatch(getAuthApp()).then(() => {
return dispatch(createUserToken(normalizeUsername(username), password)); return dispatch(createUserToken(normalizeUsername(username), password));

View File

@ -7,6 +7,7 @@
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { getLoggedInAccount } from 'soapbox/utils/auth'; import { getLoggedInAccount } from 'soapbox/utils/auth';
import { parseVersion, TRUTHSOCIAL } from 'soapbox/utils/features'; import { parseVersion, TRUTHSOCIAL } from 'soapbox/utils/features';
import { normalizeUsername } from 'soapbox/utils/input';
import api from '../api'; import api from '../api';
@ -84,15 +85,16 @@ const changePassword = (oldPassword: string, newPassword: string, confirmation:
const resetPassword = (usernameOrEmail: string) => const resetPassword = (usernameOrEmail: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const input = normalizeUsername(usernameOrEmail);
const state = getState(); const state = getState();
const v = parseVersion(state.instance.version); const v = parseVersion(state.instance.version);
dispatch({ type: RESET_PASSWORD_REQUEST }); dispatch({ type: RESET_PASSWORD_REQUEST });
const params = const params =
usernameOrEmail.includes('@') input.includes('@')
? { email: usernameOrEmail } ? { email: input }
: { nickname: usernameOrEmail, username: usernameOrEmail }; : { nickname: input, username: input };
const endpoint = const endpoint =
v.software === TRUTHSOCIAL v.software === TRUTHSOCIAL

View File

@ -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');
});

View File

@ -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,
};