Angular HTTP Interceptors
HTTP interceptors in Angular are a powerful tool for handling HTTP requests and responses. They allow you to modify the request or response before they are sent or received, and can be used for a variety of tasks such as adding authentication headers, handling errors, or logging requests.
Interceptors are implemented using the HttpInterceptor
interface in Angular. You can create a new interceptor by implementing this interface and providing it to the HTTP_INTERCEPTORS
multi-provider in your application module.
Creating an Interceptor
To create a new interceptor, you first need to create a class that implements the HttpInterceptor
interface. This interface requires you to implement a intercept
method that takes an HttpRequest
object and a HttpHandler
object as parameters.
Within the intercept
method, you can modify the request, for example by adding headers or changing the URL, and then pass the modified request to the handle
method of the HttpHandler
object to send the request to the server.
Registering the Interceptor
Once you have created your interceptor class, you need to register it with the application module. You can do this by providing it to the HTTP_INTERCEPTORS
multi-provider, which allows you to provide multiple interceptors to the application.
Here’s an example of how you might register an interceptor in your application module:
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { MyInterceptor } from './my-interceptor';
@NgModule({
...
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: MyInterceptor, multi: true }
],
...
})
Using Interceptors
Once you have registered an interceptor, it will automatically be called for every HTTP request made by your application. This allows you to centralize logic for handling requests and responses, and makes it easy to implement cross-cutting concerns such as logging or authentication.
Overall, HTTP interceptors in Angular are a powerful tool for managing HTTP requests and responses in your application. They allow you to centralize logic for handling requests and responses, and provide a flexible way to modify and augment HTTP requests and responses.
Can you spot the bug in the code that returns the expiration time? Let me know in the comments.
Source code available at: https://github.com/JasperKent/angular-authentication
Server available at: https://github.com/JasperKent/WebApi-Authentication
Remember to subscribe at http://www.youtube.com/channel/UCqWQzlUDdllnLmtgfSgYTCA?sub_confirmation=1
And if you liked the video, click the 👍.
Colonel sanders, is that you 💀
This video is so well explained, it helped in my job, thank u so much💯
What an absolute gem of a video!! Excellent explained, simple and clean example. Thank you!
i can tell you are an oldschool programmer by your ide theme
Amazing video! As always. Thank you so much for the work you're doing
Most underrated YouTube coding channel!
I truly appreciate the effort you put into creating such golden content. Keep up the fantastic work! 🙏😊Thanks!
Your videos are fantastic. Well done. You should have a much larger subscription base and view counts. Please keep it up.
With different branches on ur GitHub it would have been easier to follow along with the videos from initial to final state. I have not tried angular and typescript. But all languages concept seem similar only syntactically different. Did not know TS looks like sibling of c#
is this language type script?
Informative 😊