Skip to content

Commit

Permalink
Fix unintentional redirect to dashboard when switching language #167
Browse files Browse the repository at this point in the history
  • Loading branch information
hupf committed Apr 29, 2024
1 parent 4d1e5e7 commit 72283c8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
39 changes: 23 additions & 16 deletions src/components/Portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,29 @@ export class Portal extends LitElement {
connectedCallback(): void {
super.connectedCallback();

portalState.initialized.then(() => {
// When all roles/permissions have been loaded and the current
// app does not match the scope of the current token, activate a
// token for the app's scope. This can be the case when
// previously authenticated as another user and the current user
// has no access to the navigation item from the redirect URL,
// hence is redirected to home (see
// https://github.com/bkd-mba-fbi/evento-portal/issues/106).
if (tokenState.scope !== portalState.app.scope) {
activateTokenForScope(
oAuthClient,
portalState.app.scope,
portalState.locale,
);
}
});
portalState.initialized.then(() =>
setTimeout(() => {
// When all roles/permissions have been loaded and the current app does
// not match the scope of the current token, activate a token for the
// app's scope. This can be the case when previously authenticated as
// another user and the current user has no access to the navigation
// item from the redirect URL, hence is redirected to home (see
// https://github.com/bkd-mba-fbi/evento-portal/issues/106).
//
// Also, escape the callstack with the `setTimeout`, to make sure we are
// checking the scope after the `navigate` call in `handleLoginResult`
// (which is also executed when the `initialized` promise is resolved).
// Without the `setTimeout`, we would check the scope before it is set
// correctly.
if (tokenState.scope !== portalState.app.scope) {
activateTokenForScope(
oAuthClient,
portalState.app.scope,
portalState.locale,
);
}
}),
);

this.subscriptions.push(
portalState.subscribeScopeAndLocale(
Expand Down
24 changes: 19 additions & 5 deletions src/state/portal-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,14 @@ class PortalState extends State {

private async updateLocale(locale: PortalState["locale"]): Promise<void> {
updateQueryParam(LOCALE_QUERY_PARAM, locale);
await updateLocale(locale);
try {
await updateLocale(locale);
} catch (error) {
console.warn(
"Unable to fetch/update locale (this may happen when interrupted by a redirect):",
error,
);
}
}

private updateNavigation(): void {
Expand Down Expand Up @@ -253,10 +260,17 @@ class PortalState extends State {
private async loadInstanceName(): Promise<void> {
if (!tokenState.authenticated) return;

const instanceName = await fetchInstanceName();
this.instanceName = [msg("Evento"), instanceName]
.filter(Boolean)
.join(" | ");
try {
const instanceName = await fetchInstanceName();
this.instanceName = [msg("Evento"), instanceName]
.filter(Boolean)
.join(" | ");
} catch (error) {
console.warn(
"Unable to fetch/update instance name (this may happen when interrupted by a redirect):",
error,
);
}
}
}

Expand Down

0 comments on commit 72283c8

Please sign in to comment.