<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: process20</title>
    <description>The latest articles on DEV Community by process20 (@process).</description>
    <link>https://dev.to/process</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1080731%2F3076880f-b467-4af8-90c2-59dd8c8dd2ea.png</url>
      <title>DEV Community: process20</title>
      <link>https://dev.to/process</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/process"/>
    <language>en</language>
    <item>
      <title>Implementing the interceptors</title>
      <dc:creator>process20</dc:creator>
      <pubDate>Thu, 11 May 2023 09:55:35 +0000</pubDate>
      <link>https://dev.to/process/implementing-the-interceptors-10h6</link>
      <guid>https://dev.to/process/implementing-the-interceptors-10h6</guid>
      <description>&lt;p&gt;Hi,&lt;br&gt;
I have a problem with interceptors, now I'm learning JWT reaching the step dealing with interceptors, I'm not able to know the source of the issue, the interceptor is getting block the login API to be consumed and generate the token that will be injected in request header in the interceptor; here the code of the interceptor class:&lt;/p&gt;

&lt;p&gt;import { catchError } from 'rxjs/operators';&lt;br&gt;
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from "@angular/common/http";&lt;br&gt;
import { Injectable } from '@angular/core';&lt;br&gt;
import { tap } from 'rxjs/operators';&lt;br&gt;
import { Router } from "@angular/router";&lt;br&gt;
import { Observable, throwError } from 'rxjs';&lt;/p&gt;

&lt;p&gt;@Injectable()&lt;br&gt;
export class AuthInterceptor implements HttpInterceptor {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(){}

intercept(req: HttpRequest&amp;lt;any&amp;gt;, next: HttpHandler): Observable&amp;lt;HttpEvent&amp;lt;any&amp;gt;&amp;gt;{
      console.log("you are in interceptor");
      // get the token from the local storage
      const token = localStorage.getItem('token');
      if(token){
        console.log("token stored to the local store: "+token);
        // modify the request header
        req = req.clone({
          setHeaders: {Authorization: `${token}`}
        });
        // handling errors
        return next.handle(req).pipe(catchError(err=&amp;gt;{
          console.log("something wrong in interceptor");
          if(err instanceof HttpErrorResponse){
             console.log(err.status + ': token expired, login again');
          }
          return throwError(()=&amp;gt; new Error('Some Unknown Error'));
        }));
      }
      else console.log("token not provided");

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;if you would like the full code using Node.js and angular, here is the link on angular: &lt;a href="https://github.com/process20/JWT-Implementation"&gt;https://github.com/process20/JWT-Implementation&lt;/a&gt;.&lt;br&gt;
Hope you to help me fixing this issue, I'm struggling for more than 10 days.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
