GraphQL Authentication: JWT, login, signup, and more! | NestJS PassportJS Tutorial

Поділитися
Вставка
  • Опубліковано 5 жов 2024

КОМЕНТАРІ • 101

  • @ivancosta3229
    @ivancosta3229 2 роки тому +25

    Also note that this is a complex topic and you must do some read before jumping into implementing it.
    A few things to keep in mind:
    Anything that you put in a JWT is signed, but not encrypted, so anyone can read the user details that you attach to it. Don't expose anything sensible.
    Usually the JWT approach also involves returning a "refresh token" along the access token. The access one is short lived and the refresh one is long lived. The client then uses the refresh one to exchange it for another access token when this is about to expire. This is in order to minimize the damage that someone could do by stealing an access token.
    Finally bear in mind that there are other signing algorithms besides a shared secret (HMAC). The current state of the art if I'm not wrong is EdDSA / Ed25519 which uses a set of private and public key to sign and verify respectively.
    Cheers!

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      great points!

    • @ThePandaGuitar
      @ThePandaGuitar 2 роки тому

      How does one revoke a JWT?

    • @mariusespejo
      @mariusespejo  2 роки тому

      Generally you’d need to store something server side which uniquely identifies any given jwt, or actually just store sessions in a shared store like redis or similar and use the jwt (or a value within it) as the key to the record. Then revoking in either case is simply removing that corresponding server-side record, and making sure to always check that it exists to determine if the jwt is still valid (in addition to the other typical verification)

    • @kuhlen9028
      @kuhlen9028 2 роки тому

      @@mariusespejo Any chance you can follow up and show this implemented with a refresh token as well?

    • @dawid_dahl
      @dawid_dahl Рік тому

      I’d love a follow up with refresh token as well, as that is something one really would need.

  • @ThếPhươngDưĐức
    @ThếPhươngDưĐức Рік тому

    This saved me a day. Now, I can implement Jwt Strategy with GraphQL in NestJS. Thanks a lot for your video :))

  • @DigitalFactoryFX
    @DigitalFactoryFX 2 роки тому +7

    I know you mentioned that you're not giving us the 'production-ready' solution, would you ever consider going into things like best practices for production-ready apps? Without hands-on industry experience it's difficult to get out of the UA-cam/Udemy to-do apps level of work. Love the content by the way - miles ahead of other UA-camrs!

    • @mariusespejo
      @mariusespejo  2 роки тому +4

      production-readiness really depends a lot on several things: your or your company’s infrastructure, your security and privacy requirements, etc. It’s a big topic that can go in several different paths. With that said I don’t claim to know all possible or best paths but I’d definitely like to cover more systems design stuff which might cover some of it.
      Anyways thanks for your input and feedback!

  • @badrinath8958
    @badrinath8958 Рік тому

    Learn GraphQl using NestJS based on this tutorial. Thaks Marius keep going on...

  • @rajgohil8610
    @rajgohil8610 2 роки тому +1

    Seriously, buddy, you nailed it. I really enjoyed your deep dive into this topic, where you performed each and every step while also explaining each and every step. Thank you so much for your excellent teaching and knowledge sharing, and I hope and pray that your channel will continue to expand... Thank you very much once again.

    • @mariusespejo
      @mariusespejo  2 роки тому

      Thanks for your support Raj! I appreciate the feedback 🙏

  • @jjnimes
    @jjnimes 2 роки тому +1

    You made me subscribe to your channel~ I like your contents about NestJS because I'm a fan of it also. And also, I'm planning to create contents with it soon when there are available time. Clear, detailed, and an audible voice. More power!

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      Thank you! Yeah if you’re at all interested in creating content, I highly recommend it. Great way to learn

  • @juamp_m
    @juamp_m Рік тому

    Great vid Marius! You're right this topic is usually not well documented and I had a hard time trying to find a nice tutorial / article, I like the way you explain all concepts in detail you helped me a lot
    Cheers!

  • @meemz3144
    @meemz3144 2 роки тому

    Thank you very much for your video! Literally the best nestjs videos existing!

  • @asifiqbalmunna
    @asifiqbalmunna 2 роки тому +1

    thanks for these effective tutorials. we expect more like these. thanks a lot.

  • @lifeok6188
    @lifeok6188 2 роки тому +1

    Just in time man, I needed. Thanks man 🤞

  • @jagajaga6908
    @jagajaga6908 5 місяців тому

    thank you, bro. Amazing tutorial!!!

  • @abbastolgayylmaz4078
    @abbastolgayylmaz4078 10 місяців тому

    great! would be cool to have a new tutorial where apollo federation v2 is also in the scenario and calls to a secure gateway provides auth for other services in comm with auth service.

  • @vihoserge
    @vihoserge 2 роки тому +1

    Thanks. Really instructive

  •  2 роки тому +3

    nestjs+graphql is a topic that I feel isn't covered by many people, I do appreciate this tutorial. Do you have any plans to cover how nest deals with federated gql services?

    • @mesparzajr
      @mesparzajr 2 роки тому +1

      Look at Krishna NestJS. He does a great job explaining federation in with graphql and nest.

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      Will probably cover it at some point, still figuring out the overall content strategy for the channel. Thanks for the idea!

  • @kurtestacion6113
    @kurtestacion6113 2 роки тому

    Right when i need it again! I think you can read minds Marius!

  • @gppproton
    @gppproton 2 роки тому

    Thanks for this concise tutorial

  • @alidadaashi
    @alidadaashi 2 роки тому

    That was really awesome yo. Enjoyed

  • @LexCademy
    @LexCademy 2 роки тому +1

    Awesome!

  • @basitwahid3452
    @basitwahid3452 2 роки тому +1

    i want to forgot and reset password authentication nestjs + graphql but i didn't see any videos can you suggest me any videos or tutoial which help me and if you make video so well good

  • @hoangvietle1653
    @hoangvietle1653 2 роки тому +1

    Great, thanks for your video, but it would make it easier if you can provide the source code. Sometimes, I had unusual bug and could not make the comparison with your library version. Anw, thanks for your content.

    • @mariusespejo
      @mariusespejo  2 роки тому

      That’s a good point, thanks for the feedback. Will try to find some time to get most of the code from my videos in a repo

  • @malamhari_
    @malamhari_ Рік тому

    Thanks, this helps me alot!

  • @muratasarslan2359
    @muratasarslan2359 2 роки тому

    Very clear and easy to follow along, thank you. Default algorithm is HS256 as far as I saw. How to generate a JWT for HS512? A sample would be of the highest appreciation 😀

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      The nestjs/jwt package is really just using the jsonwebtoken package underneath, and the sign method takes in an option object that allows to set the algorithm, see: github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback

  • @АлександрЕлагин-м2ю

    Very nice! Please more nestjs+graphql, medium/amazon clone?

  • @joebowbeer
    @joebowbeer 2 роки тому +2

    28:50 => 30:00 Spoiler: The user in context has already had its password stripped, so there's no need to do it again at line 23 in login.

  • @aqdasiftekhar
    @aqdasiftekhar 5 місяців тому

    Hey mentor, quick question: What are your thoughts about refreshing the token? It's crucial for security and user experience.

    • @mariusespejo
      @mariusespejo  5 місяців тому

      Yup don’t think I covered it here but definitely good to consider having a refresh flow, e.g. creating a refresh token alongside the access token, that way when it expires you can refresh and get a new one using the refresh token, allowing the user to not have to login again. You have to be smart however with making sure that it’s stored securely and also is rotated. A compromised refresh token would be pretty bad

  • @radosawguchowski1206
    @radosawguchowski1206 Рік тому

    Furthemore errors like:
    "Missing conditions" from auth files from passport package = explanation in: 13:30 video.
    Remember to add "@Injectable" to 'LocalStrategy' class
    Remember to add "@Column" to entity (without saving to db work, but when You retrieve data from db those will miss :D

  • @zejano
    @zejano 2 роки тому

    It would be nice if we had the sources for this example.

  • @martinmtandi335
    @martinmtandi335 2 роки тому +1

    can you do a tutorial on how to deploy nestjs application to digital aceans for example

  • @MrBarbaloonga
    @MrBarbaloonga 2 роки тому

    Bro amazing video, thanks a lot

  • @e.magnoneto5101
    @e.magnoneto5101 11 місяців тому

    Hello, thank you very much for another incredibly educational video.
    I had seen the session video and was thinking about how to develop a secure API to be put into production. Could you tell me if I'm exaggerating because I would like to put sessions id in cookies, these sessions are stored in a cache database like redis, and within this session, we have the user and the jwt. Or would just sessions be safer?

  • @Kasheftin
    @Kasheftin 2 роки тому

    What's the point of using passport and passport strategies? It looks like it gives overhead only. Why not just make a regular login mutation which accepts username and password, checks it against bcrypt, and then generates and returns jwt? And then just make a regular middleware which extracts bearer token, checks it and adds user to the context. And then something like graphql-shield might be used for permissions. All these steps you already completed in this great tutorial. I just don't understand how passport works and helps in any way here.

    • @mariusespejo
      @mariusespejo  2 роки тому +2

      You absolutely could that. Where passport shines is that it helps keep implementations across multiple projects mostly consistent. Also strategies are swappable, if one day you decide to change auth strategies, e.g. maybe you want to do it via a 3rd party service or perhaps oauth, oidc, etc… then you just change strategies, the rest of the functionality stays the same. This one with basic user/password is honestly the simplest strategy so it’s not as easy to see the value. However other strategies are much more complex to put together from scratch

  • @a.anvarbekov
    @a.anvarbekov Рік тому

    Great videos, Marius! but I'm having hard time combining auth & authz. I followed your previous auth & casl videos and combined them but got a error "user undefined - in the ability factory" and unexpected behaviors when using jwt and casl guards together in one resolver endpoint. We definitely need your help. Please, make a tutorial jwt + casl + actual db(typeorm sqlite). Thanks buddy!

  • @radosawguchowski1206
    @radosawguchowski1206 Рік тому

    When playing with JwtStrategy make sure to import 'Strategy' class from 'passport-jwt' instead of 'passport-local', it will lead to error 'unknown auth method 'jwt' '.
    I got this issue when was auto-importing files via vsc.

  • @life_ofcoder
    @life_ofcoder 2 роки тому +5

    code link available?

  • @hateem8287
    @hateem8287 Рік тому

    Thx for the tutorial Marius, always looking forward to your next videos. I have a question concerning securing the user, you didn't create a password field in the user entity, but wouldn't that create a problem when validating the user since findOne() will return a User and you'll have to compare his password, yet it isn't defined in the User entity, so that would normally return an error, I'm confused why my code addressed that error yet your code didn't.

    • @mariusespejo
      @mariusespejo  Рік тому +1

      Thanks for calling that out, I had to double check what I did in the video. You are correct that the entity should have a password field that I think I forgot to add in the video or accidentally edited it out, although if you watch the rest of the video I do create an array of users (my fake database) which includes a password field

  • @Slickstef11
    @Slickstef11 2 роки тому

    Can you do a video on Wundergraph? Looks awesome.

  • @peterpot12
    @peterpot12 Рік тому

    Hi thank you so much for making this video! I have a question I'd like to ask. In your example, there is one type of user to be validated. If an application has more than one type of users to validate, and they have different graphql database schema, how should I implement the authentication so it's scalable?

    • @mariusespejo
      @mariusespejo  Рік тому

      The validation part is totally up to you. If you have multiple types of users then your validation should account for that, e.g. perhaps your query helps determine what type of user it is

    • @peterpot12
      @peterpot12 Рік тому

      Hi @@mariusespejo thanks for the reply. What I was wondering was about the auth resolver query that validate the username/email. In the case of having one type of user, the validate query should return a promise of that user type and there is no confusion in that. However, when I have two or more classes of users that have different schema, do I need to define multiple of the validate queries, each returning a promise of the corresponding user class? Or, is there a more elegant way to handle all of the validation and login queries? I am quite new to NestJS and passport.js and not sure what the standard practice for this case is. Thanks!

  • @careersvista2788
    @careersvista2788 2 роки тому

    nice tutorial but please make video also with database postgres

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      What are you looking for with postgres? I have videos on the channel with prisma and typeorm, they both expose an api that’s mostly database agnostic

    • @careersvista2788
      @careersvista2788 2 роки тому

      @@mariusespejo yes i have watched but i am working on authentication and when i am validate user password i have some issues, my password bcrypt on database

  • @محمد_وسام
    @محمد_وسام Рік тому

    What is the name of your font?

  • @mesparzajr
    @mesparzajr 2 роки тому

    THANK YOUUUUU

  • @ezaz7
    @ezaz7 Рік тому

    Hello, can you share the github repo of this video.

  • @codernerd7076
    @codernerd7076 2 роки тому

    Great video but why code first?! The schema is so much easier to write out!

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      I actually have been trying both ways, you’ll notice in my teamseas video I used schema first which I thought made it a little more consistent with prisma. This really just goes down to preference but in NestJS specifically I think code-first is actually better, because you can fully utilize decorators e.g. setting up class-validator with input types

    • @codernerd7076
      @codernerd7076 2 роки тому

      @@mariusespejo that all are very good reasons, Thanks!

  • @juamp_m
    @juamp_m Рік тому

    How could I integrate roles within this approach?

    • @mariusespejo
      @mariusespejo  Рік тому +1

      Like authorization? You can take a look at my videos about CASL

    • @juamp_m
      @juamp_m Рік тому

      @@mariusespejo Yes, oh I'll take a look thank you!

  • @tanyadovzzhenko4173
    @tanyadovzzhenko4173 2 роки тому

    Do I need to give the user an access token after registration?

    • @mariusespejo
      @mariusespejo  2 роки тому

      Depends on if you log the user in automatically after registration. If you ask them to login explicitly the first time then I assume no

  • @lazaromanuelcunga6195
    @lazaromanuelcunga6195 Рік тому

    Where's the github repo?

  • @greatdata6047
    @greatdata6047 2 роки тому

    I wish you would create a crash course about react.js and nest.js

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      I do have one for nest.. It doesn’t really make sense to do them together, nest is agnostic to whatever your frontend is

    • @greatdata6047
      @greatdata6047 2 роки тому

      @@mariusespejo thanks , yes but I don't know how to combine these together and how to deploy them

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      You don’t have to think about it as something you need to “combine” they can and should likely be be two different deployments. Your react client would make API requests to your nest backend.
      Now if you really want to combine them, you NestJs server can also serve static content: docs.nestjs.com/recipes/serve-static

  • @adnanaslam9604
    @adnanaslam9604 Рік тому

    please make a video on nest js graphql file uploading🙏

  • @krishnamandava9919
    @krishnamandava9919 2 роки тому

    Why we are using mutation instead of query for login method? We are not writing any data right ?

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      Mutations aren’t necessarily just for writing data, it can also be for things which changes state. That includes things like user sessions, login activity etc. you’re not simply fetching data in most cases with auth, you’re mutating the server’s state. For simpler state-less situations yeah I could see it being just a query

    • @krishnamandava9919
      @krishnamandava9919 2 роки тому

      @@mariusespejo Thanks for your response.
      One more question can we send own error messages for wrong arguments(Boolean type for string types) instead of deafult graphql error messages

    • @mariusespejo
      @mariusespejo  2 роки тому

      Well checking that it matches the expect schema is one of the things graphql is designed to do, but beyond that if you have other custom validation yeah you definitely can customize the response

    • @krishnamandava9919
      @krishnamandava9919 2 роки тому

      The way you replying to each mesgae is ♥️.
      One request can you make a series on micro services from basic to advance level ☺️

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      Well I try to respond when I can 🙂 I’ll think of how I might do something like that, thanks for the idea!

  • @zawriter4783
    @zawriter4783 2 роки тому

    Can you make some RemixJS videos?

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      Yeah! Im actually probably going to look into that soon

  • @n8_nguyenngocphu160
    @n8_nguyenngocphu160 2 роки тому

  • @webmakaka
    @webmakaka 2 роки тому

    Hi! Thanks!
    Please add link on source codes in description!

  • @Rivederchee
    @Rivederchee 2 роки тому

    Great content, where Can I donate you?

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      I don’t have a place for that at the moment but thank you for the thought 🙏

    • @Rivederchee
      @Rivederchee 2 роки тому

      @@mariusespejo Make it or just start selling your knowledge somewhere. It's fresh, new-standard and properly explained.

  • @NhacMuiTet
    @NhacMuiTet 11 місяців тому

    plz, give me repo

  • @OlFunkyBastard
    @OlFunkyBastard 7 місяців тому

    Most common convention for naming fields and arguments in GraphQL is the camel case, where "access_token" is represented as accessToken.

  • @grzegorzadamski997
    @grzegorzadamski997 Рік тому

    Does anyone have a good idea/example how to combine NestJS, GraphQL and session authorization using passport? I am thinking of something like this: ua-cam.com/video/_L225zpUK0M/v-deo.html

    • @mariusespejo
      @mariusespejo  Рік тому

      Not sure if you watched this whole video but I did cover using the same passport-local strategy here and how to get that to work with graphql. Copy the way sessions are created in that other video and you’re basically there

  • @ndukachukz8067
    @ndukachukz8067 2 роки тому

    dude chill youre too fast there are beginners watching this to pass interviews

    • @mariusespejo
      @mariusespejo  2 роки тому +1

      this isn't really meant for interview prep but good luck on your interviews man! If it feels like too much for you spend some time reading about the topic, it's important to understand the fundamentals... most of what I'm showing here is just a single implementation