Skip to content

Commit

Permalink
Interceptor para incluir token nas requisicoes
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscolopes committed May 5, 2018
1 parent 923eec1 commit e2266e0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ErrorInterceptorProvider } from '../interceptors/error-interceptor';
import { AuthService } from '../services/auth.service';
import { StorageService } from '../services/storage.service';
import { ClienteService } from '../services/domain/cliente.service';
import { AuthInterceptorProvider } from '../interceptors/auth-interceptor';

@NgModule({
declarations: [
Expand All @@ -31,6 +32,7 @@ import { ClienteService } from '../services/domain/cliente.service';
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
CategoriaService,
AuthInterceptorProvider,
ErrorInterceptorProvider,
AuthService,
StorageService,
Expand Down
34 changes: 34 additions & 0 deletions src/interceptors/auth-interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HTTP_INTERCEPTORS } from '@angular/common/http';
import { Observable } from 'rxjs/Rx'; // IMPORTANTE: IMPORT ATUALIZADO
import { StorageService } from '../services/storage.service';
import { API_CONFIG } from '../config/api.config';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

constructor(public storage: StorageService) {
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

let localUser = this.storage.getLocalUser();

let N = API_CONFIG.baseUrl.length;
let requestToAPI = req.url.substring(0, N) == API_CONFIG.baseUrl;

if (localUser && requestToAPI) {
const authReq = req.clone({headers: req.headers.set('Authorization', 'Bearer ' + localUser.token)});
return next.handle(authReq);
}
else {
return next.handle(req);
}
}
}

export const AuthInterceptorProvider = {
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
};
1 change: 0 additions & 1 deletion src/interceptors/error-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Observable } from 'rxjs/Rx'; // IMPORTANTE: IMPORT ATUALIZADO
export class ErrorInterceptor implements HttpInterceptor {

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log("Passou no interceptor");
return next.handle(req)
.catch((error, caught) => {

Expand Down
8 changes: 1 addition & 7 deletions src/services/domain/cliente.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ export class ClienteService {
}

findByEmail(email: string) : Observable<ClienteDTO> {

let token = this.storage.getLocalUser().token;
let authHeader = new HttpHeaders({'Authorization': 'Bearer ' + token});

return this.http.get<ClienteDTO>(
`${API_CONFIG.baseUrl}/clientes/email?value=${email}`,
{'headers': authHeader});
return this.http.get<ClienteDTO>(`${API_CONFIG.baseUrl}/clientes/email?value=${email}`);
}

getImageFromBucket(id : string) : Observable<any> {
Expand Down

0 comments on commit e2266e0

Please sign in to comment.