From be59f274ec6c6b32ab6d58c469cd1c5b767fad19 Mon Sep 17 00:00:00 2001 From: kevinpauer Date: Wed, 23 Mar 2022 19:31:34 +0100 Subject: [PATCH] Add comments --- .../src/app/Views/dashboard/dashboard.component.ts | 2 ++ frontend/src/app/Views/header/header.component.ts | 4 ++++ frontend/src/app/Views/login/login.component.ts | 11 +++++++++++ frontend/src/app/Views/register/register.component.ts | 5 +++++ frontend/src/app/app.component.ts | 7 ++++++- 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/Views/dashboard/dashboard.component.ts b/frontend/src/app/Views/dashboard/dashboard.component.ts index ecc7a4b..ff2d717 100644 --- a/frontend/src/app/Views/dashboard/dashboard.component.ts +++ b/frontend/src/app/Views/dashboard/dashboard.component.ts @@ -50,6 +50,8 @@ const ELEMENT_DATA: PeriodicElement[] = [ export class DashboardComponent implements OnInit { constructor(private dataService: DataService) {} + //TODO avoid using ngOnInit() like this + //TODO fix server problems async ngOnInit() { const data = await this.dataService.getStockData(); } diff --git a/frontend/src/app/Views/header/header.component.ts b/frontend/src/app/Views/header/header.component.ts index 93e7a6c..07e63ab 100644 --- a/frontend/src/app/Views/header/header.component.ts +++ b/frontend/src/app/Views/header/header.component.ts @@ -7,10 +7,14 @@ import { TokenStorageService } from 'src/app/Services/token.service'; styleUrls: ['./header.component.scss'], }) export class HeaderComponent implements OnInit { + /** + * @param {TokenStorageService} privatetokenStorage + */ constructor(private tokenStorage: TokenStorageService) {} ngOnInit(): void {} + //logout() clears session storage; All user data is eradicated from it and page is reloaded logout() { this.tokenStorage.signOut(); location.reload(); diff --git a/frontend/src/app/Views/login/login.component.ts b/frontend/src/app/Views/login/login.component.ts index 420e0b2..dac1bc2 100644 --- a/frontend/src/app/Views/login/login.component.ts +++ b/frontend/src/app/Views/login/login.component.ts @@ -18,17 +18,26 @@ export class LoginComponent implements OnInit { errorMessage = ''; accountName = ''; + /** + * @param {AuthService} privateauthService + * @param {TokenStorageService} privatetokenStorage + * @param {Router} privaterouter + */ constructor( private authService: AuthService, private tokenStorage: TokenStorageService, private router: Router ) {} + + //ngOnInit() checks if a ngOnInit(): void { this.tokenStorage.signOut(); if (this.tokenStorage.getToken()) { this.isLoggedIn = true; } } + + //onSubmit() saves valuable information in session storage onSubmit(): void { const { username, password } = this.form; this.authService.login(username, password).subscribe( @@ -47,6 +56,8 @@ export class LoginComponent implements OnInit { } ); } + + //reloadPage() reloads the page reloadPage(): void { window.location.reload(); } diff --git a/frontend/src/app/Views/register/register.component.ts b/frontend/src/app/Views/register/register.component.ts index b015e29..c032d71 100644 --- a/frontend/src/app/Views/register/register.component.ts +++ b/frontend/src/app/Views/register/register.component.ts @@ -15,6 +15,11 @@ export class RegisterComponent implements OnInit { isSuccessful = false; isSignUpFailed = false; errorMessage = ''; + + /** + * @param {AuthService} privateauthService + * @param {Router} privaterouter + */ constructor(private authService: AuthService, private router: Router) {} ngOnInit(): void {} onSubmit(): void { diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index e3465f6..1a1d467 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -13,8 +13,8 @@ export class AppComponent { * Application title. */ title = 'Aktienbot'; - showHeader = false; + showHeader = false; isLoggedIn = false; /** @@ -26,6 +26,7 @@ export class AppComponent { private router: Router, private tokenStorage: TokenStorageService ) { + //check if it is login or registration page, header should not show there this.router.events .pipe(filter((event) => event instanceof NavigationEnd)) .subscribe((event) => { @@ -33,11 +34,15 @@ export class AppComponent { (event as NavigationEnd).url === '/login' || (event as NavigationEnd).url === '/register' ); + + //check if token already exists from past login if (this.tokenStorage.getToken()) { this.isLoggedIn = true; } else { this.isLoggedIn = false; } + + //prevent user from accessing dashboard if not logged in if ( this.isLoggedIn === false && (event as NavigationEnd).url != '/register'