Vérification des droits d'admin pour ajouter

avions/dz/type de sauts
This commit is contained in:
Sébastien André
2021-03-17 17:21:23 +01:00
parent ba268e739f
commit 76d3943151
15 changed files with 76 additions and 100 deletions

View File

@@ -1,23 +1,14 @@
import { Injectable } from "@angular/core";
import {
CanActivate,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from "@angular/router";
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
import { AuthenticationService } from "./authentication.service";
@Injectable({ providedIn: "root" })
export class AuthGuardService implements CanActivate {
constructor(
private router: Router,
private authenticationService: AuthenticationService
) {}
constructor(private router: Router,
private authenticationService: AuthenticationService) {}
public canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) {
public canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) {
const currentUser = this.authenticationService.currentUserValue;
if (currentUser) {
// logged in so return true
@@ -25,10 +16,9 @@ export class AuthGuardService implements CanActivate {
}
// not logged in so redirect to login page with the return url
this.router.navigate(["/login"], {
skipLocationChange: true,
queryParams: { returnUrl: state.url }
});
this.router.navigate(["/login"],
{skipLocationChange: true, queryParams: { returnUrl: state.url }});
return false;
}
}

View File

@@ -34,36 +34,31 @@ export class AuthenticationService extends BaseService {
password: password
};
return this.http
.post<User>(`${this.apiUrl}/User/Authenticate`, bodyLogin, {
headers: this.headers
})
.pipe(
map(user => {
this.pushUserToken(username, password, user);
return user;
})
);
return this.http.post<User>(`${this.apiUrl}/User/Authenticate`,
bodyLogin,
{ headers: this.headers })
.pipe(map(user => {
this.pushUserToken(username, password, user);
return user;
}));
}
public create(newUser: User) {
return this.http
.post<any>(`${this.apiUrl}/User`, newUser, {
headers: this.headers
})
.pipe(
map(user => {
this.pushUserToken(newUser.login, newUser.password, user);
return user;
})
);
return this.http.post<User>(`${this.apiUrl}/User`,
newUser,
{ headers: this.headers })
.pipe(map(user => {
this.pushUserToken(newUser.login, newUser.password, user);
return user;
}));
}
private pushUserToken(login: string, password: string, user: any){
// store user details and basic auth credentials in local storage to keep user logged in between page refreshes
user.authdata = window.btoa(login + ":" + password);
localStorage.setItem("currentUser", JSON.stringify(user));
this.currentUserSubject.next(user);
private pushUserToken(login: string, password: string, user: User){
if (user && user.token) {
user.authdata = window.btoa(login + ":" + password);
localStorage.setItem("currentUser", JSON.stringify(user));
this.currentUserSubject.next(user);
}
}
private alwaysLogin() {
@@ -73,7 +68,6 @@ export class AuthenticationService extends BaseService {
}
logout() {
// remove user from local storage to log user out
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}

View File

@@ -18,16 +18,5 @@ export class BaseService {
this.apiUrl = tmpApiUrl + '/api';
}
});
// const config: ConfigurationHelper = injector.get(ConfigurationHelper);
// config.load(environment.env)
// .then(() => {
// let tmpApiUrl : string = ConfigurationHelper.settings.apiUrl;
// this.headers = new HttpHeaders({
// 'Access-Control-Allow-Origin': tmpApiUrl
// });
// this.apiUrl = tmpApiUrl + '/api';
// });
}
}