Тёмный

FastAPI JWT Tutorial | How to add User Authentication 

Eric Roby
Подписаться 7 тыс.
Просмотров 45 тыс.
50% 1

In this tutorial, we will walk you through the process of integrating JWT (JSON Web Tokens) with FastAPI to secure user authentication. If you want to add JWT authentication to your FastAPI application, this video is for you!
If you are new to FastAPI or JWT, don't worry. We will cover the basics and gradually move towards more advanced topics. You'll get to understand not only how to implement JWT but also why it is important. FastAPI JWT authentication is what you want to use for your JWT Python project
🎥 React + FastAPI: • How to build a React +...
🎥 AWS Lambda + FastAPI: • Deploy FastAPI on AWS ...
🎥 MongoDB + FastAPI: • Unlocking the Power of...
🎥 MySQL + FastAPI: • How to build a FastAPI...
🎥 PostgreSQL + FastAPI: • How to build a FastAPI...
===========================
🎓 Courses can be found:
FastAPI Beginner & Advanced: www.udemy.com/course/fastapi-...
Learn Chatbot Development: www.udemy.com/course/the-comp...
===========================
🛒 GEAR & RECOMMENDED BOOKS: kit.co/codingwithroby
👨‍💻 Hi, I am Eric!👨‍💻
Hey! I’m a seasoned developer and cloud enthusiast. I love sharing my knowledge and helping others to discover the exciting world of programming and cloud technologies. For more tutorials, tips, and tech magic, don’t forget to hit that subscribe button and ring the bell for notifications!
🙏 Support The Channel 🙏
If you found this tutorial helpful, please give it a thumbs up 👍, comment, and share it with your network 🌍
#fastapi #python #programming

Опубликовано:

 

15 июл 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 94   
@breno-codes1633
@breno-codes1633 4 месяца назад
Man, congratulations on your lessons. Here in Brazil, we find little information about this subject, and you are saving us.
@codingwithroby
@codingwithroby 4 месяца назад
Awesome, thank you! I am happy to help
@ryanschaefer4847
@ryanschaefer4847 Год назад
Wow the timing, this is literally what i was just trying to figure out! Thanks!
@codingwithroby
@codingwithroby Год назад
Glad I could help!
@8bitoverclocking932
@8bitoverclocking932 Год назад
Agreed! I was literally starting this this morning but didn't see this vid until now. You have a new sub!
@codingwithroby
@codingwithroby Год назад
Woot woot! Welcome friend 🙂
@lonehayena
@lonehayena 5 месяцев назад
Nice tutorial. But it would be awesome if you include a link to a repo. It's not because I don't want to type but I want to see multiple file at the same time while watching your video. Thanks anyway.
@JuliusThePianoGuy
@JuliusThePianoGuy 6 месяцев назад
Very nice and structured video! Helped a lot, thanks!
@codingwithroby
@codingwithroby 6 месяцев назад
Great to hear! Glad you enjoyed it!
@vincenzobuonvino5860
@vincenzobuonvino5860 6 месяцев назад
I'm having a problem in the swagger the Authorize button is performing the call towards the endpoint /token (which does not exist) instead that /auth/token as shown at 19:50. I cannot understand why.
@herozero777
@herozero777 Месяц назад
Thanks man for this tutorial. It was amazing well explained and really saved me :)
@codingwithroby
@codingwithroby Месяц назад
You are sooo welcome! Cheers 🥂
@anuj7286
@anuj7286 9 месяцев назад
Thankyou for sharing the tutorial :)
@codingwithroby
@codingwithroby 9 месяцев назад
Of course, glad you are able to find value!
@gleb29
@gleb29 6 месяцев назад
hey, how i can get the token and save it in browser(how i can use oauth2) without using fastapi docs interface
@thegrind9628
@thegrind9628 5 месяцев назад
19:47 - I want to have a place holder for just the token and not the entire form. How to do that?
@jacobwilsonmwale1674
@jacobwilsonmwale1674 2 месяца назад
Amazing tutorial. I really enjoyed it. thanks for the amazing explaination🙏
@codingwithroby
@codingwithroby 2 месяца назад
Glad you enjoyed it! These types of comments keep me going, thank you 😊
@sowmiyapannerselvam997
@sowmiyapannerselvam997 3 месяца назад
I am created crud operation using fastapi and python,then I want implement jwt refresh token,pls give any idea
@user-dr8km5bb5n
@user-dr8km5bb5n 10 месяцев назад
awesome! really looked for a good fastapi auth content. one question, as i'm trying to understand the authentication world. so every endpoint i want to be protected, should validate the user like is done at the end at the "/" endpoint? this how authentication is usually being done in backend apps? doesn't it add a very high time complexity for each request (to check for the use in the db, which can have many users)
@codingwithroby
@codingwithroby 10 месяцев назад
Hey friend! Well, you authenticate the first time after that you verify the token's data on each request. This is pretty much how all token based authentication works 🙂
@dannisisgt
@dannisisgt 9 месяцев назад
Gracias amigo!! This video is very informative, your explanation are super clear, I would ask about the expiration time an automatically refresh it, refresh token or smt, is it store on db? Thx again for your time
@codingwithroby
@codingwithroby 9 месяцев назад
Thank you! I would recommend using Redis or caching to do this :-)
@nulops
@nulops 5 месяцев назад
hello , thanks for video, is it possible to share your setup to create this kind of content ? thanks advance
@LamNguyen-hw9lq
@LamNguyen-hw9lq 9 месяцев назад
Hi, thank you for the tutorial, how do I define a route that is accessible only when an user is logged in?
@codingwithroby
@codingwithroby 9 месяцев назад
You will want to add dependency injection to "get_current_user"
@karacan2469
@karacan2469 9 месяцев назад
Hey Eric my question in my mind is always how to keep these token on the frontend, I send access token an response header and in JSON format and don't give refresh token instead set it as a httponly cookie. In frontend I keep it in context API and to make sure if user is logged in or not for each 401 message I try to send request to refresh endpoint and in this way my cookie has been read by fast API and again I keep it in context API in frontend which is nextjs What do you think of this? What is the best practice? I want to know your opinion
@codingwithroby
@codingwithroby 9 месяцев назад
Hey great question! The best way is to save the token inside your local or session storage on your frontend. This means you can call the current token whenever it is needed. When sending the token always make sure to have FastAPI verify the token. If it is successful than the token works, proceed as normal. If the token does not work either: A) Delete token on FE and redirect to login page again (so they can resign in) B) (Much more advanced) Send a new token from the BE based on other factors if you are tracking them (IP addresses location, etc) to verify it is the correct user outside of just the token
@rstiekema
@rstiekema 18 дней назад
Can I download your code from this tutorial somewhere? Keep up the good work. Btw, very helpful turorial! Thank you so much.
@appallasuryanarayanababu5074
@appallasuryanarayanababu5074 8 месяцев назад
Hi Eric, Annotated is not supported in python 3.8 version could pls suggest alternative??
@codingwithroby
@codingwithroby 8 месяцев назад
I would recommend upgrading Python, but you do not need to using Annotated. You can pass the Dependency right in the parameter itself.
@edderleonardo
@edderleonardo 6 месяцев назад
your fastapi course on udemy is amazing
@codingwithroby
@codingwithroby 6 месяцев назад
Yay! I am glad you enjoy it!!
@4crazibility75
@4crazibility75 Месяц назад
Thanks for your video. But do you know how I can integrate Clerk to my fast api project?
@codingwithroby
@codingwithroby Месяц назад
Not as of right now sorry, I can look into making a video in the future on it 🙂
@Ken-ix7ms
@Ken-ix7ms 8 месяцев назад
Really quick, what if the user is logged in and goes to the homepage, I want to redirect them to another page since they're already logged in
@codingwithroby
@codingwithroby 8 месяцев назад
You will want the Front End to handle the redirects.
@timbrap4693
@timbrap4693 11 месяцев назад
Why did you make the get_current_user function async?
@codingwithroby
@codingwithroby 11 месяцев назад
It is used for dependency injection if you are wanting to find the current user signed in
@timbrap4693
@timbrap4693 11 месяцев назад
@@codingwithroby but is it necessary to make it async? There is no awaiting inside the function
@ModernStockPortfolioTracker
@ModernStockPortfolioTracker 8 месяцев назад
Thanks for the tutorial! Can you make a sequal on token rotation. How to refresh access token seamlessly without logging the user out. Thank you.
@codingwithroby
@codingwithroby 8 месяцев назад
Ohh not a bad idea, I will add it to my backlog of future videos 🙂
@ishan.girdhar
@ishan.girdhar 8 месяцев назад
Super helpful! just 1 small request, let's not call it, un-hash. You can't un-hash a hash. Hashing is one way encryption. You can match the hash with the user provided password and match it with the hash already stored in database.
@codingwithroby
@codingwithroby 8 месяцев назад
You are right! It's tough speaking in front of a camera, sometimes things just come out LOL
@ishan.girdhar
@ishan.girdhar 8 месяцев назад
@@codingwithroby Hey, I understand. It’s hard. I just wanted to drop a comment to avoid any misunderstanding for the viewers. Not a reflection on you. Even though I have bought your course on Udemy, I am still watching your free videos here. 🤗
@codingwithroby
@codingwithroby 8 месяцев назад
You're the best! Thanks for pointing that out for future viewers 🙂
@laszlomagyar3156
@laszlomagyar3156 11 месяцев назад
If I login then refresh the page I need to login again to access the login required features. Why is that? Is there a way to avoid this?
@codingwithroby
@codingwithroby 11 месяцев назад
Not when using Swagger - An alternative could be using Postman or another API Test platform and then you can reuse the JWT
@laszlomagyar3156
@laszlomagyar3156 11 месяцев назад
@@codingwithroby Thank you!
@amiltonmoreira2341
@amiltonmoreira2341 Месяц назад
If some hack knows the secret key and the algorithm how can he authenticate? Doesn t the function get_current_user prevents that?
@codingwithroby
@codingwithroby Месяц назад
Well, hypothetically they can create the JWT from their own code then.
@rootifera
@rootifera 6 месяцев назад
Hi Eric, thanks for the video. I just finished your FastApi Udemy course and writing a small API for inventory management. I'm trying to get Refresh token to work but couldn't figure it out yet. Do you have any plans to make a video about using Refresh tokens? Thanks!
@codingwithroby
@codingwithroby 6 месяцев назад
You bet! Currently I do not for a RU-vid video, perhaps that could be a good addition to the Udemy course?
@rootifera
@rootifera 6 месяцев назад
@@codingwithroby hey Eric! yeah that would be amazing! I finally made the refresh token work (I still need to test) but it would be great to see how you do it so I can improve my code and especially understanding of the tokens. Thanks!
@vicotrshysko6761
@vicotrshysko6761 10 месяцев назад
Hey Eric this video is awesome. Just now i am implementing some my own project and this information was so useful! Is it possible to implement "logout" functionality?
@codingwithroby
@codingwithroby 10 месяцев назад
Hi friend! This is complicated and not complicated at the exact same time 🥲 JWT's are very popular because you do not need a database for validation each time and they are completely stateless. This means the backend does not know what the JWT is outside of authorizing the original intent. However, each JWT DOES have an expiration date, lets for example say 20 minutes. The absolute best way to create a log out system is to create a blacklist system for JWT's. For example using in-memory (like Redis - if you do not know Redis there is a video coming verrrrryyyy soon) store the JWT there, set the ttl to expire when the JWT expires, and compare new requests to this list of blacklist JWT's to either approve to deny.
@mudassiraqeel8604
@mudassiraqeel8604 3 месяца назад
but we never used the bearer token or did we ?
@codingwithroby
@codingwithroby 3 месяца назад
We did 🙂
@diezitrone_111
@diezitrone_111 6 месяцев назад
Hey what ide theme is that?
@codingwithroby
@codingwithroby 6 месяцев назад
One dark pro extension on VS Code.
@learnapplybuild
@learnapplybuild 7 месяцев назад
What tool do you use to record lectures. its just amazing
@codingwithroby
@codingwithroby 7 месяцев назад
Yo! thanks 🙂 I use a mirrorless camera for recording myself, heir PR40 for mircophone and camtashia for recording!
@ERYE
@ERYE 10 месяцев назад
How do i connect it with frontend?
@codingwithroby
@codingwithroby 10 месяцев назад
Great question! Check out my full stack video : ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-0zb2kohYZIM.htmlsi=v_eDYYlytP04CNcj
@a.s8840
@a.s8840 11 месяцев назад
how we integrate google auth with this?
@codingwithroby
@codingwithroby 11 месяцев назад
Hmm this sounds like a nice video idea!
@a.s8840
@a.s8840 11 месяцев назад
@@codingwithroby Yes I am very curious to learn this ..I am following you udemy course
@a.s8840
@a.s8840 11 месяцев назад
@@codingwithroby can you suggest me how can I integrate.😢😢
@sivadevil4845
@sivadevil4845 4 месяца назад
Hi Eric, Just from few weeks back i have started watching your videos are awesome, i think it will be helpful if you could post the code.
@codingwithroby
@codingwithroby 4 месяца назад
Yeah - that's my bad, I don't have it anymore.
@user-ku1bv9go6t
@user-ku1bv9go6t 5 месяцев назад
i watched many videos , you explained ,everything perfect , thank you so much , just watched it too late :( ,
@codingwithroby
@codingwithroby 5 месяцев назад
Glad it helped!
@suen-tech
@suen-tech Год назад
Keep going.
@codingwithroby
@codingwithroby Год назад
Thank you, that’s the plan!
@marteinngumundsson8086
@marteinngumundsson8086 3 месяца назад
The import statement in the auth file: from datetime import timedelta, datetime from typing import Annotated from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel from sqlalchemy.orm import Session from starlette import status from database import SessionLocal from models import Users from passlib.context import CryptContext from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer from jose import jwt, JWTError
@Nau-uc9tk
@Nau-uc9tk 7 месяцев назад
where i can get this code?
@codingwithroby
@codingwithroby 7 месяцев назад
I didn't make a repo before deleting the project - sorry 😞
@LiftAndC0de
@LiftAndC0de 10 месяцев назад
Fantastic video, can you share the Github repo?
@codingwithroby
@codingwithroby 10 месяцев назад
Hey friend! Thanks for the suggestion. As of now I have not added public repositories. I am planning on doing so in the near future 🙂
@chikkivenu6632
@chikkivenu6632 9 месяцев назад
Your code is fool proof. But I was wondering how did you map this with your finance app? Please let me know
@codingwithroby
@codingwithroby 8 месяцев назад
You're the best, thank you! The best way is to keep the token in the React SessionStorage or LocalStorage and send it as a header within the request
@Gladius000
@Gladius000 10 месяцев назад
Good content. Please look up how to pronounce "schema".
@codingwithroby
@codingwithroby 10 месяцев назад
Glad you enjoyed the content 🙂
@hrushikeswarareddy251
@hrushikeswarareddy251 4 месяца назад
also upload link for source code
@codingwithroby
@codingwithroby 4 месяца назад
Yeah - that's my bad, I don't have it anymore.
@hrushikeswarareddy251
@hrushikeswarareddy251 4 месяца назад
😅👍
@nawaraj.karkee
@nawaraj.karkee 3 месяца назад
4:45, How the hell do you unhash something, ?????
@codingwithroby
@codingwithroby 3 месяца назад
I misspoke (being on camera is hard!) - Once a password is hashed, you rehash the next "plain" password to see if the values match. Example (but always use verify method): hashed_password == hashed(plain_text_password)
@kompalalokesh5586
@kompalalokesh5586 4 месяца назад
Background music is annoying
@codingwithroby
@codingwithroby 4 месяца назад
Sorry my guy
Далее
Deploy FastAPI on AWS Lambda | In 9 MINUTES
9:02
Просмотров 24 тыс.
FastAPI Authentication with  JWT (JSON Web Tokens)
56:18
Luiza Rasulova #luizarasulova
00:37
Просмотров 2,6 млн
Cat Plays with Window Washer
00:22
Просмотров 1,6 млн
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
What Is JWT and Why Should You Use JWT
14:53
Просмотров 1,1 млн