Hi I'm stuck with simple issue.
The objective for my code is to handle 401 http response and navigate back to login screen.
The following is my base class
@Injectable({
providedIn: 'root',
})
export class BaseService {
constructor(public router: Router, public snakeBar: MatSnackBar) {}
handleError(errorResponse: HttpErrorResponse) {
if (errorResponse instanceof HttpErrorResponse) {
if (errorResponse.status == 401) {
this.router.navigate(['auth/login']);
this.snakeBar.open('Session expired, Please login again', 'OK', {
duration: 3000,
});
}
}
}
}
The following is one of the service which extends the base service.
@Injectable({
providedIn: 'root',
})
export class SettingsService extends BaseService {
constructor(
private https: HttpClient
) {
super();
}
getSomeData(): Observable<any> {
const { ctoken } = localStorage;
const httpOptions = {
headers: new HttpHeaders({
Authorization: `bearer ${ctoken}`,
}),
};
return this.https
.get(
`someUrl`,
httpOptions
)
.pipe(catchError(this.handleError));
}
}
As per code standard i have to send two parameter to my parent, both router and snack bar.
1st issue = Im extending the base serive in 10+ service, it's difficult to send from all extended class. 2nd issue = Even after sending super(router, matSnackBar). Im getting error router and snackbar is undefined in base service.
Can anybody explain how to solve this issue.
Im using angular 10