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 { Injectable } from "@angular/core";
|
||||||
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
import {
|
||||||
import { AuthenticationService } from './authentication.service';
|
CanActivate,
|
||||||
|
Router,
|
||||||
|
ActivatedRouteSnapshot,
|
||||||
|
RouterStateSnapshot
|
||||||
|
} from "@angular/router";
|
||||||
|
import { AuthenticationService } from "./authentication.service";
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
@Injectable({ providedIn: "root" })
|
||||||
export class AuthGuardService implements CanActivate {
|
export class AuthGuardService implements CanActivate {
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private authenticationService: AuthenticationService
|
private authenticationService: AuthenticationService
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
public canActivate(
|
||||||
|
route: ActivatedRouteSnapshot,
|
||||||
|
state: RouterStateSnapshot
|
||||||
|
) {
|
||||||
const currentUser = this.authenticationService.currentUserValue;
|
const currentUser = this.authenticationService.currentUserValue;
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
// logged in so return true
|
// 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
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import { map } from "rxjs/operators";
|
|||||||
|
|
||||||
import { User } from "../models/user";
|
import { User } from "../models/user";
|
||||||
import { BaseService } from "./base.service";
|
import { BaseService } from "./base.service";
|
||||||
import { DateService } from "./date.service";
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: "root"
|
providedIn: "root"
|
||||||
@@ -15,20 +14,22 @@ export class AuthenticationService extends BaseService {
|
|||||||
private currentUserSubject: BehaviorSubject<User>;
|
private currentUserSubject: BehaviorSubject<User>;
|
||||||
public currentUser: Observable<User>;
|
public currentUser: Observable<User>;
|
||||||
|
|
||||||
constructor(private http: HttpClient, private dateService: DateService) {
|
constructor(private http: HttpClient) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.currentUserSubject = new BehaviorSubject<User>(
|
this.currentUserSubject = new BehaviorSubject<User>(
|
||||||
JSON.parse(localStorage.getItem("currentUser"))
|
JSON.parse(localStorage.getItem("currentUser"))
|
||||||
);
|
);
|
||||||
this.currentUser = this.currentUserSubject.asObservable();
|
this.currentUser = this.currentUserSubject.asObservable();
|
||||||
|
|
||||||
|
this.alwaysLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
public get currentUserValue(): User {
|
public get currentUserValue(): User {
|
||||||
return this.currentUserSubject.value;
|
return this.currentUserSubject.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(username: string, password: string) {
|
public login(username: string, password: string) {
|
||||||
const bodyLogin = {
|
const bodyLogin = {
|
||||||
login: username,
|
login: username,
|
||||||
password: password
|
password: password
|
||||||
@@ -49,7 +50,7 @@ export class AuthenticationService extends BaseService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
create(newUser: User) {
|
public create(newUser: User) {
|
||||||
return this.http
|
return this.http
|
||||||
.post<any>(`${this.apiUrl}/User`, newUser, {
|
.post<any>(`${this.apiUrl}/User`, newUser, {
|
||||||
headers: this.headers
|
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() {
|
logout() {
|
||||||
// remove user from local storage to log user out
|
// remove user from local storage to log user out
|
||||||
localStorage.removeItem("currentUser");
|
localStorage.removeItem("currentUser");
|
||||||
|
|||||||
Reference in New Issue
Block a user