@@ -78,27 +78,29 @@ interface IProps {
78
78
}
79
79
80
80
interface IState {
81
+ // true if we're waiting for the user to complete
81
82
busy : boolean ;
82
83
errorText ?: ReactNode ;
83
- // true if we're waiting for the user to complete
84
84
// We remember the values entered by the user because
85
85
// the registration form will be unmounted during the
86
86
// course of registration, but if there's an error we
87
87
// want to bring back the registration form with the
88
88
// values the user entered still in it. We can keep
89
89
// them in this component's state since this component
90
90
// persist for the duration of the registration process.
91
- formVals : Record < string , string > ;
91
+ formVals : Record < string , string | undefined > ;
92
92
// user-interactive auth
93
93
// If we've been given a session ID, we're resuming
94
94
// straight back into UI auth
95
95
doingUIAuth : boolean ;
96
96
// If set, we've registered but are not going to log
97
97
// the user in to their new account automatically.
98
98
completedNoSignin : boolean ;
99
- flows : {
100
- stages : string [ ] ;
101
- } [ ] ;
99
+ flows :
100
+ | {
101
+ stages : string [ ] ;
102
+ } [ ]
103
+ | null ;
102
104
// We perform liveliness checks later, but for now suppress the errors.
103
105
// We also track the server dead errors independently of the regular errors so
104
106
// that we can render it differently, and override any other error the user may
@@ -158,7 +160,7 @@ export default class Registration extends React.Component<IProps, IState> {
158
160
window . removeEventListener ( "beforeunload" , this . unloadCallback ) ;
159
161
}
160
162
161
- private unloadCallback = ( event : BeforeUnloadEvent ) : string => {
163
+ private unloadCallback = ( event : BeforeUnloadEvent ) : string | undefined => {
162
164
if ( this . state . doingUIAuth ) {
163
165
event . preventDefault ( ) ;
164
166
event . returnValue = "" ;
@@ -215,7 +217,7 @@ export default class Registration extends React.Component<IProps, IState> {
215
217
this . loginLogic . setHomeserverUrl ( hsUrl ) ;
216
218
this . loginLogic . setIdentityServerUrl ( isUrl ) ;
217
219
218
- let ssoFlow : ISSOFlow ;
220
+ let ssoFlow : ISSOFlow | undefined ;
219
221
try {
220
222
const loginFlows = await this . loginLogic . getFlows ( ) ;
221
223
if ( serverConfig !== this . latestServerConfig ) return ; // discard, serverConfig changed from under us
@@ -289,6 +291,7 @@ export default class Registration extends React.Component<IProps, IState> {
289
291
sendAttempt : number ,
290
292
sessionId : string ,
291
293
) : Promise < IRequestTokenResponse > => {
294
+ if ( ! this . state . matrixClient ) throw new Error ( "Matrix client has not yet been loaded" ) ;
292
295
return this . state . matrixClient . requestRegisterEmailToken (
293
296
emailAddress ,
294
297
clientSecret ,
@@ -303,6 +306,8 @@ export default class Registration extends React.Component<IProps, IState> {
303
306
} ;
304
307
305
308
private onUIAuthFinished : InteractiveAuthCallback = async ( success , response ) : Promise < void > => {
309
+ if ( ! this . state . matrixClient ) throw new Error ( "Matrix client has not yet been loaded" ) ;
310
+
306
311
debuglog ( "Registration: ui authentication finished: " , { success, response } ) ;
307
312
if ( ! success ) {
308
313
let errorText : ReactNode = ( response as Error ) . message || ( response as Error ) . toString ( ) ;
@@ -327,10 +332,8 @@ export default class Registration extends React.Component<IProps, IState> {
327
332
</ div >
328
333
) ;
329
334
} else if ( ( response as IAuthData ) . required_stages ?. includes ( AuthType . Msisdn ) ) {
330
- let msisdnAvailable = false ;
331
- for ( const flow of ( response as IAuthData ) . available_flows ) {
332
- msisdnAvailable = msisdnAvailable || flow . stages . includes ( AuthType . Msisdn ) ;
333
- }
335
+ const flows = ( response as IAuthData ) . available_flows ?? [ ] ;
336
+ const msisdnAvailable = flows . some ( ( flow ) => flow . stages . includes ( AuthType . Msisdn ) ) ;
334
337
if ( ! msisdnAvailable ) {
335
338
errorText = _t ( "This server does not support authentication with a phone number." ) ;
336
339
}
@@ -348,12 +351,16 @@ export default class Registration extends React.Component<IProps, IState> {
348
351
return ;
349
352
}
350
353
351
- MatrixClientPeg . setJustRegisteredUserId ( ( response as IAuthData ) . user_id ) ;
354
+ const userId = ( response as IAuthData ) . user_id ;
355
+ const accessToken = ( response as IAuthData ) . access_token ;
356
+ if ( ! userId || ! accessToken ) throw new Error ( "Registration failed" ) ;
357
+
358
+ MatrixClientPeg . setJustRegisteredUserId ( userId ) ;
352
359
353
360
const newState : Partial < IState > = {
354
361
doingUIAuth : false ,
355
362
registeredUsername : ( response as IAuthData ) . user_id ,
356
- differentLoggedInUserId : null ,
363
+ differentLoggedInUserId : undefined ,
357
364
completedNoSignin : false ,
358
365
// we're still busy until we get unmounted: don't show the registration form again
359
366
busy : true ,
@@ -393,13 +400,13 @@ export default class Registration extends React.Component<IProps, IState> {
393
400
// the email, not the client that started the registration flow
394
401
await this . props . onLoggedIn (
395
402
{
396
- userId : ( response as IAuthData ) . user_id ,
403
+ userId,
397
404
deviceId : ( response as IAuthData ) . device_id ,
398
405
homeserverUrl : this . state . matrixClient . getHomeserverUrl ( ) ,
399
406
identityServerUrl : this . state . matrixClient . getIdentityServerUrl ( ) ,
400
- accessToken : ( response as IAuthData ) . access_token ,
407
+ accessToken,
401
408
} ,
402
- this . state . formVals . password ,
409
+ this . state . formVals . password ! ,
403
410
) ;
404
411
405
412
this . setupPushers ( ) ;
@@ -457,6 +464,8 @@ export default class Registration extends React.Component<IProps, IState> {
457
464
} ;
458
465
459
466
private makeRegisterRequest = ( auth : IAuthData | null ) : Promise < IAuthData > => {
467
+ if ( ! this . state . matrixClient ) throw new Error ( "Matrix client has not yet been loaded" ) ;
468
+
460
469
const registerParams : IRegisterRequestParams = {
461
470
username : this . state . formVals . username ,
462
471
password : this . state . formVals . password ,
@@ -494,7 +503,7 @@ export default class Registration extends React.Component<IProps, IState> {
494
503
return sessionLoaded ;
495
504
} ;
496
505
497
- private renderRegisterComponent ( ) : JSX . Element {
506
+ private renderRegisterComponent ( ) : ReactNode {
498
507
if ( this . state . matrixClient && this . state . doingUIAuth ) {
499
508
return (
500
509
< InteractiveAuth
@@ -517,8 +526,8 @@ export default class Registration extends React.Component<IProps, IState> {
517
526
< Spinner />
518
527
</ div >
519
528
) ;
520
- } else if ( this . state . flows . length ) {
521
- let ssoSection ;
529
+ } else if ( this . state . matrixClient && this . state . flows . length ) {
530
+ let ssoSection : JSX . Element | undefined ;
522
531
if ( this . state . ssoFlow ) {
523
532
let continueWithSection ;
524
533
const providers = this . state . ssoFlow . identity_providers || [ ] ;
@@ -571,6 +580,8 @@ export default class Registration extends React.Component<IProps, IState> {
571
580
</ React . Fragment >
572
581
) ;
573
582
}
583
+
584
+ return null ;
574
585
}
575
586
576
587
public render ( ) : React . ReactNode {
0 commit comments