NestJS Authentication: JWTs, Sessions, logins, and more! | NestJS PassportJS Tutorial

preview_player
Показать описание
In this video we'll discuss the fundamentals of adding authentication to your NestJS API. We'll talk about utilizing passport to use different types of authentication strategies. Specifically we will try the passport-local strategy as a basic login with username and password example. Next, we'll take a look at potentially using sessions to store user information and persist their login state. Finally we will also take a look at a state-less approach with JSON Web Tokens (JWTs). We'll create our own JWTs and I'll show you how you can use that to protect your API routes!

00:00 - Intro
03:35 - Initial routes
04:41 - UsersService
07:50 - AuthService
10:40 - Implementing passport-local strategy (username/password login)
18:08 - AuthGuards
23:25 - Summary of local login flow
29:02 - Guard to check if user is authenticated
31:41 - Setting up sessions
40:37 - Summary of login with sessions flow
44:25 - Setting up JWT strategy, signing and validating
1:00:25 - Summary of JWT strategy flow
1:04:22 - Conclusion
1:05:20 - Outro
Рекомендации по теме
Комментарии
Автор

Note: a lot of people are getting the error “request.isAuthenticated is not a function”. Please note that I explicitly mentioned in the video that it will not work until you properly setup sessions. If you run into that error, KEEP watching! The problem will resolve itself once you have the full setup, don’t stop at the point of error. If you watch the rest of the video and still have this problem, double check that you properly configured and registered your strategies and guards exactly as shown in the video.

mariusespejo
Автор

A problem I encountered: if you use argument names other than 'username' and 'password' for local strategy, you must specify them as options in local.strategy in super({ usernameField: 'otherName1', passwordField: 'otherName2' }).

If you don't, it won't even throw any errors, you'll just keep getting a 401 Unauthorized error. This drove me insane. Hope this helps others avoid this mistake.

But great tutorial regardless! Content like this makes the internet amazing.

sailormetz
Автор

Hey Marius, thank you for the clear explanation brother. I've watched this tutorial maybe a 2 or 3 years ago (and instantly subbed of course) when I first wanted to get into NestJS, and now I'm attempting it once again few years later. I followed the docs on authentication and ended up with bunch of files without properly understanding the flow of the authentication. This is exactly what I was looking for in a tutorial: to explain the flow, and you did it perfectly. Appreciate you taking it a few steps further than other YouTubers and explaining the intention behind the code. I wish your channel success brother.

rism
Автор

The best tutorial i watched so far. Author tells the info clearly and without any useless data. So, i want to say that i was here when the num of followers had been 7k

kinopiskfreepromocode
Автор

This tutorial I wanted today, what a coincidence

thecastiel
Автор

Hey Marius, just passing by to thank you, your video format is awesome.
I love how you explain everything while showing the documentation. It really adds to us as developers, so that we know like "oh, so the information was here all the time!". It particularly helped me a lot.

Keep up the good work!

JorgeFrota-rw
Автор

00:00​ - Intro
03:35​ - Initial routes
04:41​ - UsersService
07:50​ - AuthService
10:40​ - Implementing passport-local strategy (username/password login)
18:08​ - AuthGuards
23:25​ - Summary of local login flow
29:02​ - Guard to check if user is authenticated
31:41​ - Setting up sessions
40:37​ - Summary of login with sessions flow
44:25​ - Setting up JWT strategy, signing and validating
1:00:25​ - Summary of JWT strategy flow
1:04:22​ - Conclusion
1:05:20​ - Outro

mariusespejo
Автор

That walkthrough at @23:45, priceless!
So there's actually a guard on the route but you can login with the right body data? neat!!

cholasimmons
Автор

Even today i remember how i asked the question below the similar type of video about jwt and you reply back really fast althought your video was already great. As i see you still answers the questions and its impresive.

bossmusa
Автор

I highly appreciate you going throug the code roughly and also briefly explaining the NPM packages which you're using / recommending. It's really fun coding along and learning in this video!

xZunaii
Автор

Dude, after 4 days struggling you opened my eyes. They should add all of this in their documentation. You are a gold tressure! <3

OetziOfficial
Автор

Can't thank you enough for this Marius, excellent content, pitched and paced perfectly.

foofighterdaz
Автор

Thanks for this tutorial, I went through the NestJS docs a few times and tried to set this up myself but there was always something wrong. Your vid helped me finally get it all working!

Randito
Автор

your nest js contents are soo good. keep it up marius

quamzgraphix
Автор

So glad to know that you are following the official NestJs documentation! I also do the same.

darpananeja
Автор

Thanks for spending the time explaining each part and drawing the parallels to his we would do it in express.

Really helped me understand how to accomplish session auth. Felt a bit lost when the docs only covered JWT auth and all the tutorials I found were showing me the code to make it work... But not why it worked haha

FunkyToe
Автор

I love this tutorial. Clear as Spring water 💯

asogbaibrahim
Автор

Thank you so much for this tutorial, it helped clear up so much of my confusion. All the examples of different Passport strategies are written using ES6 modules, but the documentation for Nest is with classes. Your video helped translate the difference and finally got my code to work. Very clear and well explained --signed a junior developer that only learned ES6 Javascript in my bootcamp XD

h.w.b.
Автор

Best tutorial on NestJs authentication so far. Thanks a lot!

paolotessarolo
Автор

over separation of concerns. thanks for the tutorial anyway. I don't know why Nest is making everything complicated, yet people enjoying it, frankly.

aben