From d1d96308a1ac0b68de0cfc8685d1be41d9360bc9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 8 Sep 2021 11:31:26 -0500 Subject: [PATCH] Mastodon: redirect password reset to backend --- app/soapbox/build_config.js | 4 +-- .../auth_login/components/login_form.js | 28 +++++++++++++++---- app/soapbox/utils/features.js | 1 + app/soapbox/utils/state.js | 13 +++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/soapbox/build_config.js b/app/soapbox/build_config.js index 1d057163f..7e0419619 100644 --- a/app/soapbox/build_config.js +++ b/app/soapbox/build_config.js @@ -4,7 +4,7 @@ * @module soapbox/build_config */ -const { trim } = require('lodash'); +const { trim, trimEnd } = require('lodash'); const { NODE_ENV, @@ -15,7 +15,7 @@ const { const sanitizeURL = url => { try { - return new URL(url).toString(); + return trimEnd(new URL(url).toString(), '/'); } catch { return ''; } diff --git a/app/soapbox/features/auth_login/components/login_form.js b/app/soapbox/features/auth_login/components/login_form.js index d9933f6d0..1465cd687 100644 --- a/app/soapbox/features/auth_login/components/login_form.js +++ b/app/soapbox/features/auth_login/components/login_form.js @@ -3,18 +3,30 @@ import { connect } from 'react-redux'; import { injectIntl, FormattedMessage, defineMessages } from 'react-intl'; import { Link } from 'react-router-dom'; import ImmutablePureComponent from 'react-immutable-pure-component'; +import { getFeatures } from 'soapbox/utils/features'; +import { getBaseURL } from 'soapbox/utils/state'; const messages = defineMessages({ username: { id: 'login.fields.username_placeholder', defaultMessage: 'Username' }, password: { id: 'login.fields.password_placeholder', defaultMessage: 'Password' }, }); -export default @connect() +const mapStateToProps = state => { + const instance = state.get('instance'); + const features = getFeatures(instance); + + return { + baseURL: getBaseURL(state), + hasResetPasswordAPI: features.resetPasswordAPI, + }; +}; + +export default @connect(mapStateToProps) @injectIntl class LoginForm extends ImmutablePureComponent { render() { - const { intl, isLoading, handleSubmit } = this.props; + const { intl, isLoading, handleSubmit, baseURL, hasResetPasswordAPI } = this.props; return (
@@ -43,9 +55,15 @@ class LoginForm extends ImmutablePureComponent { />

- - - + {hasResetPasswordAPI ? ( + + + + ) : ( + + + + )}

diff --git a/app/soapbox/utils/features.js b/app/soapbox/utils/features.js index 331fa10b2..518f3e669 100644 --- a/app/soapbox/utils/features.js +++ b/app/soapbox/utils/features.js @@ -24,6 +24,7 @@ export const getFeatures = createSelector([ securityAPI: v.software === 'Pleroma', settingsStore: v.software === 'Pleroma', accountAliasesAPI: v.software === 'Pleroma', + resetPasswordAPI: v.software === 'Pleroma', }; }); diff --git a/app/soapbox/utils/state.js b/app/soapbox/utils/state.js index 1f855f20f..8cd6db20d 100644 --- a/app/soapbox/utils/state.js +++ b/app/soapbox/utils/state.js @@ -6,6 +6,7 @@ import { getSoapboxConfig } from'soapbox/actions/soapbox'; import { isPrerendered } from 'soapbox/precheck'; import { isURL } from 'soapbox/utils/auth'; +import { getBaseURL as getAccountBaseURL } from 'soapbox/utils/accounts'; import { BACKEND_URL } from 'soapbox/build_config'; export const displayFqn = state => { @@ -27,3 +28,15 @@ export const isStandalone = state => { const instanceFetchFailed = state.getIn(['meta', 'instance_fetch_failed'], false); return isURL(BACKEND_URL) ? false : (!isPrerendered && instanceFetchFailed); }; + +/** + * Get the baseURL of the instance. + * @param {object} state + * @returns {string} url + */ +export const getBaseURL = state => { + const me = state.get('me'); + const account = state.getIn(['accounts', me]); + + return isURL(BACKEND_URL) ? BACKEND_URL : getAccountBaseURL(account); +};