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