NestJS Interceptors

Поділитися
Вставка
  • Опубліковано 18 вер 2024
  • Support the Channel:
    Become a Member: www.youtube.co...
    Become a Patreon: / stuyy
    Buy me a Coffee: ko-fi.com/anson
    Donate on Streamlabs: streamlabs.com...
    Donate on PayPal: paypal.me/anso...
    Connect with me:
    Twitter: / ansonthedev
    Discord: / discord
    GitHub: github.com/stuyy
    Twitch: / stuy

КОМЕНТАРІ • 17

  • @allessms
    @allessms 7 місяців тому +1

    thank you Anson! it's a clear explanation!

  • @malamhari_
    @malamhari_ 3 місяці тому

    It really helps me, by any means A LOT.
    Thank you sir

  • @mustafa-ahmed-dev
    @mustafa-ahmed-dev 2 місяці тому

    Amazing explanation, keep up the good work
    Thank you

  • @bugraotken
    @bugraotken 9 місяців тому

    Thank you Anson! Maybe you can add this video inside your Nest.js tutorials playlist?

    • @ansonthedev
      @ansonthedev  9 місяців тому

      It's already in the playlist. Thanks for checking.

  • @glebbondarenko67
    @glebbondarenko67 2 місяці тому

    Cool videos about NestJS, I have no idea why you have so few subscribers

  • @NameNotFound444
    @NameNotFound444 Місяць тому

    awesome explaination ! But i have a question, what is the difference between Interceptor, Middleware, Pipe, Guard ? That always makes me confused :(( Hope you guys can explain it.

    • @user-gv5rx9sq3y
      @user-gv5rx9sq3y Місяць тому +1

      Interceptors control the responses from your routes after the controller functions were executed. Middleware catches the request and can log it somewhere if needed or modify it, for example it can add req.user into your request object that will be the User instance from DB if the user is authenticated. Guards decide if a request should be blocked, f.e. if the endpoint is for authenticated users or admins only. And pipes can transform the data sent by client or validate it, f.e. client sends id “3” as a parameter and as that is supposed to be a number, it will change the id param to 3 (as a number). Or if the id is not a number at all, it will block the request and send a corresponding response to the client. Pipes and guards are basically the same middlewares, but u separate these types for easier coding experience and because that’s NestJS)

    • @NameNotFound444
      @NameNotFound444 Місяць тому

      @@user-gv5rx9sq3y thank you so much. Wish you a good day

  • @dmsnm
    @dmsnm 9 місяців тому +1

    So would this be a good way to intercept for authentication state if implemented without an external library? Like check for validity of access token from request header/cookie?
    How is this different from express middleware?

    • @ansonthedev
      @ansonthedev  9 місяців тому +3

      Middleware is called first, then guards, then the interceptor. It can be a bit confusing on where things should go. Generally, it really is up to you how you want to handle where you want certain logic to happen. You can check the request header in a middleware, perform some logic, then pass it to a Guard, the guard can validate the request and grant access or reject if certain conditions are not met, then after the Guard, the Interceptor will be invoked if the Guard passes, at this point you have the implication that the user is authenticated, so you could do stuff like logging, emit an event to allow an async process occur, etc.
      Generally, I would use interceptors only if I have a need to transform the response. As there are many options to perform logic before the request hits the Route Handler in the Controller. For example, we filtered out all passwords from the Users array before we sent the response back to the client. This would allow you to separate concerns between each layer, making it MUCH easier to write Unit Tests, and helps you dig into the layer that is causing any errors, bugs, etc. while E2E testing or manual testing as a user.
      So in short: Use Interceptors if you need to transform the response back to the client. Otherwise, there are likely better options for what you are needing to do. Hope this helps.

    • @dmsnm
      @dmsnm 9 місяців тому

      @@ansonthedev Thanks

    • @ericpeng6128
      @ericpeng6128 3 місяці тому

      @@ansonthedev Thanks for the explanation! I was wondering about the necessity of using intercepter this whole video, lol.

  • @trentcox9239
    @trentcox9239 8 місяців тому

    loving your channel mate! is there any way you can get around to showing how to implement auth between a nextjs app and a nestjs api? its a god damn information minefield out there trying to make heads or tails of how to do it properly, at least until auth.js finally releases support for express.js...

  • @thanhduynguyen6253
    @thanhduynguyen6253 8 місяців тому

    Tks Anson, could you make video about jwt and accessToken and refreshToken with Nestjs

    • @PatalJunior
      @PatalJunior 8 місяців тому

      Would love to see a video on it.

  • @abdelbassetchettouh7416
    @abdelbassetchettouh7416 2 місяці тому

    i couldn't find the github repo
    i'm that kind of guy that understands with practice
    pls response ASAP