How To Manage User Roles In Node.js

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ค. 2024
  • 🚨 IMPORTANT:
    1 Year Free Hosting: www.atlantic.net/webdevsimpli...
    Use code KYLE for an additional $50
    User permission systems are the core of nearly every application. From Reddit to accounting every site needs to manage user permissions, but that is much easier said than done. In this video I will be showing you exactly how to set up a flexible and robust user permission system which you can use in any Node.js application.
    📚 Materials/References:
    Starting GitHub Code: github.com/WebDevSimplified/n...
    Ending GitHub Code: github.com/WebDevSimplified/n...
    HTTP Status Codes Tutorial: • Learn HTTP Status Code...
    🧠 Concepts Covered:
    - How to handle user auth in Node.js
    - How to create flexible user permissions in Node.js
    - How to manage scoped routes in Node.js
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    #Node.js #WDS #UserPermissions

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

  • @hnasr
    @hnasr 4 ปีที่แล้ว +242

    Legend! Love the way you clearly explain these concepts in a simple way. Cheers bro. You are on my recommended channel list on my YT

    • @AmanNidhi
      @AmanNidhi 4 ปีที่แล้ว +8

      i recommend you both on mine

    • @neon13x
      @neon13x 3 ปีที่แล้ว +7

      Legend praising another legend

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

      This comment section is pretty legendary

    • @JD-hq1kn
      @JD-hq1kn 10 หลายเดือนก่อน

      Ok

  • @cdporgrammer7168
    @cdporgrammer7168 4 ปีที่แล้ว +137

    Man I wonder if Kyle actually realises how much he helps us out with his vids man
    Respect man

    • @MrEnsiferum77
      @MrEnsiferum77 4 ปีที่แล้ว

      React kids won't understand u...

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

      @@MrEnsiferum77 реакт тут причем?

  • @mrsmasked180
    @mrsmasked180 4 ปีที่แล้ว +17

    You're simply the best out there because you explain your content very well and you go straight to the point, respect man you're a legend !

  • @eduardonunes1379
    @eduardonunes1379 4 ปีที่แล้ว +4

    This video is so motivating since I was taking a break from programming bc of authentication and authorization. Thank you so much!

  • @taquilol
    @taquilol 4 ปีที่แล้ว +132

    pretty much exactly the video I needed for a project. My man web dev simplified is a legend.

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

      You get it m8

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

      @@cdporgrammer7168 dear how it connect with mongodb?

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

      Very true.
      He is one of the few "bigger" code TH-camrs that has beneficial stuff I've used in real projects

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

      @@sohailkashif6992 a common cope I would get hit with in boot camp was,
      "Depends on your use case"
      Which it actually does.
      Having a hard time understanding the relationship between a user of the database
      And
      A user that is using the site.

    • @ng4logic
      @ng4logic 3 ปีที่แล้ว

      this dude is literally a mentor

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

    Love the progression in this video. Super clear

  • @evanmcdaniel5544
    @evanmcdaniel5544 3 ปีที่แล้ว

    Thanks so much for this. One of the better Node tutorials I've seen.

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

    Awesome video. Concept of modularity with pure functions make this truly scalable.

  • @ridl27
    @ridl27 4 ปีที่แล้ว +12

    such a clear explanation, thank you!

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

    I always keep telling everyone to subscribe to your channel. you're really great really hoping people would recognize you more often.

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

    Currently I'm learning MEAN and this is exactly what I need for my pet project. Thank you so much for what you're doing!

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

    Yay!!!!!!!!!!!!!!!!!!! So awesome!!! Thank you !!!! I was just thinking about this because I been following your other tutorials and I love them. I am excited to watch this video.

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

    Awesome, helped me with some of my lingering middleware integration ignorance. Well explained!

  • @NorteXGame
    @NorteXGame 4 ปีที่แล้ว +4

    Never happier when any youtuber uploads

  • @coanjos
    @coanjos 4 ปีที่แล้ว

    This video came in the perfect time. Thank you!

  • @dthoma128
    @dthoma128 4 ปีที่แล้ว

    Kyle ... great tutorial ... very glad I found your channel! Thank you.

  • @jeffmair
    @jeffmair 4 ปีที่แล้ว

    Your tutorials are top notch Kyle!

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

    Thank you very much! This is exactly what I needed for my project!

  • @joel9909
    @joel9909 3 ปีที่แล้ว

    Just found your channel, loving it so far.

  • @feminjustin2478
    @feminjustin2478 4 ปีที่แล้ว

    you explain things so simply....easy to follow.....

  • @rfossella
    @rfossella 4 ปีที่แล้ว +3

    As usual, great video - clear, concise and immediately usable. Thanks!

  • @thejasong01
    @thejasong01 4 ปีที่แล้ว

    YOU ARE AWESOME thankyou for putting great content, with fast but effective and SIMPLE :)

  • @adithyasanthosh30
    @adithyasanthosh30 4 ปีที่แล้ว

    Nice. Introductory tutorial on how to manage user roles.

  • @virgiliogervacioestadillo1389
    @virgiliogervacioestadillo1389 3 ปีที่แล้ว

    Thank you so much Kyle. I found this very helpful.

  • @a1225johnny
    @a1225johnny 3 ปีที่แล้ว

    very clear, it’s helpful in my project. Thanks a lot

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

    Your video helps me a lot. I really appreciate your effort.
    Thank you so much!

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

    You've Successfully Simplified Web For Us. Mission Accomplished 😎

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

    Kayel your videos are greate for intermediat developers. Clear and simple explanations. Have been following you. You are recommended.

  • @apurvsawant5703
    @apurvsawant5703 3 ปีที่แล้ว

    Exactly the video I needed. Thanks man.

  • @BBI-Brandboost
    @BBI-Brandboost 4 ปีที่แล้ว

    Thanks for sharing, it is nice and easy to follow which is a massive help.

  • @danial9864
    @danial9864 3 ปีที่แล้ว

    Thank you so much for the tutorial video it's quite informative and professional

  • @taherr1341
    @taherr1341 3 ปีที่แล้ว

    Awesome. You are the best. I wonder who could be so unfair to dislike it?

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

    Before watching this video, I was creating separate documents for admin and user. Thank you for such a clear explanation tutorial.

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

      I did that and submited lol. Had to pay for that in viva.

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

    wow! such a great tutorial. thanks for the guide!

  • @saisaske1
    @saisaske1 3 ปีที่แล้ว

    Just want to say thankyou man. Words are short here.

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

    This is the best video about role based authentication...Thanks

  • @pankaj_9998
    @pankaj_9998 3 ปีที่แล้ว

    Thank you. This was a great learing experience.

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

    Enjoyed the turotial throughly!

  • @edisvelicanin8854
    @edisvelicanin8854 4 ปีที่แล้ว

    exactly what i was looking for

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

    Thank you for such an amazing tutorial!

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

    Great content. Gives motivation to keep our channel going.

  • @fullstackgreekdeveloper8033
    @fullstackgreekdeveloper8033 4 ปีที่แล้ว

    Thank you for this video!

  • @thinkdomotic4915
    @thinkdomotic4915 4 ปีที่แล้ว

    Great content, very helpful, thanks lots

  • @user-qo3fu1lb2l
    @user-qo3fu1lb2l 5 หลายเดือนก่อน

    Thank you very much. This video... no, all your videos I watched are amazing and helpfull!

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

    Really amazing explanation!

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

    too good. I did a version in typescript but followed your flows. Thanks

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

    that was a great video thankyou so much!

  • @nirmesh44
    @nirmesh44 4 ปีที่แล้ว

    Perfect. Very nicely done

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

    YOu are amazing... Really simplified complex things

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

    My name is Kyle and my job is to simplify the web for you!
    Your tutorials are always gre8!

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

    You have helped me alot In React js

  • @indigosay
    @indigosay 4 ปีที่แล้ว

    I was just about to look for this information

  • @macarenamolina8875
    @macarenamolina8875 3 ปีที่แล้ว

    THANK YOUUUUUUUUUUU!!!!!! I finally understand this.

  • @ginokoybi7010
    @ginokoybi7010 11 หลายเดือนก่อน +1

    Your video help me too much thanks 🙏

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

    Kyle ... One Word: Genius

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

    Literally god tier content

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

    This is what i want , u are awesome sir 🤘🤘🤘

  • @alex_lomov
    @alex_lomov 3 ปีที่แล้ว

    Great layout of material.

  • @amitshrestha898
    @amitshrestha898 3 ปีที่แล้ว

    Found what I am looking for thanks.

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

    Thank you, Kyle.

  • @nfaical3
    @nfaical3 3 ปีที่แล้ว

    Thank you very much , as always easy explanations.
    Please add how to protect the server from attacks like ddos and brute force attack.

  • @pedro_jaber9357
    @pedro_jaber9357 8 หลายเดือนก่อน

    OMG! thank you. I believe in Web Dev Simplified supremacy!!!!!

  • @omar9987
    @omar9987 4 ปีที่แล้ว +7

    It would be awesome if you could create like a mini blog[or anything] with react & express that uses user-based roles. I'm trying to add user based rules to my react app :p

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

    Very good explained , Love from India.

  • @iurii7752
    @iurii7752 4 ปีที่แล้ว

    The best educational videos

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

    best teacher ever

  • @sakshamsrivastava6280
    @sakshamsrivastava6280 3 ปีที่แล้ว

    thanks for this man

  • @shuvokhan2054
    @shuvokhan2054 4 ปีที่แล้ว +5

    You're my boss ❤

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

    Nicely explained

  • @vidumini23
    @vidumini23 4 ปีที่แล้ว

    Very useful. Thank you..

  • @houssemkhi5036
    @houssemkhi5036 3 ปีที่แล้ว

    great as usual

  • @tough_year
    @tough_year 4 ปีที่แล้ว

    Awesome video !!!

  • @jacob.lee380
    @jacob.lee380 2 ปีที่แล้ว

    Every time I get stuck in something and try to solve a problem by myself. I'm always ended up on your video finding a solution

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

    Thank you so much

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

    Lovved it thanks

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

    More tutorials please!

  • @agasthya3693
    @agasthya3693 4 ปีที่แล้ว

    Hey Kayle please do video on Gulp, Grunt, webpack and parcel & when to choose which tool

  • @tharhtetkyaw7528
    @tharhtetkyaw7528 3 ปีที่แล้ว

    thanks you are a legend

  • @yadneshkhode3091
    @yadneshkhode3091 4 ปีที่แล้ว

    Thank you !!!

  • @indigosay
    @indigosay 4 ปีที่แล้ว

    I added this video to my Gold Collection

  • @benjamincisneros797
    @benjamincisneros797 4 ปีที่แล้ว +3

    Great video. I just think one small refactor is to use the canViewProject function inside your filter for scopedProjects

  • @webmobi2773
    @webmobi2773 3 ปีที่แล้ว

    Thank you ❤️

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

    Big thanks

  • @ankitdubey9310
    @ankitdubey9310 3 ปีที่แล้ว

    best video, way to go

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

    Loved it :)

  • @ayoubalem865
    @ayoubalem865 4 ปีที่แล้ว +20

    7:11 Th res.Status Should be 401 Not 403
    401 Means Unauthorized , Forbidden Mean He is Sign Up But he doesn't have the permission to access to the given operation.
    Thank You btw !

    • @Study-bt6qh
      @Study-bt6qh 10 หลายเดือนก่อน

      kakashi hatake😍😍

  • @tranquility6358
    @tranquility6358 4 ปีที่แล้ว +47

    I believe you mixed up the status codes a bit.
    401 Unauthorized - send this code when the user is not authorized to view the content (as in the user didn't login)
    403 Forbidden - send this code when the user is logged in, but is not allowed to view specific content
    The difference between these status codes is that 401 should be sent if authorization fails, but proper authorization is possible while 403 is sent when the user is authorized, but doesn't have the required permissions.

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

      are you sure?
      authentication means verifying identity. It gets confusing because the header used to authenticate is called 'authorization' but it is still authentication and 401.
      authorization means verifying permissions which is 403

    • @brielov
      @brielov 4 ปีที่แล้ว +10

      I agree... 401 is for unauthorized (not logged in) and 403 is forbidden (resource authorization)

    • @Hadermite
      @Hadermite 4 ปีที่แล้ว +4

      @@ArthurCowdery Yes, he's correct. The title of the status code is a bit messed up, should probably have been "Unauthenticated" instead, it generates a lot of confusion.

    • @andrejvujic
      @andrejvujic 3 ปีที่แล้ว

      @Jovan Jevtic jovaneeeee

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

    Thanks man

  • @sonamuhialdeen7866
    @sonamuhialdeen7866 3 ปีที่แล้ว

    So good tutorial

  • @h4kster182
    @h4kster182 4 ปีที่แล้ว

    Nice explanation

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

    I've got a permission system where the permissions are strings like "mail.create" or "mail.*". Each user has an array of permissions, and the permission strings are referenced in a POJO that maps object keys to permission names. So if you have an endpoint that should be accessible to anyone with a mail permission, you call `hasAnyPermission(req.user, Permissions.mail)` where Permissions.mail is an object containing more fine-grained permissions, and that endpoint would be accessible to anybody with a "mail.(whatever)" permission. "*" is a wildcard that refers to all sub-permissions, so if someone has the permission "*" then they're a superuser, and if they have the permission "boards.*" then they can do anything on any image board. There are some functions, like `hasAnyPermission` or `hasPermission` that you stick at the beginning of your route handler to enforce the permissions.
    Of course you don't have to put them at the beginning of your route handler - the entire frontend of the app is handlebars so I also use them on specific pages where I want to display some items/links but each link requires a separate permission. I'm actually pretty surprised that I've made it this far using only handlebars and plain JS for the frontend. The backend is NodeJS with Typescript which is great but I've managed to write a multiplayer FPS almost entirely in plain JS (server-side is that typescript nodejs I just mentioned) and build a forum engine and now I've set up a mail server and a way for users to create and manage email addresses on my domain. handlebars may not be flashy but damn if I like it

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

    You always get me bro

  • @vokhactri
    @vokhactri 4 ปีที่แล้ว

    Thanks so much

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

    I would recommend to assign every role an int so you can calculate up and downwards !
    Edit: To calculate the inheritance of the groups if given!

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

      Why ? explain briefly please..

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

      @@montasirmahmud3585 Then you can just use bit calculations to check if a role is hierarchically over another role or not by for example using >=

  • @dubola5777
    @dubola5777 3 ปีที่แล้ว

    hats off!

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

    Awesome video

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

    Very good ❤

  • @ayushthakur733
    @ayushthakur733 3 ปีที่แล้ว

    So accurate with the name web dev SIMPLIFIED

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

    Great content. Would love to see an elaborated version of this with more of a real world scenarios where an user can have multiple role for different context.

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

      Really all you need to do is make the user.role property an array and then check if the role is in the array or not.

    • @smsibasish
      @smsibasish 4 ปีที่แล้ว

      @@WebDevSimplified I feel that will create some problem. Let me explain with your example. Say I introduce two new roles project viewer and project admin. Now one user can be project viewer for one project and project admin for another. If I model my user to have an array of roles then how do I keep track of which project he is an admin of.

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

      @@smsibasish I think you can simply tie the role to the project, meaning your DB entry can look like this:
      Projects: [
      { id:1, name:"first project", userId: 1, userRole: canView},
      ...
      ]
      then you check for the user's role with its id to distinguish between what can be viewed and what can be edited, deleted or updated
      but what case should require something like that?

    • @smsibasish
      @smsibasish 4 ปีที่แล้ว

      @@mrviometal4948 consider this example. A user can be part of many groups and a group may contain a few user. A user can be a admin of few groups but member of other groups. In that case what should be the role of the user in User model and should the Group model have the admin and member fields separately.

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

      @@smsibasish I see, I think it can be solved by an abstraction between admin and view. i.e. any admin can view, but not any viewer can be an admin. And so, the edit and delete have to be from an admin or the respective user of the project. But the project entries can contain an array of users who can view...i.e. =>
      Projects: [
      { id:1, name:"first project", userId: 1, usersWhoCanView: [2,3,4,5...]},
      ...
      ]
      so in the implementation of the can view, we can look up the array if it contains the signed-in user's id. In this case, those who can view are (Admins, People who create the project i.e. userId, and any user in the array)
      Does that make sense?

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

    This is so cool