I need to pass new HttpHeaders options to my interceptor. Why I want to do this? Because I need to take out the Authorization from the header, because my API doens't accept it for an authentication proccess... I tried to clone it, to delete('Authorization'), and create a new one...
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
userId: StorageUtil.getUserId().toString(),
marinaId: StorageUtil.getMarinaId().toString(),
token: StorageUtil.getMarinaIntegrationKey()})
}
All of these StorageUtil's are working fine.
This is what I get in the log =>
The lazyInit only contains an empty object with name: ""
I also tried the .append(), .set() functions...
const httpOptions = {
headers: new HttpHeaders().set('Content-Type', 'application/json')
.set('userId', StorageUtil.getUserId().toString())
.set('marinaId', StorageUtil.getMarinaId().toString())
.set("token", StorageUtil.getMarinaIntegrationKey())
};
But it gives me this lazyUpdate stuff, and it also doesn't work...
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
this.token = StorageUtil.getToken();
const header = request.headers['lazyUpdate'];
if (this.token && this.token !== '') {
if (
request.url.indexOf('easyworkshop') > 0 ||
request.url.indexOf('localhost') > 0 ||
request.url.includes('192.168.25')
) {
let headers = new HttpHeaders({
Authorization: 'Bearer ' + this.token,
marinaId: '' + StorageUtil.getMarinaId(),
userId: '' + StorageUtil.getUserId(),
integrationKey: '' + StorageUtil.getMarinaIntegrationKey(),
OriginEasy: 'EasyWeb',
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json',
});
if (header && header.length > 0) {
const contentTypes = header.filter(
(x) => x.name === 'Content-Type' || x.name === 'Accept'
);
if (contentTypes.length > 0) {
headers = new HttpHeaders({
Authorization: 'Bearer ' + this.token,
MarinaId: '' + StorageUtil.getMarinaId(),
UserId: '' + StorageUtil.getUserId(),
integrationKey: '' + StorageUtil.getMarinaIntegrationKey(),
OriginEasy: 'EasyWeb',
'Access-Control-Allow-Origin': '*',
});
}
}
console.log("linha 65")
console.log(request)
request = request.clone({headers});
console.log("linha 68")
console.log(request)
}
}
return next.handle(request).pipe(
filter(
(event: any) => {
if (event instanceof HttpResponse) {
const token = JSON.parse(JSON.stringify(event.body.token));
console.log("Bateu aqui 1 ")
if (token && token.length > 0) {
StorageUtil.saveToken(token);
}
}
return event;
},
async (err: any) => {
if (err.status === 0) {
this.messageUtil.generateMessage(
'error',
'SUMMARY.ERROR',
'Ocorreu uma falha de comunicação com o servidor.',
3000
);
return;
}
}
),
catchError((error: HttpErrorResponse) => {
if (error.status === 401 && !this.swal) {
if (
error.url.includes('authenticate') //TODO criar uma pagina de roteamento onde ira levar o usuario informando que a sessão expirou, ele deve logar novamento pelo easy marine. Apresentar um Swal como mensagem
) {
throw error;
}
const token = StorageUtil.getToken();
console.log("Linha 104")
const headers = new HttpHeaders({
'Content-Type': 'application/json',
userId: StorageUtil.getUserId().toString(),
marinaId: StorageUtil.getMarinaId().toString(),
token: StorageUtil.getMarinaIntegrationKey()
});
console.log(headers);
request = request.clone({headers})
console.log(request)
if (!token) {
return this.http
.post(
environment.apiHost + '/authentication/login',
'',
{headers: headers}
)
.pipe(mergeMap((resp: any) => {
try {
console.log("Bateu aqui??")
const token = JSON.parse(JSON.stringify(resp.data.token));
if (token) {
StorageUtil.saveToken(token);
const reqConst = request.clone({
setHeaders: {
Authorization: 'Bearer ' + token,
},
});
return next.handle(reqConst);
}
} catch (error) {
this.swal = Swal.fire('WARNING', 'Sua sessão expirou.', 'warning');
}
return throwError(error);
})
);
} else {
this.swal = Swal.fire('WARNING', 'Sua sessão expirou.', 'warning');
}
}
throw error;
})
);

