HttpInterceptorは、トークン注入・エラー処理・ローディング状態などの横断的関心事を処理するAngularの最適な場所です。
認証インターセプター
typescript
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(
req: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
const token = this.auth.getToken();
if (!token) return next.handle(req);
const authReq = req.clone({
headers: req.headers.set("Authorization", `Bearer ${token}`),
});
return next.handle(authReq);
}
}
エラーハンドリングインターセプター
typescript
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
return next.handle(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
this.router.navigate(["/login"]);
}
return throwError(() => error);
}),
);
}
}
インターセプターの登録
typescript
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }
]
})
インターセプターチェーンは登録順に実行されます。multi: trueにより複数のインターセプターを共存させることができます。