Ajout d'un appel pour check la
validité du token
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<User>;
|
||||
public currentUser: Observable<User>;
|
||||
|
||||
constructor(private http: HttpClient, private dateService: DateService) {
|
||||
constructor(private http: HttpClient) {
|
||||
super();
|
||||
|
||||
this.currentUserSubject = new BehaviorSubject<User>(
|
||||
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<any>(`${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");
|
||||
|
||||
Reference in New Issue
Block a user