Skip to content

Commit e2757b3

Browse files
committed
Merge pull request element-hq#4 from matrix-org/registration
Registration
2 parents fe98d6e + 931a4f2 commit e2757b3

11 files changed

Lines changed: 553 additions & 12 deletions

File tree

‎examples/trivial/index.js‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,52 @@ var React = require("react");
2424
// maps cannot pass through two stages).
2525
var MatrixReactSdk = require("../../src/index");
2626

27-
React.render(
28-
<MatrixReactSdk.MatrixChat />,
27+
// Here, we do some crude URL analysis to allow
28+
// deep-linking. We only support registration
29+
// deep-links in this example.
30+
function routeUrl(location) {
31+
if (location.hash.indexOf('#/register') == 0) {
32+
var hashparts = location.hash.split('?');
33+
var params = {};
34+
if (hashparts.length == 2) {
35+
var pairs = hashparts[1].split('&');
36+
for (var i = 0; i < pairs.length; ++i) {
37+
var parts = pairs[i].split('=');
38+
if (parts.length != 2) continue;
39+
params[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
40+
}
41+
}
42+
window.matrixChat.showScreen('register', params);
43+
}
44+
}
45+
46+
var loaded = false;
47+
48+
window.onload = function() {
49+
routeUrl(window.location);
50+
loaded = true;
51+
}
52+
53+
// This will be called whenever the SDK changes screens,
54+
// so a web page can update the URL bar appropriately.
55+
var onNewScreen = function(screen) {
56+
if (!loaded) return;
57+
window.location.hash = '#/'+screen;
58+
}
59+
60+
// We use this to work out what URL the SDK should
61+
// pass through when registering to allow the user to
62+
// click back to the client having registered.
63+
// It's up to us to recognise if we're loaded with
64+
// this URL and tell MatrixClient to resume registration.
65+
var makeRegistrationUrl = function() {
66+
return window.location.protocol + '//' +
67+
window.location.host +
68+
window.location.pathname +
69+
'#/register';
70+
}
71+
72+
window.matrixChat = React.render(
73+
<MatrixReactSdk.MatrixChat onNewScreen={onNewScreen} registrationUrl={makeRegistrationUrl()} />,
2974
document.getElementById('matrixchat')
3075
);

‎skins/base/views/organisms/RoomView.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ module.exports = React.createClass({
3838
mixins: [RoomViewController],
3939

4040
render: function() {
41+
if (!this.state.room) {
42+
return (
43+
<div />
44+
);
45+
}
46+
4147
var myUserId = MatrixClientPeg.get().credentials.userId;
4248
if (this.state.room.currentState.members[myUserId].membership == 'invite') {
4349
if (this.state.joining) {

‎skins/base/views/pages/MatrixChat.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var RoomList = ComponentBroker.get('organisms/RoomList');
2323
var RoomView = ComponentBroker.get('organisms/RoomView');
2424
var MatrixToolbar = ComponentBroker.get('molecules/MatrixToolbar');
2525
var Login = ComponentBroker.get('templates/Login');
26+
var Register = ComponentBroker.get('templates/Register');
2627

2728
var MatrixChatController = require("../../../../src/controllers/pages/MatrixChat");
2829

@@ -51,6 +52,14 @@ module.exports = React.createClass({
5152
return (
5253
<Loader />
5354
);
55+
} else if (this.state.screen == 'register') {
56+
return (
57+
<Register onLoggedIn={this.onLoggedIn} clientSecret={this.state.register_client_secret}
58+
sessionId={this.state.register_session_id} idSid={this.state.register_id_sid}
59+
hsUrl={this.state.register_hs_url} isUrl={this.state.register_is_url}
60+
registrationUrl={this.props.registrationUrl}
61+
/>
62+
);
5463
} else {
5564
return (
5665
<Login onLoggedIn={this.onLoggedIn} />

‎skins/base/views/templates/Login.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = React.createClass({
4040
<h1>Please log in:</h1>
4141
{this.componentForStep(this.state.step)}
4242
<div className="error">{this.state.errorText}</div>
43+
<a onClick={this.showRegister} href="#">Create a new account</a>
4344
</div>
4445
);
4546
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var ComponentBroker = require("../../../../src/ComponentBroker");
22+
23+
var Loader = require("react-loader");
24+
25+
var RegisterController = require("../../../../src/controllers/templates/Register");
26+
27+
module.exports = React.createClass({
28+
displayName: 'Register',
29+
mixins: [RegisterController],
30+
31+
registerContent: function() {
32+
if (this.state.busy) {
33+
return (
34+
<Loader />
35+
);
36+
} else {
37+
return (
38+
<div>
39+
<h1>Create a new account:</h1>
40+
{this.componentForStep(this.state.step)}
41+
<div className="error">{this.state.errorText}</div>
42+
<a onClick={this.showLogin} href="#">Sign in with existing account</a>
43+
</div>
44+
);
45+
}
46+
},
47+
48+
render: function() {
49+
return (
50+
<div className="mx_Register">
51+
{this.registerContent()}
52+
</div>
53+
);
54+
}
55+
});

‎src/ComponentBroker.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ require('../skins/base/views/molecules/MemberTile');
8585
require('../skins/base/views/organisms/RoomList');
8686
require('../skins/base/views/organisms/RoomView');
8787
require('../skins/base/views/templates/Login');
88+
require('../skins/base/views/templates/Register');
8889
require('../skins/base/views/organisms/Notifier');
8990
require('../skins/base/views/organisms/CreateRoom');
9091
require('../skins/base/views/molecules/UserSelector');

‎src/MatrixClientPeg.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ var matrixClient = null;
2424
var localStorage = window.localStorage;
2525
if (localStorage) {
2626
var hs_url = localStorage.getItem("mx_hs_url");
27+
var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
2728
var access_token = localStorage.getItem("mx_access_token");
2829
var user_id = localStorage.getItem("mx_user_id");
2930
if (access_token && user_id && hs_url) {
3031
matrixClient = Matrix.createClient({
3132
baseUrl: hs_url,
33+
idBaseUrl: is_url,
3234
accessToken: access_token,
3335
userId: user_id
3436
});
@@ -44,8 +46,11 @@ module.exports = {
4446
matrixClient = cli;
4547
},
4648

47-
replaceUsingUrl: function(hs_url) {
48-
matrixClient = Matrix.createClient(hs_url);
49+
replaceUsingUrls: function(hs_url, is_url) {
50+
matrixClient = Matrix.createClient({
51+
baseUrl: hs_url,
52+
idBaseUrl: is_url
53+
});
4954
}
5055
};
5156

‎src/controllers/organisms/RoomView.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var tileTypes = {
3636
module.exports = {
3737
getInitialState: function() {
3838
return {
39-
room: MatrixClientPeg.get().getRoom(this.props.roomId),
39+
room: this.props.roomId ? MatrixClientPeg.get().getRoom(this.props.roomId) : null,
4040
messageCap: INITIAL_SIZE
4141
}
4242
},

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ module.exports = {
4444
this.focusComposer = false;
4545
document.addEventListener("keydown", this.onKeyDown);
4646
window.addEventListener("focus", this.onFocus);
47+
if (this.state.logged_in) {
48+
this.notifyNewScreen('');
49+
} else {
50+
this.notifyNewScreen('login');
51+
}
4752
},
4853

4954
componentWillUnmount: function() {
@@ -63,14 +68,45 @@ module.exports = {
6368

6469
switch (payload.action) {
6570
case 'logout':
66-
this.setState({
71+
this.replaceState({
6772
logged_in: false,
6873
ready: false
6974
});
75+
localStorage.removeItem("mx_hs_url");
76+
localStorage.removeItem("mx_user_id");
77+
localStorage.removeItem("mx_access_token");
7078
Notifier.stop();
7179
MatrixClientPeg.get().removeAllListeners();
7280
MatrixClientPeg.replace(null);
7381
break;
82+
case 'start_registration':
83+
if (this.state.logged_in) return;
84+
var newState = payload.params || {};
85+
newState.screen = 'register';
86+
if (
87+
payload.params &&
88+
payload.params.client_secret &&
89+
payload.params.session_id &&
90+
payload.params.hs_url &&
91+
payload.params.is_url &&
92+
payload.params.sid
93+
) {
94+
newState.register_client_secret = payload.params.client_secret;
95+
newState.register_session_id = payload.params.session_id;
96+
newState.register_hs_url = payload.params.hs_url;
97+
newState.register_is_url = payload.params.is_url;
98+
newState.register_id_sid = payload.params.sid;
99+
}
100+
this.replaceState(newState);
101+
this.notifyNewScreen('register');
102+
break;
103+
case 'start_login':
104+
if (this.state.logged_in) return;
105+
this.replaceState({
106+
screen: 'login'
107+
});
108+
this.notifyNewScreen('login');
109+
break;
74110
case 'view_room':
75111
this.focusComposer = true;
76112
this.setState({
@@ -99,8 +135,12 @@ module.exports = {
99135
},
100136

101137
onLoggedIn: function() {
102-
this.setState({logged_in: true});
138+
this.setState({
139+
screen: undefined,
140+
logged_in: true
141+
});
103142
this.startMatrixClient();
143+
this.notifyNewScreen('');
104144
},
105145

106146
startMatrixClient: function() {
@@ -137,6 +177,26 @@ module.exports = {
137177

138178
onFocus: function(ev) {
139179
dis.dispatch({action: 'focus_composer'});
180+
},
181+
182+
showScreen(screen, params) {
183+
if (screen == 'register') {
184+
dis.dispatch({
185+
action: 'start_registration',
186+
params: params
187+
});
188+
} else if (screen == 'login') {
189+
dis.dispatch({
190+
action: 'start_login',
191+
params: params
192+
});
193+
}
194+
},
195+
196+
notifyNewScreen: function(screen) {
197+
if (this.props.onNewScreen) {
198+
this.props.onNewScreen(screen);
199+
}
140200
}
141201
};
142202

‎src/controllers/templates/Login.js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var React = require('react');
2020

2121
var MatrixClientPeg = require("../../MatrixClientPeg");
2222
var Matrix = require("matrix-js-sdk");
23+
var dis = require("../../dispatcher");
2324

2425
var ComponentBroker = require("../../ComponentBroker");
2526

@@ -41,8 +42,14 @@ module.exports = {
4142

4243
onHSChosen: function(ev) {
4344
ev.preventDefault();
44-
MatrixClientPeg.replaceUsingUrl(this.refs.serverConfig.getHsUrl());
45-
this.setState({hs_url: this.refs.serverConfig.getHsUrl()});
45+
MatrixClientPeg.replaceUsingUrls(
46+
this.refs.serverConfig.getHsUrl(),
47+
this.refs.serverConfig.getIsUrl()
48+
);
49+
this.setState({
50+
hs_url: this.refs.serverConfig.getHsUrl(),
51+
is_url: this.refs.serverConfig.getIsUrl()
52+
});
4653
this.setStep("fetch_stages");
4754
var cli = MatrixClientPeg.get();
4855
this.setState({busy: true});
@@ -71,12 +78,14 @@ module.exports = {
7178
// XXX: we assume this means we're logged in, but there could be a next stage
7279
MatrixClientPeg.replace(Matrix.createClient({
7380
baseUrl: that.state.hs_url,
81+
idBaseUrl: that.state.is_url,
7482
userId: data.user_id,
7583
accessToken: data.access_token
7684
}));
7785
var localStorage = window.localStorage;
7886
if (localStorage) {
7987
localStorage.setItem("mx_hs_url", that.state.hs_url);
88+
localStorage.setItem("mx_is_url", that.state.is_url);
8089
localStorage.setItem("mx_user_id", data.user_id);
8190
localStorage.setItem("mx_access_token", data.access_token);
8291
} else {
@@ -85,9 +94,6 @@ module.exports = {
8594
if (that.props.onLoggedIn) {
8695
that.props.onLoggedIn();
8796
}
88-
/*dis.dispatch({
89-
'action': 'logged_in'
90-
});*/
9197
}, function(error) {
9298
that.setStep("stage_m.login.password");
9399
that.setState({errorText: 'Login failed.'});
@@ -118,4 +124,11 @@ module.exports = {
118124
);
119125
}
120126
},
127+
128+
showRegister: function(ev) {
129+
ev.preventDefault();
130+
dis.dispatch({
131+
action: 'start_registration'
132+
});
133+
}
121134
};

0 commit comments

Comments
 (0)