Strip leading @ from password reset input

This commit is contained in:
Alex Gleason 2022-09-16 10:42:05 -05:00
parent 3509fd1c6e
commit c19fe9f167
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
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,
};