My problem is : Property 'do' does not exist on type 'Observable<HttpEvent<any>>
https://github.com/DotnetMob/Angular-5-Login-and-Logout-in-Web-API-Using-Token-Based-Authentication/blob/a88b2bb57e0c03f2a31f1477cd68faa18d0a82d9/Angular5/src/app/auth/auth.interceptor.ts#L22
'do' has been changed to 'tap' in angular 6.Also you should use piping instead of chaining.
Ok thanks. Can you help me how can I pipeline it?
`import { HttpInterceptor, HttpRequest, HttpHandler, HttpUserEvent, HttpEvent } from "@angular/common/http"; import { Observable } from "rxjs"; import { } from 'rxjs/add/operator/do'; import { tap } from 'rxjs/operators'; import { Injectable } from "@angular/core"; import { Router } from "@angular/router";
@Injectable() export class AuthInterceptor implements HttpInterceptor {
constructor(private router: Router) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.get('No-Auth') == "True")
return next.handle(req.clone());
if (localStorage.getItem('userToken') != null) {
const clonedreq = req.clone({
headers: req.headers.set("Authorization", "Bearer " + localStorage.getItem('userToken'))
});
return next.handle(clonedreq).pipe(
tap((err: any) => {
if (err.status === 401)
this.router.navigateByUrl('/login');
})
);
}
else {
this.router.navigateByUrl('/login');
}
}
}`
Little late to the party here - but here is the code updated with the tap pipe
Thank You for response.
On Wed, Feb 19, 2020 at 7:14 AM ryanking44 [email protected] wrote:
`import { HttpInterceptor, HttpRequest, HttpHandler, HttpUserEvent, HttpEvent } from "@angular/common/http"; import { Observable } from "rxjs"; import { } from 'rxjs/add/operator/do'; import { tap } from 'rxjs/operators'; import { Injectable } from "@angular/core"; import { Router } from "@angular/router";
@Injectable https://github.com/Injectable() export class AuthInterceptor implements HttpInterceptor {
constructor(private router: Router) { }
intercept(req: HttpRequest
, next: HttpHandler): Observable<HttpEvent > { if (req.headers.get('No-Auth') == "True") return next.handle(req.clone()); if (localStorage.getItem('userToken') != null) { const clonedreq = req.clone({ headers: req.headers.set("Authorization", "Bearer " + localStorage.getItem('userToken')) }); return next.handle(clonedreq).pipe( tap((err: any) => { if (err.status === 401) this.router.navigateByUrl('/login'); }) ); } else { this.router.navigateByUrl('/login'); }}
}`
Little late to the party here - but here is the code updated with the tap pipe
— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/CodAffection/Angular-5-Login-and-Logout-in-Web-API-Using-Token-Based-Authentication/issues/2?email_source=notifications&email_token=AIIYGIF2NVHHAF4JTW3UP4LRDSTIDA5CNFSM4FIBPEWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMGHV5Q#issuecomment-588020470, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIIYGIGBCUENZU4CCOKC423RDSTIDANCNFSM4FIBPEWA .