Skip to content

Commit

Permalink
fix: redirect the user to the original Url after login (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
illfixit authored Nov 28, 2024
1 parent 2d436a6 commit d5c1282
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ please see [changelog_updates.md](docs/dev/changelog_updates.md).
- Added live update when deactivating/reactivating users ([#287](https://github.com/sovity/authority-portal/issues/287))
- Fixed Website title not updating in some scenarios [#237](https://github.com/sovity/authority-portal/issues/237)
- Fixed security vulnerabilities
- Fixed the user not being redirected to the correct URL after login ([#324](https://github.com/sovity/authority-portal/issues/324))

### Known issues

Expand Down
23 changes: 11 additions & 12 deletions authority-portal-frontend/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ export const LOADING_ROUTES: Routes = [
},
];

export const FEATURE_HOME_ROUTE: Routes = [
{
path: 'home',
component: HomePageComponent,
data: {
requiresRole: ['USER'] satisfies UserRoleDto[],
},
canActivate: [requiresRole],
},
];

const REDIRECT_TO_HOME: string[] = [
'',
'registration/pending',
Expand All @@ -94,7 +83,6 @@ const REDIRECT_TO_HOME: string[] = [

const getProperRedirectUrl = (fallbackUrl: string) => {
const url = localStorage.getItem('originalUrl') || fallbackUrl;
localStorage.removeItem('originalUrl');
return url;
};

Expand All @@ -110,6 +98,17 @@ export const HOME_REDIRECTS: Routes = REDIRECT_TO_HOME.map((path) => ({
pathMatch: 'full',
}));

export const FEATURE_HOME_ROUTE: Routes = [
{
path: 'home',
component: HomePageComponent,
data: {
requiresRole: ['USER'] satisfies UserRoleDto[],
},
canActivate: [requiresRole],
},
];

export const FEATURE_DASHBOARD_ROUTE: Routes = [
{
path: 'dashboard',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from './global-state-actions';
import {AuthorityPortalPageSet} from './routes/authority-portal-page-set';
import {RouteConfigService} from './routes/route-config-service';
import {UrlBeforeLoginService} from './routes/url-before-login.service';

@State<GlobalState>({
name: 'GlobalState',
Expand All @@ -46,6 +47,7 @@ export class GlobalStateImpl implements NgxsOnInit {
private ngZone: NgZone,
private apiService: ApiService,
private routeConfigService: RouteConfigService,
private urlBeforeLoginService: UrlBeforeLoginService,
) {}

ngxsOnInit(ctx: StateContext<any>): void {
Expand Down Expand Up @@ -123,6 +125,10 @@ export class GlobalStateImpl implements NgxsOnInit {
let roles = new Set(userInfo.map((it) => it.roles).orElse([]));
roles = isEqualSets(state.roles, roles) ? state.roles : roles;

if (!roles.has('UNAUTHENTICATED')) {
this.urlBeforeLoginService.clearOriginalUrl();
}

return {userInfo, pageSet, roles};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,29 @@ export class RouteConfigService {
}

// Change routes
const routes = this.mapping[nextPageSet];
const routes = [...this.mapping[nextPageSet]];

if (nextPageSet === 'AUTHORITY_PORTAL') {
const apRouteChildren = routes.find((r) => r.path === '')?.children;
const rootRouteIndex = routes.findIndex((r) => r.path === '');

// Add home route depending on feature set
if (this.activeFeatureSet.isHomePageEnabled()) {
apRouteChildren?.push(...HOME_REDIRECTS, ...FEATURE_HOME_ROUTE);
} else {
apRouteChildren?.push(...CATALOG_REDIRECTS);
}
if (rootRouteIndex !== -1) {
const rootRoute = routes[rootRouteIndex];
const existingChildren = rootRoute.children || [];

// Add home route depending on feature set
const newChildren = this.activeFeatureSet.isHomePageEnabled()
? [...existingChildren, ...HOME_REDIRECTS, ...FEATURE_HOME_ROUTE]
: [...existingChildren, ...CATALOG_REDIRECTS];

// Add additional routes depending on feature set & configuration
if (this.activeFeatureSet.isDashboardEnabled()) {
newChildren.push(...FEATURE_DASHBOARD_ROUTE);
}

// Add additional routes depending on feature set & configuration
if (this.activeFeatureSet.isDashboardEnabled()) {
apRouteChildren?.push(...FEATURE_DASHBOARD_ROUTE);
routes[rootRouteIndex] = {
...rootRoute,
children: newChildren,
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ export class UrlBeforeLoginService {
});
}

public reset(): void {
public clearOriginalUrl(): void {
this.originalUrl = '';
localStorage.removeItem('originalUrl');
}

public goToOriginalUrl(): void {
if (this.originalUrl) {
this.router.navigateByUrl(this.originalUrl);
this.reset();
this.clearOriginalUrl();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* sovity GmbH - initial implementation
*/
import {Component, HostBinding, Inject} from '@angular/core';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {DomSanitizer, SafeResourceUrl, Title} from '@angular/platform-browser';
import {APP_CONFIG, AppConfig} from 'src/app/core/services/config/app-config';

@Component({
Expand All @@ -31,7 +31,9 @@ export class HomePageComponent {
constructor(
@Inject(APP_CONFIG) public appConfig: AppConfig,
private sanitizer: DomSanitizer,
private titleService: Title,
) {
this.titleService.setTitle('Home');
if (this.appConfig.iframeUrl) {
this.iframeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
this.appConfig.iframeUrl,
Expand Down

0 comments on commit d5c1282

Please sign in to comment.