Merge pull request #49 from AlineRinquin/blandine

interceptor et guards
This commit is contained in:
AlineRinquin 2022-02-25 10:56:56 +01:00 committed by GitHub
commit 9e40a5ff68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 25 deletions

View file

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AuthInterceptor } from './auth.interceptor';
describe('AuthInterceptor', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
AuthInterceptor
]
}));
it('should be created', () => {
const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor);
expect(interceptor).toBeTruthy();
});
});

View file

@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { TokenService } from './token.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private tokenService: TokenService) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
console.log('Coucou je suis le videur ! ');
const token = this.tokenService.getToken();
if(token) {
const authReq = request.clone(
{
headers : request.headers.set('Authorization', `Bearer ${token}`)
}
)
return next.handle(authReq);
} else {
return next.handle(request);
}
}
}

View file

@ -14,7 +14,7 @@ export class AuthService {
private tokenKey: string;
constructor(private http: HttpClient) {
// On se sert des variables d'environnement de notre application
this.apiUrl = environment.apiUrl;
this.tokenKey = environment.tokenKey;
}
@ -34,19 +34,14 @@ export class AuthService {
};
console.log('Mon body : ', body);
// Modifier cette partie ci-dessous :
// - pour pouvoir stocker dans le localstorage notre accesstoken
// - Sous la clé "TOKEN-LBP"
return this.http.post(`${this.apiUrl}/membres/sign-in`, body).pipe(
map((x: any) => {
console.log('Service : ', x.token);
// Modification à faire ici
localStorage.setItem(this.tokenKey, x.token);
return x; // permet de renvoyer la réponse à l'initiateur (page Signin) après le traitement du map
})
);
}
forgotPassword(email: string): Observable<any> {