Début d'ajout pour la gestion du login
This commit is contained in:
@@ -8,13 +8,13 @@ import { AircraftResp, AircraftReq } from '../models/aircraft';
|
||||
export class AircraftService {
|
||||
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public getListOfAircrafts(): Observable<Array<AircraftResp>> {
|
||||
return this.http.get<Array<AircraftResp>>(
|
||||
`${environment.urlApi}/api/Aircraft`,
|
||||
`${environment.apiUrl}/api/Aircraft`,
|
||||
{ headers: this.headers }
|
||||
);
|
||||
}
|
||||
@@ -26,7 +26,7 @@ export class AircraftService {
|
||||
};
|
||||
|
||||
this.http
|
||||
.post(`${environment.urlApi}/api/Aircraft`, bodyNewAircraft, {
|
||||
.post(`${environment.apiUrl}/api/Aircraft`, bodyNewAircraft, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
|
||||
23
Front/skydivelogs-app/src/services/auth-guard.service.ts
Normal file
23
Front/skydivelogs-app/src/services/auth-guard.service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
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
|
||||
) { }
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const currentUser = this.authenticationService.currentUserValue;
|
||||
if (currentUser) {
|
||||
// logged in so return true
|
||||
return true;
|
||||
}
|
||||
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
43
Front/skydivelogs-app/src/services/authentication.service.ts
Normal file
43
Front/skydivelogs-app/src/services/authentication.service.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { environment } from '../environments/environment';
|
||||
import { User } from '../models/user';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService {
|
||||
private currentUserSubject: BehaviorSubject<User>;
|
||||
public currentUser: Observable<User>;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
|
||||
this.currentUser = this.currentUserSubject.asObservable();
|
||||
}
|
||||
|
||||
public get currentUserValue(): User {
|
||||
return this.currentUserSubject.value;
|
||||
}
|
||||
|
||||
login(username: string, password: string) {
|
||||
return this.http.post<any>(`${environment.apiUrl}/users/authenticate`, { username, password })
|
||||
.pipe(map(user => {
|
||||
// store user details and basic auth credentials in local storage to keep user logged in between page refreshes
|
||||
user.authdata = window.btoa(username + ':' + password);
|
||||
localStorage.setItem('currentUser', JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
}));
|
||||
}
|
||||
|
||||
logout() {
|
||||
// remove user from local storage to log user out
|
||||
localStorage.removeItem('currentUser');
|
||||
this.currentUserSubject.next(null);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpEvent,
|
||||
HttpRequest,
|
||||
HttpResponse,
|
||||
HttpInterceptor,
|
||||
HttpHandler
|
||||
} from '@angular/common/http';
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { startWith, tap } from 'rxjs/operators';
|
||||
import 'rxjs/add/observable/of';
|
||||
|
||||
import { RequestCache } from './request-cache.service';
|
||||
|
||||
@Injectable()
|
||||
export class CachingInterceptor implements HttpInterceptor {
|
||||
constructor(private cache: RequestCache) { }
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler) {
|
||||
const cachedResponse = this.cache.get(req);
|
||||
return cachedResponse
|
||||
? Observable.of(cachedResponse)
|
||||
: this.sendRequest(req, next);
|
||||
}
|
||||
|
||||
sendRequest(
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
tap(event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
this.cache.put(req, event);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,13 @@ import { DropZoneResp } from '../models/dropzone';
|
||||
export class DropzoneService {
|
||||
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public getListOfDropZones(): Observable<Array<DropZoneResp>> {
|
||||
return this.http
|
||||
.get<Array<DropZoneResp>>(`${environment.urlApi}/api/DropZone`, {
|
||||
.get<Array<DropZoneResp>>(`${environment.apiUrl}/api/DropZone`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -30,7 +30,7 @@ export class DropzoneService {
|
||||
public SetFavoriteDropZone(selectedDz: DropZoneResp): boolean {
|
||||
selectedDz.isFavorite = true;
|
||||
this.http
|
||||
.put(`${environment.urlApi}/api/DropZone/${selectedDz.id}`, selectedDz, {
|
||||
.put(`${environment.apiUrl}/api/DropZone/${selectedDz.id}`, selectedDz, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
@@ -41,7 +41,7 @@ export class DropzoneService {
|
||||
public RemoveFavoriteDropZone(selectedDz: DropZoneResp): boolean {
|
||||
selectedDz.isFavorite = false;
|
||||
this.http
|
||||
.put(`${environment.urlApi}/api/DropZone/${selectedDz.id}`, selectedDz, {
|
||||
.put(`${environment.apiUrl}/api/DropZone/${selectedDz.id}`, selectedDz, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
|
||||
@@ -9,12 +9,12 @@ import { GearResp, GearReq } from '../models/gear';
|
||||
export class GearService {
|
||||
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public getListOfGears(): Observable<Array<GearResp>> {
|
||||
return this.http.get<Array<GearResp>>(`${environment.urlApi}/api/Gear`, {
|
||||
return this.http.get<Array<GearResp>>(`${environment.apiUrl}/api/Gear`, {
|
||||
headers: this.headers
|
||||
});
|
||||
}
|
||||
@@ -38,7 +38,7 @@ export class GearService {
|
||||
};
|
||||
|
||||
this.http
|
||||
.post(`${environment.urlApi}/api/Gear`, bodyNewGear, {
|
||||
.post(`${environment.apiUrl}/api/Gear`, bodyNewGear, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
|
||||
@@ -8,13 +8,13 @@ import { JumpTypeResp } from '../models/jumpType';
|
||||
export class JumpTypeService {
|
||||
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
public getListOfJumpTypes(): Observable<Array<JumpTypeResp>> {
|
||||
return this.http.get<Array<JumpTypeResp>>(
|
||||
`${environment.urlApi}/api/JumpType`,
|
||||
`${environment.apiUrl}/api/JumpType`,
|
||||
{ headers: this.headers }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,12 +10,12 @@ import { JumpResp, JumpReq } from '../models/jump';
|
||||
export class JumpService {
|
||||
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
constructor(private http: HttpClient, private dateService: DateService) { }
|
||||
|
||||
public getListOfJumps(): Observable<Array<JumpResp>> {
|
||||
return this.http.get<Array<JumpResp>>(`${environment.urlApi}/api/Jump`, {
|
||||
return this.http.get<Array<JumpResp>>(`${environment.apiUrl}/api/Jump`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -98,7 +98,7 @@ export class JumpService {
|
||||
};
|
||||
|
||||
this.http
|
||||
.post(`${environment.urlApi}/api/Jump`, bodyNewjump, {
|
||||
.post(`${environment.apiUrl}/api/Jump`, bodyNewjump, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
@Injectable()
|
||||
export class StatsService {
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
'Access-Control-Allow-Origin': environment.apiUrl
|
||||
});
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
@@ -42,7 +42,7 @@ export class StatsService {
|
||||
|
||||
private getSimpleSummary(): Observable<SimpleSummary> {
|
||||
return this.http
|
||||
.get<Array<SimpleSummary>>(`${environment.urlApi}/api/Stats/Simple`, {
|
||||
.get<Array<SimpleSummary>>(`${environment.apiUrl}/api/Stats/Simple`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -55,7 +55,7 @@ export class StatsService {
|
||||
|
||||
private getStatsByDz(): Observable<Array<StatsByDzResp>> {
|
||||
return this.http
|
||||
.get<Array<StatsByDzResp>>(`${environment.urlApi}/api/Stats/ByDz`, {
|
||||
.get<Array<StatsByDzResp>>(`${environment.apiUrl}/api/Stats/ByDz`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -69,7 +69,7 @@ export class StatsService {
|
||||
private getStatsByAircraft(): Observable<Array<StatsByAircraftResp>> {
|
||||
return this.http
|
||||
.get<Array<StatsByAircraftResp>>(
|
||||
`${environment.urlApi}/api/Stats/ByAircraft`,
|
||||
`${environment.apiUrl}/api/Stats/ByAircraft`,
|
||||
{ headers: this.headers }
|
||||
)
|
||||
.pipe(
|
||||
@@ -83,7 +83,7 @@ export class StatsService {
|
||||
private getStatsByJumpType(): Observable<Array<StatsByJumpTypeResp>> {
|
||||
return this.http
|
||||
.get<Array<StatsByJumpTypeResp>>(
|
||||
`${environment.urlApi}/api/Stats/ByJumpType`,
|
||||
`${environment.apiUrl}/api/Stats/ByJumpType`,
|
||||
{ headers: this.headers }
|
||||
)
|
||||
.pipe(
|
||||
@@ -96,7 +96,7 @@ export class StatsService {
|
||||
|
||||
private getStatsByGear(): Observable<Array<StatsByGearResp>> {
|
||||
return this.http
|
||||
.get<Array<StatsByGearResp>>(`${environment.urlApi}/api/Stats/ByGear`, {
|
||||
.get<Array<StatsByGearResp>>(`${environment.apiUrl}/api/Stats/ByGear`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -109,7 +109,7 @@ export class StatsService {
|
||||
|
||||
private getStatsByYear(): Observable<Array<StatsByYearResp>> {
|
||||
return this.http
|
||||
.get<Array<StatsByYearResp>>(`${environment.urlApi}/api/Stats/ByYear`, {
|
||||
.get<Array<StatsByYearResp>>(`${environment.apiUrl}/api/Stats/ByYear`, {
|
||||
headers: this.headers
|
||||
})
|
||||
.pipe(
|
||||
@@ -123,7 +123,7 @@ export class StatsService {
|
||||
private getStatsOfLastYear(): Observable<StatsForLastYearResp> {
|
||||
return this.http
|
||||
.get<StatsForLastYearResp>(
|
||||
`${environment.urlApi}/api/Stats/ForLastYear`,
|
||||
`${environment.apiUrl}/api/Stats/ForLastYear`,
|
||||
{ headers: this.headers }
|
||||
)
|
||||
.pipe(
|
||||
@@ -141,7 +141,7 @@ export class StatsService {
|
||||
private getStatsOfLastMonth(): Observable<StatsForLastMonthResp> {
|
||||
return this.http
|
||||
.get<StatsForLastYearResp>(
|
||||
`${environment.urlApi}/api/Stats/ForLastMonth`,
|
||||
`${environment.apiUrl}/api/Stats/ForLastMonth`,
|
||||
{ headers: this.headers }
|
||||
)
|
||||
.pipe(
|
||||
|
||||
Reference in New Issue
Block a user