@@ -106,9 +106,16 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
106106 const isLogin = this . navigation . path . get ( "login" ) ;
107107 const logoutSessionId = this . navigation . path . get ( "logout" ) ?. value ;
108108 const isForcedLogout = this . navigation . path . get ( "forced" ) ?. value ;
109- const sessionId = this . navigation . path . get ( "session" ) ?. value ;
110109 const loginToken = this . navigation . path . get ( "sso" ) ?. value ;
111110
111+ let sessionId = this . navigation . path . get ( "session" ) ?. value ;
112+ if ( sessionId === true ) {
113+ // When logging out, we end up here (sessionId = true).
114+ // Since user is now logged out and as there can only be a single session,
115+ // we want to show the login screen directly.
116+ sessionId = null ;
117+ }
118+
112119 if ( isLogin ) {
113120 if ( this . activeSection !== Section . Login ) {
114121 this . _showLogin ( undefined ) ;
@@ -121,10 +128,6 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
121128 if ( this . activeSection !== Section . Logout ) {
122129 this . _showLogout ( logoutSessionId ) ;
123130 }
124- } else if ( sessionId === true ) {
125- if ( this . activeSection !== Section . SessionPicker ) {
126- void this . _showPicker ( ) ;
127- }
128131 } else if ( sessionId ) {
129132 const singleRoomId = await this . getSingleRoomId ( ) ;
130133 if ( singleRoomId ) {
@@ -153,76 +156,55 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
153156 }
154157 } else {
155158 try {
156- await this . _showInitialScreen ( shouldRestoreLastUrl ) ;
159+ await this . _showInitialScreen ( ) ;
157160 } catch ( err ) {
158161 console . error ( err ) ;
159162 this . _setSection ( ( ) => this . _error = err ) ;
160163 }
161164 }
162165 }
163166
164- private async _showInitialScreen ( shouldRestoreLastUrl : boolean ) {
165- const sessionInfos = await this . platform . sessionInfoStorage . getAll ( ) ;
166- const singleRoomId = await this . getSingleRoomId ( ) ;
167+ private async _showInitialScreen ( ) {
168+ let sessionInfos = await this . platform . sessionInfoStorage . getAll ( ) ;
167169
168- if ( shouldRestoreLastUrl && singleRoomId ) {
169- // Do not restore last URL to Login if we're in single-room mode.
170- // We do this so that we can try guest login when appropriate.
171- const willShowLogin = this . platform . history . getLastSessionUrl ( ) === "#/login" ;
172- if ( willShowLogin ) {
173- shouldRestoreLastUrl = false ;
170+ // In previous versions, it was possible to have multiple sessions open.
171+ // When we have multiple sessions, we want to log out all of them except one.
172+ if ( sessionInfos . length > 1 ) {
173+ for ( let i = 0 ; i < sessionInfos . length - 1 ; i ++ ) {
174+ await this . platform . sessionInfoStorage . delete ( sessionInfos [ i ] . id ) ;
174175 }
175176
176- // Do not restore last URL to session picker if we're in single-room mode and there are zero or one sessions.
177- // We do this so that we can try guest login when there are no sessions, or in the case where there is one
178- // session, use that session.
179- const willShowSessionPicker = this . platform . history . getLastSessionUrl ( ) === "#/session" ;
180- if ( shouldRestoreLastUrl && willShowSessionPicker && sessionInfos . length <= 1 ) {
181- shouldRestoreLastUrl = false ;
177+ sessionInfos = await this . platform . sessionInfoStorage . getAll ( ) ;
178+ if ( sessionInfos . length > 1 ) {
179+ console . error ( "Expected to have a single session, but multiple sessions were found." ) ;
182180 }
183181 }
184182
183+ // Only restore last url if there is already a session.
184+ // This will result in the user being sent to login screen.
185+ let shouldRestoreLastUrl = sessionInfos . length > 1 ;
186+
187+ // We never want to show the session picker, even if it was the last url.
188+ const willShowSessionPicker = this . platform . history . getLastSessionUrl ( ) === "#/session" ;
189+ if ( willShowSessionPicker ) {
190+ shouldRestoreLastUrl = false ;
191+ }
192+
185193 if ( shouldRestoreLastUrl && this . urlRouter . tryRestoreLastUrl ( ) ) {
186194 // Restored last URL.
187- // By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else
188- // to do.
195+ // By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else to do.
189196 return ;
190197 }
191198
192199 // We were not able to restore the last URL.
193200 // So we send the user to the screen that makes the most sense, according to how many sessions they have.
194201
195- // Go to Login or, when in single-room mode, try registering guest user.
196- if ( sessionInfos . length === 0 ) {
197- if ( ! singleRoomId ) {
198- this . navigation . push ( Section . Login ) ;
199- return ;
200- }
201-
202- // Attempt to log in as guest. If it fails, go to Login.
203- const homeserver = await lookupHomeserver ( singleRoomId . split ( ':' ) [ 1 ] , this . platform . request ) ;
204- const client = new Client ( this . platform ) ;
205-
206- await client . doGuestLogin ( homeserver ) ;
207- if ( client . loadError ) {
208- console . warn ( "Failed to login as guest. Guest registration is probably disabled on the homeserver" , client . loadError ) ;
209- this . navigation . push ( Section . Login ) ;
210- return ;
211- }
212-
213- this . _pendingClient = client ;
214- this . navigation . push ( Section . Session , client . sessionId ) ;
215- return ;
216- }
217-
218- // Open session.
219202 if ( sessionInfos . length === 1 ) {
220203 this . navigation . push ( Section . Session , sessionInfos [ 0 ] . id ) ;
221204 return ;
222205 }
223206
224- // Open session picker.
225- this . navigation . push ( Section . Session ) ;
207+ this . navigation . push ( Section . Login ) ;
226208 }
227209
228210 private async resolveRoomAlias ( roomIdOrAlias : string ) : Promise < string > {
0 commit comments