@@ -293,14 +293,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
293
293
294
294
RoomNotificationStateStore . instance . on ( UPDATE_STATUS_INDICATOR , this . onUpdateStatusIndicator ) ;
295
295
296
- // Force users to go through the soft logout page if they're soft logged out
297
- if ( Lifecycle . isSoftLogout ( ) ) {
298
- // When the session loads it'll be detected as soft logged out and a dispatch
299
- // will be sent out to say that, triggering this MatrixChat to show the soft
300
- // logout page.
301
- Lifecycle . loadSession ( ) ;
302
- }
303
-
304
296
this . dispatcherRef = dis . register ( this . onAction ) ;
305
297
306
298
this . themeWatcher = new ThemeWatcher ( ) ;
@@ -314,52 +306,77 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
314
306
// we don't do it as react state as i'm scared about triggering needless react refreshes.
315
307
this . subTitleStatus = "" ;
316
308
317
- // the first thing to do is to try the token params in the query-string
318
- // if the session isn't soft logged out (ie: is a clean session being logged in)
319
- if ( ! Lifecycle . isSoftLogout ( ) ) {
320
- Lifecycle . attemptDelegatedAuthLogin (
321
- this . props . realQueryParams ,
322
- this . props . defaultDeviceDisplayName ,
323
- this . getFragmentAfterLogin ( ) ,
324
- ) . then ( async ( loggedIn ) : Promise < boolean | void > => {
325
- if (
326
- this . props . realQueryParams ?. loginToken ||
327
- this . props . realQueryParams ?. code ||
328
- this . props . realQueryParams ?. state
329
- ) {
330
- // remove the loginToken or auth code from the URL regardless
331
- this . props . onTokenLoginCompleted ( ) ;
332
- }
309
+ initSentry ( SdkConfig . get ( "sentry" ) ) ;
333
310
334
- if ( loggedIn ) {
335
- this . tokenLogin = true ;
311
+ this . initSession ( ) . catch ( ( err ) => {
312
+ // TODO: show an error screen, rather than a spinner of doom
313
+ logger . error ( "Error initialising Matrix session" , err ) ;
314
+ } ) ;
315
+ }
336
316
337
- // Create and start the client
338
- // accesses the new credentials just set in storage during attemptTokenLogin
339
- // and sets logged in state
340
- await Lifecycle . restoreFromLocalStorage ( {
341
- ignoreGuest : true ,
342
- } ) ;
343
- return this . postLoginSetup ( ) ;
344
- }
317
+ /**
318
+ * Do what we can to establish a Matrix session.
319
+ *
320
+ * * Special-case soft-logged-out sessions
321
+ * * If we have OIDC or token login parameters, follow them
322
+ * * If we have a guest access token in the query params, use that
323
+ * * If we have parameters in local storage, use them
324
+ * * Attempt to auto-register as a guest
325
+ * * If all else fails, present a login screen.
326
+ */
327
+ private async initSession ( ) : Promise < void > {
328
+ // If the user was soft-logged-out, we want to make the SoftLogout component responsible for doing any
329
+ // token auth (rather than Lifecycle.attemptDelegatedAuthLogin), since SoftLogout knows about submitting the
330
+ // device ID and preserving the session.
331
+ //
332
+ // So, we start by special-casing soft-logged-out sessions.
333
+ if ( Lifecycle . isSoftLogout ( ) ) {
334
+ // When the session loads it'll be detected as soft logged out and a dispatch
335
+ // will be sent out to say that, triggering this MatrixChat to show the soft
336
+ // logout page.
337
+ Lifecycle . loadSession ( ) ;
338
+ return ;
339
+ }
345
340
346
- // if the user has followed a login or register link, don't reanimate
347
- // the old creds, but rather go straight to the relevant page
348
- const firstScreen = this . screenAfterLogin ? this . screenAfterLogin . screen : null ;
349
- const restoreSuccess = await this . loadSession ( ) ;
350
- if ( restoreSuccess ) {
351
- return true ;
352
- }
341
+ // Otherwise, the first thing to do is to try the token params in the query-string
342
+ const delegatedAuthSucceeded = await Lifecycle . attemptDelegatedAuthLogin (
343
+ this . props . realQueryParams ,
344
+ this . props . defaultDeviceDisplayName ,
345
+ this . getFragmentAfterLogin ( ) ,
346
+ ) ;
353
347
354
- if ( firstScreen === "login" || firstScreen === "register" || firstScreen === "forgot_password" ) {
355
- this . showScreenAfterLogin ( ) ;
356
- }
348
+ // remove the loginToken or auth code from the URL regardless
349
+ if (
350
+ this . props . realQueryParams ?. loginToken ||
351
+ this . props . realQueryParams ?. code ||
352
+ this . props . realQueryParams ?. state
353
+ ) {
354
+ this . props . onTokenLoginCompleted ( ) ;
355
+ }
357
356
358
- return false ;
359
- } ) ;
357
+ if ( delegatedAuthSucceeded ) {
358
+ // token auth/OIDC worked! Time to fire up the client.
359
+ this . tokenLogin = true ;
360
+
361
+ // Create and start the client
362
+ // accesses the new credentials just set in storage during attemptDelegatedAuthLogin
363
+ // and sets logged in state
364
+ await Lifecycle . restoreFromLocalStorage ( { ignoreGuest : true } ) ;
365
+ await this . postLoginSetup ( ) ;
366
+ return ;
360
367
}
361
368
362
- initSentry ( SdkConfig . get ( "sentry" ) ) ;
369
+ // if the user has followed a login or register link, don't reanimate
370
+ // the old creds, but rather go straight to the relevant page
371
+ const firstScreen = this . screenAfterLogin ? this . screenAfterLogin . screen : null ;
372
+ const restoreSuccess = await this . loadSession ( ) ;
373
+ if ( restoreSuccess ) {
374
+ return ;
375
+ }
376
+
377
+ if ( firstScreen === "login" || firstScreen === "register" || firstScreen === "forgot_password" ) {
378
+ this . showScreenAfterLogin ( ) ;
379
+ }
363
380
}
364
381
365
382
private async postLoginSetup ( ) : Promise < void > {
0 commit comments