Skip to content

Commit

Permalink
improve login redirect performance + inntroducce ccode param for the …
Browse files Browse the repository at this point in the history
…not-authorized route
  • Loading branch information
waseem.sabjee committed Sep 15, 2021
1 parent b02bfb1 commit 195b238
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/app/pages/error/error.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<h2>{{errorMessage | translate}}</h2>
<div *ngIf="code">
<div *ngIf="code == 'no-app-access-msg'">{{ 'no_app_access_msg' | translate }}</div>
</div>
16 changes: 14 additions & 2 deletions src/app/pages/error/error.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import { GenericUtil } from '@worldskills/worldskills-angular-lib';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'app-error',
Expand All @@ -10,12 +12,22 @@ export class ErrorComponent implements OnInit {

errorMessage: string;

constructor(private route: ActivatedRoute) {
code: string;

constructor(private route: ActivatedRoute, private translator: TranslateService) {
}

ngOnInit(): void {
const data = this.route.snapshot.data;
this.errorMessage = data && data.error ? data.error : 'Not found';
}

this.route.queryParams.subscribe(
queryParams => {
console.log(queryParams);
if (queryParams.hasOwnProperty('code')) {
this.code = queryParams.code;
}
}
);
}
}
3 changes: 3 additions & 0 deletions src/app/pages/home/home.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<ng-container *ngIf="initialized">
loaded
</ng-container>
<ng-container *ngIf="!initialized">
<div class="mt-4">
<ws-spinner></ws-spinner>
Expand Down
24 changes: 13 additions & 11 deletions src/app/pages/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ export class HomeComponent implements OnInit {

ngOnInit(): void {
const homepage = this.getHomepage();

this.handler.redirectOrReturn({url: [homepage], onlyIfExact: this.route})
.subscribe(() => {
if (homepage.length > 0) {
this.initialized = true;
} else {
this.auth.login();
}
});

if (homepage === 'not-authorized') {
this.router.navigate(['/not-authorized'], { queryParams: { code: 'no-app-access-msg'}});
} else {
this.handler.redirectOrReturn({url: [homepage], onlyIfExact: this.route})
.subscribe(() => {
if (homepage.length > 0) {
this.initialized = true;
} else {
this.auth.login();
}
});
}
}

// when admin then homepage = org
Expand All @@ -49,7 +51,7 @@ export class HomeComponent implements OnInit {
} else {
// tslint:disable-next-line:max-line-length
if (user.roles && (user.roles.length === 0 || user.roles.filter(x => x.role_application.application_code === environment.worldskillsAppId).length === 0)) {
this.router.navigate(['/not-authorized']);
homepage = 'not-authorized';
} else {
const hasAdminRole = user.roles.filter(x => x.name === 'Admin').length > 0;
if (hasAdminRole) {
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
"no_app_access_msg": "You do nnot have access to this application, please contact your member organization."
}

0 comments on commit 195b238

Please sign in to comment.