I’m getting an error: Function lacks ending return statement and return type does not include ‘undefined’.
import { HttpInterceptor, HttpRequest, HttpHandler, HttpUserEvent, HttpEvent } from "@angular/common/http";
import { Observable } from "rxjs";
import { LoginService } from "../services/login.service";
import 'rxjs/add/operator/do';
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>> { // here error
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)
.do(
succ => { },
err => {
if (err.status === 401)
this.router.navigateByUrl('/signin');
}
);
}
else {
this.router.navigateByUrl('/sigin');
}
}
}
I’ve tried do it with try but I’m still getting the same error.
Does anyone knows how to make it work?
Source: Angular Questions