Is this the best NodeJS Authorization Library? | CASL JavaScript Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ต.ค. 2024
  • In this video we take a look at a fairly popular authorization library: CASL. You can use this solution both on the server side, perhaps on a Node JS Express API, and on the client side like in React or Vue JS. We'll discuss the basics of how to use and how to define abilities for a given user's permission in an application.

ความคิดเห็น • 91

  • @mariusespejo
    @mariusespejo  2 ปีที่แล้ว +28

    Note: if you're using the newest version of CASL the Ability class is deprecated, instead you now have to use createMongoAbility
    const { can, cannot, build } = new AbilityBuilder(createMongoAbility);

  • @VijayVerma-dq2bu
    @VijayVerma-dq2bu ปีที่แล้ว +3

    Very simple yes detailed enough explanation. You saved me multiple hours. Thank you 😊

  • @Herna14W
    @Herna14W 2 ปีที่แล้ว +6

    This is amazing!, thank you so much for sharing the knowledge!, It would be good to see an example of how to use this with react, and render different UI content based on roles.

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +3

      Might make a video on it when I get a chance, but pretty much as long as you understand the fundamentals from this video regarding how to define abilities, then you can just use the official react casl package which let’s you do “can” checks using a component: casl.js.org/v5/en/package/casl-react/

  • @MrBatocko93
    @MrBatocko93 2 ปีที่แล้ว +8

    All tuned up for the dynamic permissions in Nest, official docs are a bit unclear on that regard. Keep up the good work, dude.

  • @mgan59
    @mgan59 2 ปีที่แล้ว +6

    Great overview of the library! Curious how did you get the repl behavior with the inline errors etc inside of your code editor? Is that a plug-in?

  • @foofighterdaz
    @foofighterdaz 2 ปีที่แล้ว

    Been looking forward to this one Marius!

  • @pranjalsonigaming
    @pranjalsonigaming 2 ปีที่แล้ว +1

    thank you, looking forward for the next one 🔥

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว

      🙌

    • @kitemafrank968
      @kitemafrank968 2 หลายเดือนก่อน

      I am also looking forward for a nextjs one.

  • @mkroven
    @mkroven 2 ปีที่แล้ว

    Thank you. Waiting for nestjs usage.

  • @DioArsya
    @DioArsya 2 ปีที่แล้ว +2

    cool! after nestjs, the one is express with this cacl is nice btw

  • @codesuit
    @codesuit 2 ปีที่แล้ว +3

    Thanks bro, waiting for nestjs usage too ❤️What if you additionally connect a database to better understand the communication between casl-nestjs and a db?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +2

      Will see what I can do!

    • @lutfiikbalmajid3128
      @lutfiikbalmajid3128 2 ปีที่แล้ว +1

      thats cool, but maybe it can be work with JWT, as JWT return username too, just add if username "admin" role read & write.

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      Yes it can work with any authentication strategy, for you to really be able to do authorization you ideally already solved authentication and JWTs might be part of that solution.

  • @guhkunpatata3150
    @guhkunpatata3150 2 ปีที่แล้ว

    as always, great explanation .... thanks for the vid

  • @GustavoCardoso
    @GustavoCardoso 2 ปีที่แล้ว

    Thanks, great content. I'll start learning but this vídeo was a great start point.

  • @sksflier
    @sksflier 2 ปีที่แล้ว +1

    Great video and explanation. Could you explain how can we manage dynamic assignment and un assignment? E.g. user was give access to edit Post from the frontend by admin and then revoked?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว

      Good question, I think I forgot to mention it but you can also do ability.update(rules). You can use that in addition to using the JSON builder api, that means your rules needs to be saved in the database instead of being statically defined. Initialize the ability by pulling the rules from the database, and when an admin updates it you just need a mechanism to make sure clients are updated either by updating their sessions or perhaps triggering an ability.update(newRules) somehow

  • @isurumaldeniya9536
    @isurumaldeniya9536 2 ปีที่แล้ว

    Wonderful video thank you very much. What is your vs code setup?

  • @timothymm
    @timothymm 2 ปีที่แล้ว

    Yet another clear, concise, well delivered video. Really appreciate your time, Marius!

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      Thanks for stopping by to comment!

  • @bablukpik
    @bablukpik 2 ปีที่แล้ว +2

    Thanks dude for this awesome tutorial. Please also cover the following:
    1. @casl/prisma
    2. @casl/react

  • @junaid_arif
    @junaid_arif ปีที่แล้ว

    great and simple explaination bro
    thanks alot bro😇😇

  • @raymondcaddick6208
    @raymondcaddick6208 ปีที่แล้ว

    Great tutorial!

  • @durgadaskamath
    @durgadaskamath 2 ปีที่แล้ว +1

    Interesting ! btw, what plugin have you installed to get those indicated on valid / invalid statements , declarations ?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +8

      Like the true/false value in the editor? I used the quokka extension, very useful for demos like this

    • @durgadaskamath
      @durgadaskamath 2 ปีที่แล้ว

      @@mariusespejo yea, thanks.

  • @lucasfontesgaspareto
    @lucasfontesgaspareto ปีที่แล้ว

    Learning so much... thanks

  • @greensquarecinema
    @greensquarecinema 2 ปีที่แล้ว +1

    Class Ability has been deprecated, it says to use createMongoAbility function instead, but no much info about it when googling.

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว

      that's correct. The docs itself explains it. Note it has nothing to do with using MongoDB, just that conditions are using some syntax inspired by mongo, hence the name

  • @DragosBurciu
    @DragosBurciu ปีที่แล้ว

    Would love a cover video about casl vue

  • @bzsfps
    @bzsfps 2 ปีที่แล้ว

    Amazin video, can u pls do a tutorial teaching how to use this in the frontend with ReactJS?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      Yeah maybe when I get a chance. It’s honestly not much more to learn, basically take the fundamentals from this video and apply it to UI authorization. Basically make things visually disabled/hidden/locked if user doesn’t have the ability

  • @AngelPuc
    @AngelPuc 2 ปีที่แล้ว

    Thanks for sharing bro! Let me ask you something, in your opinion, which is better keycloak or castle?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      I haven’t tried keycloak but it looks like it’s more for authentication? So that would be an apple’s to oranges comparison

    • @AngelPuc
      @AngelPuc 2 ปีที่แล้ว

      @@mariusespejo I've been trying keycloak from 5 months ago, Keycloak works for both Authorization and Authentication. I liked castle, I read some information about castle before but I didn't try it, it looks like easy thanks to you. (Sorry for my English).

  • @burakkaratay1136
    @burakkaratay1136 2 ปีที่แล้ว

    Great Content!

  • @sushilbalami
    @sushilbalami 2 ปีที่แล้ว

    finally ! Thanks Dude!

  • @jsnagaraj
    @jsnagaraj 2 ปีที่แล้ว

    How can we archive this for REST API? how to read the subject and actions from API requests?, is it expected from API requests?

  • @ifacro
    @ifacro 2 ปีที่แล้ว

    I'd love you to cover casl/vue in a following video. I'm having some troubles implementing it and I'd really appreciate it

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      I’ll try to cover it when I get a chance. Have you tried the official casl/vue package?

    • @ifacro
      @ifacro 2 ปีที่แล้ว

      @@mariusespejo Hello! Thanks for replying so fast, wow.
      Yes, I'm using the official casl/vue package, but for some reason, the $ability plugin is always 'undefined', even when it shows that it's correctly installed (the linter recognizes it)

  • @sahukarisantosh4655
    @sahukarisantosh4655 2 ปีที่แล้ว +1

    please do video on case--react

  • @nikkobryandelosreyes135
    @nikkobryandelosreyes135 2 ปีที่แล้ว

    Thanks bro, what plugin you are using when you type is has an inline suggestions?

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      that’s just a vs code setting I think, I don’t remember installing a plugin for that

  • @avimehenwal
    @avimehenwal 2 ปีที่แล้ว

    wonderful wonderfule video :D Do you happen to have any code-repo (preferrably open sourced and MIT) with express.js that I can use a referencce to see how casl works with backends ? Super thanks for such a amzing video :)

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      I don’t sorry! I do have a video on using casl with nestjs though if that helps

    • @avimehenwal
      @avimehenwal 2 ปีที่แล้ว

      @@mariusespejo Thankyou for the reply. Sure I am gonna search it out on your YT channel. Lotss of great videos here :D thankyou for sharing

  • @scorshek
    @scorshek ปีที่แล้ว

    great video

  • @anasouardini
    @anasouardini ปีที่แล้ว

    What's the point? privs are typically stored in DB and the server who decides what and when, the client is supposed to only receive and visualize permissions.

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว

      It’s just a tool to help with that, I thought that I had explained that in the video

    • @anasouardini
      @anasouardini ปีที่แล้ว

      @@mariusespejo I'm genuinely confused; you didn't connect all of those parts together.
      I guess I'll take a look at the docs.

    • @anasouardini
      @anasouardini ปีที่แล้ว

      @@mariusespejo My simple apps are often not perfect enough to benefit from this, given the way I understand it currently, still trying to make sense of it though.
      Maybe I'm doing authorization wrong!!
      I often have to combine multiple conditions to make sure I'm covering all edge cases, some of those conditions are not a simple read, write, etc
      [edit]
      I'm starting to grasp why it exists, but still confused as to the very little characters it saves you from typing!
      Though I like the consistency of the syntax, the only thing I think I lack in the way I implement authorization. This lib can serve as a template.

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว +1

      Alright, let me try to fill in some gaps. There are several ways to do authorization. You could have something as simple as RBAC (role based access control) where you simply assign a basic role to a user, e.g. admins can do special stuff, general users can do basic stuff, etc. Simple apps can get away with just that alone, and your authorization checks server-side would simply need to check roles to protect things. However that doesn’t scale very well. What happens when you start having more roles? What happens when each role start having a mix of different privileges? I’m sure you can imagine without any other tool you’d end up with several if statements in your code to satisfy all your requirements. But then what happens when your requirements change as they often do in the real world? Then you have to hunt down all those auth checks and make sure they’re all still valid.
      CASL provides you with one possible solution to be able to represent all your authorization rules in a single file. There is one source of truth. The video should explain that part well, where it has a nice basic api that lets you define given a user and a subject what can or cannot be done. In addition to that in even more complex scenarios you can add conditions, e.g. a user can only edit a post if they are the author. For a lot of apps where the roles and privileges are static, this file-based approach is enough. In an even more complex dynamic privileges scenario where privileges can be modified per user, you can instead save that ability representation as json in a database, and CASL can instantiate an auth “checker” based on that saved json.
      Now that checker basically encapsulates all the info you need to check if a user can do X given Y conditions, across your entire codebase whether that’s sever or client side, CASL allows you to have a unified API/utility. It seems you’re making an assumption that authorization checks can/should only happen server-side but that’s not always the case. It depends on the use case. Not something I’m going to debate in a comment.
      Anyways so what’s the point? It’s just a lightweight tool, that helps you with authorization/ability representation and a util for checking at runtime.

    • @scorshek
      @scorshek ปีที่แล้ว

      @@mariusespejo great explanation buddy

  • @ashishkumari-yg4gn
    @ashishkumari-yg4gn 7 หลายเดือนก่อน

    Thank you

    • @mariusespejo
      @mariusespejo  7 หลายเดือนก่อน +1

      You’re welcome!

  • @parasarora5869
    @parasarora5869 ปีที่แล้ว

    How can we persist these abilities as roles n permissions in db?? 🤔... What will that setup look like

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว

      You can initialize an ability from json, meaning you can represent those permissions in the db using a similar schema that the json requires: casl.js.org/v5/en/guide/define-rules#json-objects
      So basically at the point when you create an ability you’ll need to add a db query. If you use sessions then you only need to do that at login, unless you want to check each request

  • @thangtraninh5857
    @thangtraninh5857 ปีที่แล้ว

    with json:
    import { createMongoAbility } from '@casl/ability';
    export default createMongoAbility([
    {
    action: 'read',
    subject: 'Post'
    },
    {
    inverted: true,
    action: 'delete',
    subject: 'Post',
    conditions: { published: true }
    }
    ])
    how to if want conditions such as authorId === userId?

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว

      You already have an example of conditions in your snippet there, e.g. you might do conditions: { authorId: 5 }

    • @thangtraninh5857
      @thangtraninh5857 ปีที่แล้ว

      @@mariusespejo but it store in database and cannot fixed authorId, it's dynamic based on request from client. Can you help me?

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว

      Then just store it as string “userId” and make sure to replace it with the correct value at the time that you create the ability

  • @ohwell1832
    @ohwell1832 ปีที่แล้ว

    Hello I'm new to programming. I know firebase auth / database and nextjs. Can I use firebase auth with this? Thanks sir

    • @mariusespejo
      @mariusespejo  ปีที่แล้ว

      Hey Ohwell, hope you’re enjoying programming. If you’re not familiar I recommend reading about the differences between authentication and authorization. I’m not familiar with firebase auth but I’m going to assume that’s authentication. This video covers one way to do authorization, which would happen after the user is already logged in (authenticated). It doesn’t care about how you achieved that authentication, so I would assume it would work with any authentication strategy

    • @ohwell1832
      @ohwell1832 ปีที่แล้ว

      ​@@mariusespejo Hello, yes its firebase authentication. Thanks for your input. I think it will work. The way you explained in the video is clear for me. Thanks!

  • @longnhat6405
    @longnhat6405 2 ปีที่แล้ว

    hay lắm anh, em cảm ơn nhiều

  • @nishanthakumara8472
    @nishanthakumara8472 2 ปีที่แล้ว

    Thanks. better if you can do the video about the Redux saga.

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      Redux saga is a bit overkill nowadays in my opinion. Check out react-query, there’s video in my channel. Once you realize how much that simplifies managing state from your API calls you’ll likely also realize how unnecessarily complex sagas are

    • @nishanthakumara8472
      @nishanthakumara8472 2 ปีที่แล้ว

      @@mariusespejo Sure. thanks 🙂

  • @lifeok6188
    @lifeok6188 2 ปีที่แล้ว

    Integration with Nestjs would be great

  • @akhileshway9076
    @akhileshway9076 ปีที่แล้ว

    Hi friend can you make a video on saga in nestjs, DDD in nestjs

  • @reyhanehmoayeri7564
    @reyhanehmoayeri7564 2 ปีที่แล้ว +1

    casl Vue

  • @rajnarayan9558
    @rajnarayan9558 2 ปีที่แล้ว

    Hello sir can you share your vs code theme or font family please

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +1

      I used github dark for this one!

  • @connieleung96
    @connieleung96 2 ปีที่แล้ว

    nestjs recommends it too

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +6

      Yes! I plan on covering that integration in a follow up video

    • @GagikHarutyunyan_dev
      @GagikHarutyunyan_dev 2 ปีที่แล้ว

      @@mariusespejo loocking forward to it

  • @sahukarisantosh4655
    @sahukarisantosh4655 2 ปีที่แล้ว +1

    please make uideo on casl-react

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว +3

      Will consider it

    • @sahukarisantosh4655
      @sahukarisantosh4655 2 ปีที่แล้ว +1

      @@mariusespejo thank you ...plz do on ASAP...i want to use it in my code

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว

      If you need it asap then please check the docs, it’s fairly well written. Like I said I can consider it but I can’t guarantee it

  • @genechristiansomoza4931
    @genechristiansomoza4931 2 ปีที่แล้ว

    Ability is kinda cringey for me. I'm used to it for games not on development. 😅

    • @mariusespejo
      @mariusespejo  2 ปีที่แล้ว

      Lol you can always rename your variables however you like 😄

  • @JoãoSantos-r8b
    @JoãoSantos-r8b 7 หลายเดือนก่อน

    dude's last name is "mirror" lol