Improve registration so the container page can pick what URL it's going to route through to registration.

This commit is contained in:
David Baker 2015-07-15 20:33:12 +01:00
parent 23d9cee299
commit 77114e0081
5 changed files with 66 additions and 35 deletions

View File

@ -24,30 +24,52 @@ var React = require("react");
// maps cannot pass through two stages). // maps cannot pass through two stages).
var MatrixReactSdk = require("../../src/index"); var MatrixReactSdk = require("../../src/index");
// Here, we do some crude URL analysis to allow
// deep-linking. We only support registration
// deep-links in this example.
function routeUrl(location) { function routeUrl(location) {
if (location.hash.indexOf('#/register') == 0) { if (location.hash.indexOf('#/register') == 0) {
var hashparts = location.hash.split('?'); var hashparts = location.hash.split('?');
if (hashparts.length != 2) return;
var pairs = hashparts[1].split('&');
var params = {}; var params = {};
if (hashparts.length == 2) {
var pairs = hashparts[1].split('&');
for (var i = 0; i < pairs.length; ++i) { for (var i = 0; i < pairs.length; ++i) {
var parts = pairs[i].split('='); var parts = pairs[i].split('=');
if (parts.length != 2) continue; if (parts.length != 2) continue;
params[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]); params[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
} }
window.matrixChat.resumeRegistration(params); }
window.matrixChat.showScreen('register', params);
} }
} }
var loaded = false;
window.onload = function() { window.onload = function() {
routeUrl(window.location); routeUrl(window.location);
loaded = true;
} }
// This will be called whenever the SDK changes screens,
// so a web page can update the URL bar appropriately.
var onNewScreen = function(screen) { var onNewScreen = function(screen) {
if (!loaded) return;
window.location.hash = '#/'+screen; window.location.hash = '#/'+screen;
} }
// We use this to work out what URL the SDK should
// pass through when registering to allow the user to
// click back to the client having registered.
// It's up to us to recognise if we're loaded with
// this URL and tell MatrixClient to resume registration.
var makeRegistrationUrl = function() {
return window.location.protocol + '//' +
window.location.host +
window.location.pathname +
'#/register';
}
window.matrixChat = React.render( window.matrixChat = React.render(
<MatrixReactSdk.MatrixChat onNewScreen={onNewScreen} />, <MatrixReactSdk.MatrixChat onNewScreen={onNewScreen} registrationUrl={makeRegistrationUrl()} />,
document.getElementById('matrixchat') document.getElementById('matrixchat')
); );

View File

@ -57,6 +57,7 @@ module.exports = React.createClass({
<Register onLoggedIn={this.onLoggedIn} clientSecret={this.state.register_client_secret} <Register onLoggedIn={this.onLoggedIn} clientSecret={this.state.register_client_secret}
sessionId={this.state.register_session_id} idSid={this.state.register_id_sid} sessionId={this.state.register_session_id} idSid={this.state.register_id_sid}
hsUrl={this.state.register_hs_url} isUrl={this.state.register_is_url} hsUrl={this.state.register_hs_url} isUrl={this.state.register_is_url}
registrationUrl={this.props.registrationUrl}
/> />
); );
} else { } else {

View File

@ -30,7 +30,7 @@ module.exports = {
return { return {
onHsUrlChanged: function() {}, onHsUrlChanged: function() {},
onIsUrlChanged: function() {}, onIsUrlChanged: function() {},
default_hs_url: 'https://matrix.org/', default_hs_url: 'http://localhost:8008',
default_is_url: 'https://matrix.org/' default_is_url: 'https://matrix.org/'
}; };
}, },

View File

@ -81,9 +81,23 @@ module.exports = {
break; break;
case 'start_registration': case 'start_registration':
if (this.state.logged_in) return; if (this.state.logged_in) return;
this.replaceState({ var newState = payload.params || {};
screen: 'register' newState.screen = 'register';
}); if (
payload.params &&
payload.params.client_secret &&
payload.params.session_id &&
payload.params.hs_url &&
payload.params.is_url &&
payload.params.sid
) {
newState.register_client_secret = payload.params.client_secret;
newState.register_session_id = payload.params.session_id;
newState.register_hs_url = payload.params.hs_url;
newState.register_is_url = payload.params.is_url;
newState.register_id_sid = payload.params.sid;
}
this.replaceState(newState);
this.notifyNewScreen('register'); this.notifyNewScreen('register');
break; break;
case 'start_login': case 'start_login':
@ -165,22 +179,18 @@ module.exports = {
dis.dispatch({action: 'focus_composer'}); dis.dispatch({action: 'focus_composer'});
}, },
resumeRegistration(params) { showScreen(screen, params) {
if (!params.hs_url) return false; if (screen == 'register') {
if (!params.is_url) return false; dis.dispatch({
if (!params.client_secret) return false; action: 'start_registration',
if (!params.session_id) return false; params: params
if (!params.sid) return false;
if (this.state.logged_in) return false;
this.setState({
screen: 'register',
register_client_secret: params.client_secret,
register_session_id: params.session_id,
register_hs_url: params.hs_url,
register_is_url: params.is_url,
register_id_sid: params.sid
}); });
} else if (screen == 'login') {
dis.dispatch({
action: 'start_login',
params: params
});
}
}, },
notifyNewScreen: function(screen) { notifyNewScreen: function(screen) {

View File

@ -189,10 +189,8 @@ module.exports = {
this.savedParams.client_secret = cli.generateClientSecret(); this.savedParams.client_secret = cli.generateClientSecret();
this.savedParams.send_attempt = 1; this.savedParams.send_attempt = 1;
var nextLink = window.location.protocol + '//' + var nextLink = this.props.registrationUrl +
window.location.host + '?client_secret=' +
window.location.pathname +
'#/register?client_secret=' +
encodeURIComponent(this.savedParams.client_secret) + encodeURIComponent(this.savedParams.client_secret) +
"&hs_url=" + "&hs_url=" +
encodeURIComponent(this.state.hs_url) + encodeURIComponent(this.state.hs_url) +
@ -254,9 +252,9 @@ module.exports = {
return ( return (
<div> <div>
<form onSubmit={this.onInitialStageSubmit}> <form onSubmit={this.onInitialStageSubmit}>
Email: <input ref="email" /><br /> Email: <input type="text" ref="email" /><br />
Username: <input ref="username" /><br /> Username: <input type="text" ref="username" /><br />
Password: <input ref="password" /><br /> Password: <input type="password" ref="password" /><br />
<ServerConfig ref="serverConfig" /> <ServerConfig ref="serverConfig" />
<input type="submit" value="Continue" /> <input type="submit" value="Continue" />
</form> </form>