diff --git a/Front/skydivelogs-app/src/services/auth-guard.service.ts b/Front/skydivelogs-app/src/services/auth-guard.service.ts index 3e71ce4..b9e5e6f 100644 --- a/Front/skydivelogs-app/src/services/auth-guard.service.ts +++ b/Front/skydivelogs-app/src/services/auth-guard.service.ts @@ -1,15 +1,23 @@ -import { Injectable } from '@angular/core'; -import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; -import { AuthenticationService } from './authentication.service'; +import { Injectable } from "@angular/core"; +import { + CanActivate, + Router, + ActivatedRouteSnapshot, + RouterStateSnapshot +} from "@angular/router"; +import { AuthenticationService } from "./authentication.service"; -@Injectable({ providedIn: 'root' }) +@Injectable({ providedIn: "root" }) export class AuthGuardService implements CanActivate { constructor( private router: Router, private authenticationService: AuthenticationService - ) { } + ) {} - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + public canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ) { const currentUser = this.authenticationService.currentUserValue; if (currentUser) { // logged in so return true @@ -17,7 +25,7 @@ export class AuthGuardService implements CanActivate { } // not logged in so redirect to login page with the return url - this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } }); + this.router.navigate(["/login"], { queryParams: { returnUrl: state.url } }); return false; } } diff --git a/Front/skydivelogs-app/src/services/authentication.service.ts b/Front/skydivelogs-app/src/services/authentication.service.ts index 681258d..583d040 100644 --- a/Front/skydivelogs-app/src/services/authentication.service.ts +++ b/Front/skydivelogs-app/src/services/authentication.service.ts @@ -6,7 +6,6 @@ import { map } from "rxjs/operators"; import { User } from "../models/user"; import { BaseService } from "./base.service"; -import { DateService } from "./date.service"; @Injectable({ providedIn: "root" @@ -15,20 +14,22 @@ export class AuthenticationService extends BaseService { private currentUserSubject: BehaviorSubject; public currentUser: Observable; - constructor(private http: HttpClient, private dateService: DateService) { + constructor(private http: HttpClient) { super(); this.currentUserSubject = new BehaviorSubject( JSON.parse(localStorage.getItem("currentUser")) ); this.currentUser = this.currentUserSubject.asObservable(); + + this.alwaysLogin(); } public get currentUserValue(): User { return this.currentUserSubject.value; } - login(username: string, password: string) { + public login(username: string, password: string) { const bodyLogin = { login: username, password: password @@ -49,7 +50,7 @@ export class AuthenticationService extends BaseService { ); } - create(newUser: User) { + public create(newUser: User) { return this.http .post(`${this.apiUrl}/User`, newUser, { headers: this.headers @@ -61,6 +62,12 @@ export class AuthenticationService extends BaseService { ); } + private alwaysLogin() { + return this.http.get(`${this.apiUrl}/User/AlwayLogin`, { + headers: this.headers + }); + } + logout() { // remove user from local storage to log user out localStorage.removeItem("currentUser");