Bring server config juggling into MatrixChat
This way the server config is consistent across login, password reset, and registration. This also brings the code into a more generic place for all 3 duplicated efforts.
This commit is contained in:
parent
0b1a0c77b7
commit
1f527e71b1
1 changed files with 17 additions and 138 deletions
|
@ -50,8 +50,7 @@ import SettingsStore, {SettingLevel} from "../../settings/SettingsStore";
|
||||||
import { startAnyRegistrationFlow } from "../../Registration.js";
|
import { startAnyRegistrationFlow } from "../../Registration.js";
|
||||||
import { messageForSyncError } from '../../utils/ErrorUtils';
|
import { messageForSyncError } from '../../utils/ErrorUtils';
|
||||||
import ResizeNotifier from "../../utils/ResizeNotifier";
|
import ResizeNotifier from "../../utils/ResizeNotifier";
|
||||||
|
import {ValidatedDiscoveryConfig} from "../../utils/AutoDiscoveryUtils";
|
||||||
const AutoDiscovery = Matrix.AutoDiscovery;
|
|
||||||
|
|
||||||
// Disable warnings for now: we use deprecated bluebird functions
|
// Disable warnings for now: we use deprecated bluebird functions
|
||||||
// and need to migrate, but they spam the console with warnings.
|
// and need to migrate, but they spam the console with warnings.
|
||||||
|
@ -181,16 +180,8 @@ export default React.createClass({
|
||||||
// Parameters used in the registration dance with the IS
|
// Parameters used in the registration dance with the IS
|
||||||
register_client_secret: null,
|
register_client_secret: null,
|
||||||
register_session_id: null,
|
register_session_id: null,
|
||||||
register_hs_url: null,
|
|
||||||
register_is_url: null,
|
|
||||||
register_id_sid: null,
|
register_id_sid: null,
|
||||||
|
|
||||||
// Parameters used for setting up the authentication views
|
|
||||||
defaultServerName: this.props.config.default_server_name,
|
|
||||||
defaultHsUrl: this.props.config.default_hs_url,
|
|
||||||
defaultIsUrl: this.props.config.default_is_url,
|
|
||||||
defaultServerDiscoveryError: null,
|
|
||||||
|
|
||||||
// When showing Modal dialogs we need to set aria-hidden on the root app element
|
// When showing Modal dialogs we need to set aria-hidden on the root app element
|
||||||
// and disable it when there are no dialogs
|
// and disable it when there are no dialogs
|
||||||
hideToSRUsers: false,
|
hideToSRUsers: false,
|
||||||
|
@ -211,42 +202,15 @@ export default React.createClass({
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
getDefaultServerName: function() {
|
// TODO: TravisR - Remove this or put it somewhere else
|
||||||
return this.state.defaultServerName;
|
|
||||||
},
|
|
||||||
|
|
||||||
getCurrentHsUrl: function() {
|
|
||||||
if (this.state.register_hs_url) {
|
|
||||||
return this.state.register_hs_url;
|
|
||||||
} else if (MatrixClientPeg.get()) {
|
|
||||||
return MatrixClientPeg.get().getHomeserverUrl();
|
|
||||||
} else {
|
|
||||||
return this.getDefaultHsUrl();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getDefaultHsUrl(defaultToMatrixDotOrg) {
|
|
||||||
defaultToMatrixDotOrg = typeof(defaultToMatrixDotOrg) !== 'boolean' ? true : defaultToMatrixDotOrg;
|
|
||||||
if (!this.state.defaultHsUrl && defaultToMatrixDotOrg) return "https://matrix.org";
|
|
||||||
return this.state.defaultHsUrl;
|
|
||||||
},
|
|
||||||
|
|
||||||
getFallbackHsUrl: function() {
|
getFallbackHsUrl: function() {
|
||||||
return this.props.config.fallback_hs_url;
|
return this.props.config.fallback_hs_url;
|
||||||
},
|
},
|
||||||
|
|
||||||
getCurrentIsUrl: function() {
|
getServerProperties() {
|
||||||
if (this.state.register_is_url) {
|
let props = this.state.serverConfig;
|
||||||
return this.state.register_is_url;
|
if (!props) props = SdkConfig.get()["validated_server_config"];
|
||||||
} else if (MatrixClientPeg.get()) {
|
return {serverConfig: props};
|
||||||
return MatrixClientPeg.get().getIdentityServerUrl();
|
|
||||||
} else {
|
|
||||||
return this.getDefaultIsUrl();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getDefaultIsUrl() {
|
|
||||||
return this.state.defaultIsUrl || "https://vector.im";
|
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillMount: function() {
|
componentWillMount: function() {
|
||||||
|
@ -260,40 +224,6 @@ export default React.createClass({
|
||||||
MatrixClientPeg.opts.initialSyncLimit = this.props.config.sync_timeline_limit;
|
MatrixClientPeg.opts.initialSyncLimit = this.props.config.sync_timeline_limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the default URLs (async)
|
|
||||||
if (this.getDefaultServerName() && !this.getDefaultHsUrl(false)) {
|
|
||||||
this.setState({loadingDefaultHomeserver: true});
|
|
||||||
this._tryDiscoverDefaultHomeserver(this.getDefaultServerName());
|
|
||||||
} else if (this.getDefaultServerName() && this.getDefaultHsUrl(false)) {
|
|
||||||
// Ideally we would somehow only communicate this to the server admins, but
|
|
||||||
// given this is at login time we can't really do much besides hope that people
|
|
||||||
// will check their settings.
|
|
||||||
this.setState({
|
|
||||||
defaultServerName: null, // To un-hide any secrets people might be keeping
|
|
||||||
defaultServerDiscoveryError: _t(
|
|
||||||
"Invalid configuration: Cannot supply a default homeserver URL and " +
|
|
||||||
"a default server name",
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a default HS with query param `hs_url`
|
|
||||||
const paramHs = this.props.startingFragmentQueryParams.hs_url;
|
|
||||||
if (paramHs) {
|
|
||||||
console.log('Setting register_hs_url ', paramHs);
|
|
||||||
this.setState({
|
|
||||||
register_hs_url: paramHs,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Set a default IS with query param `is_url`
|
|
||||||
const paramIs = this.props.startingFragmentQueryParams.is_url;
|
|
||||||
if (paramIs) {
|
|
||||||
console.log('Setting register_is_url ', paramIs);
|
|
||||||
this.setState({
|
|
||||||
register_is_url: paramIs,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// a thing to call showScreen with once login completes. this is kept
|
// a thing to call showScreen with once login completes. this is kept
|
||||||
// outside this.state because updating it should never trigger a
|
// outside this.state because updating it should never trigger a
|
||||||
// rerender.
|
// rerender.
|
||||||
|
@ -374,8 +304,8 @@ export default React.createClass({
|
||||||
return Lifecycle.loadSession({
|
return Lifecycle.loadSession({
|
||||||
fragmentQueryParams: this.props.startingFragmentQueryParams,
|
fragmentQueryParams: this.props.startingFragmentQueryParams,
|
||||||
enableGuest: this.props.enableGuest,
|
enableGuest: this.props.enableGuest,
|
||||||
guestHsUrl: this.getCurrentHsUrl(),
|
guestHsUrl: this.getServerProperties().serverConfig.hsUrl,
|
||||||
guestIsUrl: this.getCurrentIsUrl(),
|
guestIsUrl: this.getServerProperties().serverConfig.isUrl,
|
||||||
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
|
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
|
||||||
});
|
});
|
||||||
}).then((loadedSession) => {
|
}).then((loadedSession) => {
|
||||||
|
@ -1823,44 +1753,7 @@ export default React.createClass({
|
||||||
},
|
},
|
||||||
|
|
||||||
onServerConfigChange(config) {
|
onServerConfigChange(config) {
|
||||||
const newState = {};
|
this.setState({serverConfig: config});
|
||||||
if (config.hsUrl) {
|
|
||||||
newState.register_hs_url = config.hsUrl;
|
|
||||||
}
|
|
||||||
if (config.isUrl) {
|
|
||||||
newState.register_is_url = config.isUrl;
|
|
||||||
}
|
|
||||||
this.setState(newState);
|
|
||||||
},
|
|
||||||
|
|
||||||
_tryDiscoverDefaultHomeserver: async function(serverName) {
|
|
||||||
try {
|
|
||||||
const discovery = await AutoDiscovery.findClientConfig(serverName);
|
|
||||||
const state = discovery["m.homeserver"].state;
|
|
||||||
if (state !== AutoDiscovery.SUCCESS) {
|
|
||||||
console.error("Failed to discover homeserver on startup:", discovery);
|
|
||||||
this.setState({
|
|
||||||
defaultServerDiscoveryError: discovery["m.homeserver"].error,
|
|
||||||
loadingDefaultHomeserver: false,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const hsUrl = discovery["m.homeserver"].base_url;
|
|
||||||
const isUrl = discovery["m.identity_server"].state === AutoDiscovery.SUCCESS
|
|
||||||
? discovery["m.identity_server"].base_url
|
|
||||||
: "https://vector.im";
|
|
||||||
this.setState({
|
|
||||||
defaultHsUrl: hsUrl,
|
|
||||||
defaultIsUrl: isUrl,
|
|
||||||
loadingDefaultHomeserver: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
this.setState({
|
|
||||||
defaultServerDiscoveryError: _t("Unknown error discovering homeserver"),
|
|
||||||
loadingDefaultHomeserver: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_makeRegistrationUrl: function(params) {
|
_makeRegistrationUrl: function(params) {
|
||||||
|
@ -1879,8 +1772,7 @@ export default React.createClass({
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.state.view === VIEWS.LOADING ||
|
this.state.view === VIEWS.LOADING ||
|
||||||
this.state.view === VIEWS.LOGGING_IN ||
|
this.state.view === VIEWS.LOGGING_IN
|
||||||
this.state.loadingDefaultHomeserver
|
|
||||||
) {
|
) {
|
||||||
const Spinner = sdk.getComponent('elements.Spinner');
|
const Spinner = sdk.getComponent('elements.Spinner');
|
||||||
return (
|
return (
|
||||||
|
@ -1958,18 +1850,13 @@ export default React.createClass({
|
||||||
sessionId={this.state.register_session_id}
|
sessionId={this.state.register_session_id}
|
||||||
idSid={this.state.register_id_sid}
|
idSid={this.state.register_id_sid}
|
||||||
email={this.props.startingFragmentQueryParams.email}
|
email={this.props.startingFragmentQueryParams.email}
|
||||||
defaultServerName={this.getDefaultServerName()}
|
|
||||||
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
|
|
||||||
defaultHsUrl={this.getDefaultHsUrl()}
|
|
||||||
defaultIsUrl={this.getDefaultIsUrl()}
|
|
||||||
brand={this.props.config.brand}
|
brand={this.props.config.brand}
|
||||||
customHsUrl={this.getCurrentHsUrl()}
|
|
||||||
customIsUrl={this.getCurrentIsUrl()}
|
|
||||||
makeRegistrationUrl={this._makeRegistrationUrl}
|
makeRegistrationUrl={this._makeRegistrationUrl}
|
||||||
onLoggedIn={this.onRegistered}
|
onLoggedIn={this.onRegistered}
|
||||||
onLoginClick={this.onLoginClick}
|
onLoginClick={this.onLoginClick}
|
||||||
onServerConfigChange={this.onServerConfigChange}
|
onServerConfigChange={this.onServerConfigChange}
|
||||||
/>
|
{...this.getServerProperties()}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1978,14 +1865,11 @@ export default React.createClass({
|
||||||
const ForgotPassword = sdk.getComponent('structures.auth.ForgotPassword');
|
const ForgotPassword = sdk.getComponent('structures.auth.ForgotPassword');
|
||||||
return (
|
return (
|
||||||
<ForgotPassword
|
<ForgotPassword
|
||||||
defaultServerName={this.getDefaultServerName()}
|
|
||||||
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
|
|
||||||
defaultHsUrl={this.getDefaultHsUrl()}
|
|
||||||
defaultIsUrl={this.getDefaultIsUrl()}
|
|
||||||
customHsUrl={this.getCurrentHsUrl()}
|
|
||||||
customIsUrl={this.getCurrentIsUrl()}
|
|
||||||
onComplete={this.onLoginClick}
|
onComplete={this.onLoginClick}
|
||||||
onLoginClick={this.onLoginClick} />
|
onLoginClick={this.onLoginClick}
|
||||||
|
onServerConfigChange={this.onServerConfigChange}
|
||||||
|
{...this.getServerProperties()}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1995,16 +1879,11 @@ export default React.createClass({
|
||||||
<Login
|
<Login
|
||||||
onLoggedIn={Lifecycle.setLoggedIn}
|
onLoggedIn={Lifecycle.setLoggedIn}
|
||||||
onRegisterClick={this.onRegisterClick}
|
onRegisterClick={this.onRegisterClick}
|
||||||
defaultServerName={this.getDefaultServerName()}
|
|
||||||
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
|
|
||||||
defaultHsUrl={this.getDefaultHsUrl()}
|
|
||||||
defaultIsUrl={this.getDefaultIsUrl()}
|
|
||||||
customHsUrl={this.getCurrentHsUrl()}
|
|
||||||
customIsUrl={this.getCurrentIsUrl()}
|
|
||||||
fallbackHsUrl={this.getFallbackHsUrl()}
|
fallbackHsUrl={this.getFallbackHsUrl()}
|
||||||
defaultDeviceDisplayName={this.props.defaultDeviceDisplayName}
|
defaultDeviceDisplayName={this.props.defaultDeviceDisplayName}
|
||||||
onForgotPasswordClick={this.onForgotPasswordClick}
|
onForgotPasswordClick={this.onForgotPasswordClick}
|
||||||
onServerConfigChange={this.onServerConfigChange}
|
onServerConfigChange={this.onServerConfigChange}
|
||||||
|
{...this.getServerProperties()}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue