diff --git a/rossa-tech-cli/src/app/auth/auth.service.ts b/rossa-tech-cli/src/app/auth/auth.service.ts index 5ba1fe7..c6ec6ea 100644 --- a/rossa-tech-cli/src/app/auth/auth.service.ts +++ b/rossa-tech-cli/src/app/auth/auth.service.ts @@ -1,30 +1,42 @@ -import { Injectable } from "@angular/core"; +import { Injectable } from '@angular/core'; +import { BehaviorSubject, Observable } from 'rxjs'; @Injectable({ - providedIn: "root", + providedIn: 'root', }) export class AuthService { - constructor() {} + private isLoggedInSubject: BehaviorSubject = + new BehaviorSubject(true); + public isLoggedIn: Observable; + + constructor() { + this.isLoggedIn = this.isLoggedInSubject.asObservable(); + } public checkUserLoggedIn(): Boolean { - let retVal: Boolean = false, + let retVal: boolean = false, currUserObj: any = null, currUser: any; - if (sessionStorage.getItem("currentUser") != null) { - currUserObj = sessionStorage.getItem("currentUser"); + if (sessionStorage.getItem('currentUser') != null) { + currUserObj = sessionStorage.getItem('currentUser'); if ( currUserObj != null && currUserObj.toString() != null && - currUserObj.toString().trim() !== "" + currUserObj.toString().trim() !== '' ) { currUser = JSON.parse(currUserObj.toString()); - if (currUser && currUser.userId && currUser.userId.trim() !== "") { + if (currUser && currUser.userId && currUser.userId.trim() !== '') { retVal = - currUser.tokenValue != null && currUser.tokenValue.trim() !== ""; + currUser.tokenValue != null && currUser.tokenValue.trim() !== ''; } } } + this.setUserLoggedIn(retVal); return retVal; } + + public setUserLoggedIn(val: boolean): void { + this.isLoggedInSubject.next(val); + } } diff --git a/rossa-tech-cli/src/app/auth/login.service.ts b/rossa-tech-cli/src/app/auth/login.service.ts index 443327f..51ed66b 100644 --- a/rossa-tech-cli/src/app/auth/login.service.ts +++ b/rossa-tech-cli/src/app/auth/login.service.ts @@ -1,19 +1,19 @@ -import { Injectable } from "@angular/core"; -import { HttpClient, HttpHeaders } from "@angular/common/http"; -import { Observable } from "rxjs"; +import { Injectable } from '@angular/core'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { Observable } from 'rxjs'; -import { LoginUser } from "../dataModels/loginUser.type"; -import { environment } from "../../environments/environment"; +import { LoginUser } from '../dataModels/loginUser.type'; +import { environment } from '../../environments/environment'; @Injectable({ - providedIn: "root", + providedIn: 'root', }) export class LoginService { constructor(private http: HttpClient) {} public login(userToLogin: LoginUser): Observable { return this.http.post( - environment.apiBaseUrl + "/authenticate", + environment.apiBaseUrl + '/authenticate', userToLogin ); } @@ -21,11 +21,11 @@ export class LoginService { public signout(): Observable { let jwtToken: String = this.getUserSecurityToken(), headers: HttpHeaders = new HttpHeaders({ - authorization: "bearer " + jwtToken, + authorization: 'bearer ' + jwtToken, }), options = { headers: headers }; return this.http.post( - environment.apiBaseUrl + "/signOut", + environment.apiBaseUrl + '/signOut', null, options ); @@ -35,21 +35,21 @@ export class LoginService { if ( userToAdd != null && userToAdd.userId && - userToAdd.userId.trim() !== "" && + userToAdd.userId.trim() !== '' && userToAdd.tokenValue && - userToAdd.tokenValue.trim() !== "" + userToAdd.tokenValue.trim() !== '' ) { - if (sessionStorage.getItem("currentUser") != null) { - sessionStorage.removeItem("currentUser"); + if (sessionStorage.getItem('currentUser') != null) { + sessionStorage.removeItem('currentUser'); } - sessionStorage.setItem("currentUser", JSON.stringify(userToAdd)); + sessionStorage.setItem('currentUser', JSON.stringify(userToAdd)); } } public removeSessionCurrentUser(): void { - if (sessionStorage.getItem("currentUser") != null) { - sessionStorage.removeItem("currentUser"); + if (sessionStorage.getItem('currentUser') != null) { + sessionStorage.removeItem('currentUser'); } } @@ -58,15 +58,15 @@ export class LoginService { currUser: any | null = null, currUserObj: any = null; - if (sessionStorage.getItem("currentUser") != null) { - currUserObj = sessionStorage.getItem("currentUser"); + if (sessionStorage.getItem('currentUser') != null) { + currUserObj = sessionStorage.getItem('currentUser'); if ( currUserObj != null && currUserObj.toString() != null && - currUserObj.toString().trim() !== "" + currUserObj.toString().trim() !== '' ) { currUser = JSON.parse(currUserObj.toString()); - if (currUser && currUser.userId && currUser.userId.trim() !== "") { + if (currUser && currUser.userId && currUser.userId.trim() !== '') { retVal = currUser; } } @@ -76,19 +76,19 @@ export class LoginService { } public getUserSecurityToken(): String { - let retVal: String = "", + let retVal: String = '', currUser: any | null = null, currUserObj: any = null; - if (sessionStorage.getItem("currentUser") != null) { - currUserObj = sessionStorage.getItem("currentUser"); + if (sessionStorage.getItem('currentUser') != null) { + currUserObj = sessionStorage.getItem('currentUser'); if ( currUserObj != null && currUserObj.toString() != null && - currUserObj.toString().trim() !== "" + currUserObj.toString().trim() !== '' ) { currUser = JSON.parse(currUserObj.toString()); - if (currUser && currUser.userId && currUser.userId.trim() !== "") { + if (currUser && currUser.userId && currUser.userId.trim() !== '') { retVal = currUser.tokenValue; } } diff --git a/rossa-tech-cli/src/app/auth/pageSecurity.service.ts b/rossa-tech-cli/src/app/auth/pageSecurity.service.ts index 7b080e5..81420db 100644 --- a/rossa-tech-cli/src/app/auth/pageSecurity.service.ts +++ b/rossa-tech-cli/src/app/auth/pageSecurity.service.ts @@ -1,20 +1,20 @@ -import { Injectable } from "@angular/core"; -import { Router } from "@angular/router"; -import { HttpErrorResponse } from "@angular/common/http"; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { HttpErrorResponse } from '@angular/common/http'; @Injectable({ - providedIn: "root", + providedIn: 'root', }) export class PageSecurityService { constructor(private router: Router) {} public gotoLoginPage() { - this.router.navigate(["/login"]); + this.router.navigate(['/login']); } public checkPageSecurityError(httpError: HttpErrorResponse) { if (!httpError || httpError.status == null) { - throw new Error("Invalid http error object."); + throw new Error('Invalid http error object.'); } if (httpError.status === 401) { @@ -22,7 +22,7 @@ export class PageSecurityService { } if (httpError.status === 403) { - this.router.navigate(["/accessDenied"]); + this.router.navigate(['/accessDenied']); } } } diff --git a/rossa-tech-cli/src/app/components/header/header.component.ts b/rossa-tech-cli/src/app/components/header/header.component.ts index efd6919..caa7b8d 100644 --- a/rossa-tech-cli/src/app/components/header/header.component.ts +++ b/rossa-tech-cli/src/app/components/header/header.component.ts @@ -26,6 +26,10 @@ export class HeaderComponent implements OnInit { ngOnInit(): void { this.userLoggedIn = this.authService.checkUserLoggedIn(); + + this.authService.isLoggedIn.subscribe((res) => { + this.userLoggedIn = res; + }); } public onClickLogout(): void { diff --git a/rossa-tech-cli/src/app/components/loginPage/login.component.ts b/rossa-tech-cli/src/app/components/loginPage/login.component.ts index 3fb13d1..adba442 100644 --- a/rossa-tech-cli/src/app/components/loginPage/login.component.ts +++ b/rossa-tech-cli/src/app/components/loginPage/login.component.ts @@ -1,24 +1,24 @@ -import { Component, OnInit } from "@angular/core"; -import { HttpErrorResponse } from "@angular/common/http"; -import { Router } from "@angular/router"; +import { Component, OnInit } from '@angular/core'; +import { HttpErrorResponse } from '@angular/common/http'; +import { Router } from '@angular/router'; -import { LoginUser } from "../../dataModels/loginUser.type"; -import { LoginService } from "../../auth/login.service"; -import { FormsService } from "../../services/forms.service"; -import { AuthService } from "src/app/auth/auth.service"; -import { FormControl, FormGroup, Validators } from "@angular/forms"; -import { LoaderService } from "../loader/loader.service"; -import { NotificationService } from "src/app/services/notification.service"; +import { LoginUser } from '../../dataModels/loginUser.type'; +import { LoginService } from '../../auth/login.service'; +import { FormsService } from '../../services/forms.service'; +import { AuthService } from 'src/app/auth/auth.service'; +import { FormControl, FormGroup, Validators } from '@angular/forms'; +import { LoaderService } from '../loader/loader.service'; +import { NotificationService } from 'src/app/services/notification.service'; @Component({ - selector: "app-root", - templateUrl: "./login.component.html", - styleUrls: ["./login.component.scss"], + selector: 'app-root', + templateUrl: './login.component.html', + styleUrls: ['./login.component.scss'], }) export class LoginComponent implements OnInit { loginForm: FormGroup = new FormGroup({ - userName: new FormControl("", Validators.required), - userPass: new FormControl("", Validators.required), + userName: new FormControl('', Validators.required), + userPass: new FormControl('', Validators.required), }); constructor( @@ -33,7 +33,7 @@ export class LoginComponent implements OnInit { ngOnInit() { let userLoggedIn: Boolean = this.authService.checkUserLoggedIn(); if (userLoggedIn) { - this.router.navigate(["/index"]); + this.router.navigate(['/dashboard']); } } @@ -58,33 +58,33 @@ export class LoginComponent implements OnInit { if ( resp != null && resp.userId != null && - resp.userId.trim() !== "" && + resp.userId.trim() !== '' && resp.tokenValue != null && - resp.tokenValue.trim() !== "" + resp.tokenValue.trim() !== '' ) { this.loginService.setSessionCurrentUser(resp); - this.router.navigate(["/"]); + this.router.navigate(['/']); } }, error: (error: HttpErrorResponse) => { if (error != null) { if (error.status === 0) { - console.log("Client error."); + console.log('Client error.'); this.notificationService.showSnackbar( - "Client error", + 'Client error', 3000, - "snackbar-warning", + 'snackbar-warning', true, - "alert-circle-outline" + 'alert-circle-outline' ); } else if (error.status === 401 || error.status === 403) { this.loginForm.reset(); this.formsService.makeFormFieldsClean(this.loginForm); - console.log("You are not authorized."); + console.log('You are not authorized.'); } else if (error.status === 500) { - console.log("Server error occurred."); + console.log('Server error occurred.'); } else { - console.log("Unknown error: " + error.status); + console.log('Unknown error: ' + error.status); } this.loaderService.hide(); }