Angular-5-Login-and-Logout-in-Web-API-Using-Token-Based-Authentication icon indicating copy to clipboard operation
Angular-5-Login-and-Logout-in-Web-API-Using-Token-Based-Authentication copied to clipboard

My problem is : Property 'do' does not exist on type 'Observable<HttpEvent<any>>

Open HassanKhaleghirad opened this issue 7 years ago • 4 comments

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

HassanKhaleghirad avatar Jul 03 '18 06:07 HassanKhaleghirad

'do' has been changed to 'tap' in angular 6.Also you should use piping instead of chaining.

ashutoshgupta0088 avatar Jun 14 '19 07:06 ashutoshgupta0088

Ok thanks. Can you help me how can I pipeline it?

HassanKhaleghirad avatar Jun 14 '19 13:06 HassanKhaleghirad

`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

ryanking44 avatar Feb 19 '20 03:02 ryanking44

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 .

HassanKhaleghirad avatar Feb 29 '20 06:02 HassanKhaleghirad