Skip to content

Commit

Permalink
DDT-1461: delegate auth to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdiiir authored and nt-gt committed Jan 17, 2023
1 parent 95953fd commit 6771099
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 81 deletions.
3 changes: 1 addition & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import {Routes, RouterModule} from '@angular/router';
import {AuthGuard} from './auth/auth-guard.guard';
import {AuthService} from './auth/auth.service';
import {DashboardComponent} from './view/dashboard/dashboard.component';
import {environment} from '../environments/environment';

const guards: any[] = environment.authentication ? [AuthGuard] : [];
const guards: any[] = [AuthGuard];

const routes: Routes = [
{path: '', redirectTo: '/dashboard', pathMatch: 'full'},
Expand Down
23 changes: 14 additions & 9 deletions src/app/auth/auth-guard.guard.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { CanActivate, UrlTree } from '@angular/router';
import { Auth } from 'aws-amplify';
import { Globals } from '../model/portCall/globals';

@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {

async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean | UrlTree> {
try {
const session = await Auth.currentSession()
return session.isValid()
} catch(e) {
return false;
constructor(
private globals: Globals,
) { }
async canActivate(): Promise<boolean | UrlTree> {
if (this.globals.config.authentication === true) {
try {
const session = await Auth.currentSession()
return session.isValid()
} catch (e) {
return false;
}
}

return true;
}
}
1 change: 0 additions & 1 deletion src/app/controller/services/base/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ export class ConfigService {
resolve();
});
});

}
}
1 change: 1 addition & 0 deletions src/app/model/jit/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export interface Config {
authUserPoolWebClientId: string;
authRedirectUriSignIn: string;
enableShowTimestampsAsJSON: boolean | null;
authentication: boolean | null;

}
2 changes: 0 additions & 2 deletions src/app/model/portCall/globals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {Injectable} from '@angular/core';
import {Port} from "./port";
import {Terminal} from "./terminal";
import {Config} from "../jit/config";

@Injectable()
Expand Down
9 changes: 4 additions & 5 deletions src/app/view/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Port} from "../model/portCall/port";
import {TranslateService} from "@ngx-translate/core";
import {TransportCall} from "../model/jit/transport-call";
import { Timestamp } from '../model/jit/timestamp';
import { Component, ChangeDetectorRef, OnInit, OnDestroy, NgZone } from '@angular/core';
import { onAuthUIStateChange, CognitoUserInterface, AuthState } from '@aws-amplify/ui-components';
import { environment } from "src/environments/environment";
import { Globals } from "../model/portCall/globals";
import { Router } from "@angular/router";
import {AuthService} from "../auth/auth.service";

Expand All @@ -28,13 +26,14 @@ export class AppComponent implements OnInit, OnDestroy {
private authService: AuthService,
private ref: ChangeDetectorRef,
private zone: NgZone,
private router: Router) {
private router: Router,
private globals: Globals) {
translate.setDefaultLang('en');
translate.use('en');
}

ngOnInit() {
this.authLocalState = environment.authentication;
this.authLocalState = this.globals.config.authentication;
this.stateChange = onAuthUIStateChange((authState, authData) => {
this.authState = authState;
this.user = authData as CognitoUserInterface;
Expand Down
25 changes: 0 additions & 25 deletions src/app/view/dashboard/dashboard.component.spec.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/app/view/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { MenuItem, MessageService, SelectItem } from "primeng/api";
import { TranslateService } from "@ngx-translate/core";
import { Globals } from "../../model/portCall/globals";
import { ExportService } from "../../controller/services/base/export.service";
import { environment } from "src/environments/environment";
import {take} from "rxjs";

@Component({
Expand Down Expand Up @@ -65,7 +64,7 @@ export class HeaderComponent implements OnInit {
]
}
];
this.authLocalState = environment.authentication;
this.authLocalState = this.globals.config.authentication;
}

showInstructions() {
Expand Down
10 changes: 5 additions & 5 deletions src/assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"uiSupportBackendURL": "http://localhost:9091/ui-support/v1",
"jitBackendURL": "http://localhost:9090/jit/v1",
"dateTimeFormat": "dd MMM yyyy HH:mm",
"authRegion":"eu-west-1",
"authRegion": "eu-west-1",
"authUserPoolId": "eu-west-1_khrRmnDXz",
"authUserPoolWebClientId": "5qgd24cfb8m4sm6bkrfk55cqfr",
"authRedirectUriSignIn":"/dashboard",
"enableShowTimestampsAsJSON": true
}

"authRedirectUriSignIn": "/dashboard",
"enableShowTimestampsAsJSON": true,
"authentication": false
}
52 changes: 26 additions & 26 deletions src/assets/devconfig.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"id": "be5bc290-7bac-48bb-a211-f3fa5a3ab3ae",
"company": "DCSA",
"publisher": {
"partyName": "Test publisher",
"taxReference1": null,
"taxReference2": null,
"address": null,
"identifyingCodes": [
{
"DCSAResponsibleAgencyCode": "SMDG",
"partyCode": "MSK",
"codeListName": "LCL"
}
]
},
"publisherRoles": ["CA", "AG", "VSL", "TR", "ATH", "PLT", "TWG", "BUK", "LSH", "SLU", "SVP", "MOR"],
"uiSupportBackendURL": "/uisupport",
"jitBackendURL": "/jit",
"dateTimeFormat": "dd MMM yyyy HH:mm",
"authRegion":"eu-west-1",
"authUserPoolId": "eu-west-1_khrRmnDXz",
"authUserPoolWebClientId": "5qgd24cfb8m4sm6bkrfk55cqfr",
"authRedirectUriSignIn":"/dashboard",
"enableShowTimestampsAsJSON": true
}

"id": "be5bc290-7bac-48bb-a211-f3fa5a3ab3ae",
"company": "DCSA",
"publisher": {
"partyName": "Test publisher",
"taxReference1": null,
"taxReference2": null,
"address": null,
"identifyingCodes": [
{
"DCSAResponsibleAgencyCode": "SMDG",
"partyCode": "MSK",
"codeListName": "LCL"
}
]
},
"publisherRoles": ["CA", "AG", "VSL", "TR", "ATH", "PLT", "TWG", "BUK", "LSH", "SLU", "SVP", "MOR"],
"uiSupportBackendURL": "/uisupport",
"jitBackendURL": "/jit",
"dateTimeFormat": "dd MMM yyyy HH:mm",
"authRegion": "eu-west-1",
"authUserPoolId": "eu-west-1_khrRmnDXz",
"authUserPoolWebClientId": "5qgd24cfb8m4sm6bkrfk55cqfr",
"authRedirectUriSignIn": "/dashboard",
"enableShowTimestampsAsJSON": true,
"authentication": false
}
3 changes: 1 addition & 2 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const environment = {
production: true,
authentication: true
production: true
};

// define Server here, if you are not in production mode and want to have another server than http://localhost:8080
Expand Down
3 changes: 1 addition & 2 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// The list of file replacements can be found in `angular.json`.

export const environment = {
production: false,
authentication: false
production: false
};

export const BACKEND_URL = window.location.origin + "/api";
Expand Down

0 comments on commit 6771099

Please sign in to comment.