No video

Passport JWT tutorial - Authentication with JSON Web Tokens

  Рет қаралды 11,272

Seeker Labs

Seeker Labs

Күн бұрын

Source Code: truthseekers.i...
In this Passport JWT Tutorial we go through authentication with Passport and JSON Web Tokens step by step. The first thing we do is look at JSON Web Tokens by themselves, without any passport.js code.
A JWT is just a jumbled string that when decoded contains some piece of identifying data. So, you could store an entire user object, or just the ID. When you create the token, you're also adding a "secret key" to the mix that is used in the formula to jumble the string. Without that "secret key" you won't be able to decode the string.
This is useful for security, and it also makes it easier to share the JWT across multiple domains and applications. All you need to do is use the same secret key across apps that you want to share login for. If the secret key changes at all, then all the tokens using it will break.
Once the server has created the token, we give that token to the client, and they store it somewhere "safe" (Like LocalStorage?). On every request, or every request where they're trying to access "secure" routes, the token is passed along with the request to the server in the HEADERS. The server will see the token and try to decode it. If the server is able to decode the data properly, it will use the decoded data to handle the rest of the request. For example, getting a list of the user's recipes from the database based on the ID of the user in the decoded JWT.
When you throw Passport.js into the equation, it's not much harder to understand. Passport.js provides a "Local Strategy" for users to enter their signup/login information. Passport provides you a function to allow you to determine if the users entered info meets your criteria and matches any records in the database. If the user is signing up, you create a function to make sure the password meets the criteria, there's no existing user with that email, etc. If all is good you pass to Passport's callback function.
From the callback function you take the error, user object, and optional message object and process that how you like. If all is good you log them in by creating a JWT. Otherwise you throw an error and redirect.
Once you give the JWT to the "client", the next step is to "lock down" any routes that require authorization / authentication. This is where Passport's JWT strategy comes into the picture.
The Passport JWT acts as a "lock" to all the routes requiring a user to be logged in. So any time a user wants to visit a secured area of the site, they have to pull out their JWT, and give it to the JWT strategy, which decodes the JWT and allows them in or rejects them.

Пікірлер: 14
@Dev_Jet
@Dev_Jet Жыл бұрын
I just finished your tutorial on Passport JWT and it was awesome. Thanks for making it so clear and concise. I learned a lot.
@francescomerighi6458
@francescomerighi6458 8 ай бұрын
i don't understand why generating a token when a user sign up, but the tutorial was awesome!
@SeekerLabs
@SeekerLabs 8 ай бұрын
Yeah, I think I was intending to have the user login automatically after signup and then I forgot. lol. oops.
@BohdanIlienko
@BohdanIlienko Жыл бұрын
good job, dude)
@user-ox1fi4cw7e
@user-ox1fi4cw7e 7 ай бұрын
What about refreshing token when it expires?
@vinitjain7
@vinitjain7 Жыл бұрын
hey can you make a video , in which you use jwt passport and bcrypt
@vardgesmovsesyan3967
@vardgesmovsesyan3967 Жыл бұрын
Nice tutorial bro
@andresdavila1504
@andresdavila1504 Жыл бұрын
it was all good until you did the logout, what you did does not make a logout because your token will be active forever... btw good tutorial
@notyourbusiness2672
@notyourbusiness2672 7 күн бұрын
Source code link leads to ERR_CONNECTION_TIMED_OUT
@SeekerLabs
@SeekerLabs 7 күн бұрын
github.com/truthseekers Sorry about that.. I have a handful of jwt repos, most should work.
@notyourbusiness2672
@notyourbusiness2672 7 күн бұрын
@@SeekerLabs Wow I didn't expect an answer! Thank you very much!!!
@amanmanojpreeti
@amanmanojpreeti 4 ай бұрын
logout?
@audunhilden
@audunhilden 10 ай бұрын
hey, github url? your website is down
@SeekerLabs
@SeekerLabs 10 ай бұрын
ah shit. Sorry! Managing content is.. hard lol. I have a couple repos regarding auth, and I'm not sure which one goes with this video. github.com/truthseekers/graphql-react-passport-jwt-auth github.com/truthseekers/auth-mern-rest-jwt-passport/ github.com/truthseekers/auth-mern-gql-jwt-passport/ I thought I had a fullstack project for both graphql and react, and I'm not sure which of these repos goes with the video. Sorry my Github is such a mess. Hopefully the repos help.
What Is JWT and Why Should You Use JWT
14:53
Web Dev Simplified
Рет қаралды 1,1 МЛН
123 GO! Houseによる偽の舌ドッキリ 😂👅
00:20
123 GO! HOUSE Japanese
Рет қаралды 5 МЛН
JWT Authentication | Node JS and Express tutorials for Beginners
1:00:03
Difference between cookies, session and tokens
11:53
Valentin Despa
Рет қаралды 617 М.
Passport JWT Strategy Configuration (Node + Passport + Express)
38:47
Zach Gollwitzer
Рет қаралды 30 М.
Express JS #15 - Passport.js Authentication
37:41
Anson the Developer
Рет қаралды 12 М.
Cookies, Sessions, JSON Web Tokens (JWT) and More 🍪🔐
46:41
LearnWebCode
Рет қаралды 105 М.
Learn JWT in 10 Minutes with Express, Node, and Cookie Parser
10:20
Web Dev Cody
Рет қаралды 106 М.
Why is JWT popular?
5:14
ByteByteGo
Рет қаралды 305 М.
Comprehensive Passport.js Tutorial with Express & Sessions
1:29:02
JWT Authentication Tutorial - Node.js
27:36
Web Dev Simplified
Рет қаралды 1 МЛН