FastAPI JWT Tutorial | How to add User Authentication

  Рет қаралды 55,668

Eric Roby

Eric Roby

Күн бұрын

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/...
Learn Chatbot Development: www.udemy.com/...
===========================
🛒 GEAR & RECOMMENDED BOOKS: kit.co/codingw...
👨‍💻 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

Пікірлер: 110
@breno-codes1633
@breno-codes1633 7 ай бұрын
Man, congratulations on your lessons. Here in Brazil, we find little information about this subject, and you are saving us.
@codingwithroby
@codingwithroby 7 ай бұрын
Awesome, thank you! I am happy to help
@default_youtube_profile
@default_youtube_profile Ай бұрын
you can read the docs online as well.
@lonehayena
@lonehayena 8 ай бұрын
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.
@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 🙂
@jacobwilsonmwale1674
@jacobwilsonmwale1674 5 ай бұрын
Amazing tutorial. I really enjoyed it. thanks for the amazing explaination🙏
@codingwithroby
@codingwithroby 5 ай бұрын
Glad you enjoyed it! These types of comments keep me going, thank you 😊
@edderleonardo
@edderleonardo 9 ай бұрын
your fastapi course on udemy is amazing
@codingwithroby
@codingwithroby 9 ай бұрын
Yay! I am glad you enjoy it!!
@ishan.girdhar
@ishan.girdhar 11 ай бұрын
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 11 ай бұрын
You are right! It's tough speaking in front of a camera, sometimes things just come out LOL
@ishan.girdhar
@ishan.girdhar 11 ай бұрын
@@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 11 ай бұрын
You're the best! Thanks for pointing that out for future viewers 🙂
@herozero777
@herozero777 4 ай бұрын
Thanks man for this tutorial. It was amazing well explained and really saved me :)
@codingwithroby
@codingwithroby 4 ай бұрын
You are sooo welcome! Cheers 🥂
@rainymatch
@rainymatch 19 күн бұрын
thanks for the vid. don't see what's the point of the music in the background all the way through? if I want to listen to music, I put on some music.
@codingwithroby
@codingwithroby 18 күн бұрын
I've been getting better and learning KZbin. Future videos don't have music during coding.
@Imperialcodex1
@Imperialcodex1 3 күн бұрын
thanks alot
@codingwithroby
@codingwithroby 2 күн бұрын
You're welcome!
@anuj7286
@anuj7286 Жыл бұрын
Thankyou for sharing the tutorial :)
@codingwithroby
@codingwithroby Жыл бұрын
Of course, glad you are able to find value!
@williamyoutube368
@williamyoutube368 16 күн бұрын
Thanks for the tutorial. Could you suggest some way to mask/hide the SECRET_KEY and ALGORITHM values? One of my motivation of using JWT is to avoid hard-code some password or secret in source code/config file. Thanks.
@codingwithroby
@codingwithroby 15 күн бұрын
You can add to a .env file locally and then store in a secret manager on the cloud.
@karacan2469
@karacan2469 Жыл бұрын
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 Жыл бұрын
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
@vincenzobuonvino5860
@vincenzobuonvino5860 9 ай бұрын
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.
@sowmiyapannerselvam997
@sowmiyapannerselvam997 5 ай бұрын
I am created crud operation using fastapi and python,then I want implement jwt refresh token,pls give any idea
@marteinngumundsson8086
@marteinngumundsson8086 6 ай бұрын
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
@dannisisgt
@dannisisgt Жыл бұрын
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 Жыл бұрын
Thank you! I would recommend using Redis or caching to do this :-)
@default_youtube_profile
@default_youtube_profile Ай бұрын
I converted your tutorial to async sqlalchemy.
@codingwithroby
@codingwithroby Ай бұрын
Nice work!
@thegrind9628
@thegrind9628 8 ай бұрын
19:47 - I want to have a place holder for just the token and not the entire form. How to do that?
@Ken-ix7ms
@Ken-ix7ms 11 ай бұрын
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 11 ай бұрын
You will want the Front End to handle the redirects.
@4crazibility75
@4crazibility75 4 ай бұрын
Thanks for your video. But do you know how I can integrate Clerk to my fast api project?
@codingwithroby
@codingwithroby 4 ай бұрын
Not as of right now sorry, I can look into making a video in the future on it 🙂
@nulops
@nulops 8 ай бұрын
hello , thanks for video, is it possible to share your setup to create this kind of content ? thanks advance
@amiltonmoreira2341
@amiltonmoreira2341 4 ай бұрын
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 4 ай бұрын
Well, hypothetically they can create the JWT from their own code then.
@ModernStockPortfolioTracker
@ModernStockPortfolioTracker 11 ай бұрын
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 11 ай бұрын
Ohh not a bad idea, I will add it to my backlog of future videos 🙂
@gleb29
@gleb29 9 ай бұрын
hey, how i can get the token and save it in browser(how i can use oauth2) without using fastapi docs interface
@diezitrone_111
@diezitrone_111 9 ай бұрын
Hey what ide theme is that?
@codingwithroby
@codingwithroby 9 ай бұрын
One dark pro extension on VS Code.
@timbrap4693
@timbrap4693 Жыл бұрын
Why did you make the get_current_user function async?
@codingwithroby
@codingwithroby Жыл бұрын
It is used for dependency injection if you are wanting to find the current user signed in
@timbrap4693
@timbrap4693 Жыл бұрын
@@codingwithroby but is it necessary to make it async? There is no awaiting inside the function
@appallasuryanarayanababu5074
@appallasuryanarayanababu5074 11 ай бұрын
Hi Eric, Annotated is not supported in python 3.8 version could pls suggest alternative??
@codingwithroby
@codingwithroby 10 ай бұрын
I would recommend upgrading Python, but you do not need to using Annotated. You can pass the Dependency right in the parameter itself.
@learnapplybuild
@learnapplybuild 10 ай бұрын
What tool do you use to record lectures. its just amazing
@codingwithroby
@codingwithroby 10 ай бұрын
Yo! thanks 🙂 I use a mirrorless camera for recording myself, heir PR40 for mircophone and camtashia for recording!
@a.s8840
@a.s8840 Жыл бұрын
how we integrate google auth with this?
@codingwithroby
@codingwithroby Жыл бұрын
Hmm this sounds like a nice video idea!
@a.s8840
@a.s8840 Жыл бұрын
@@codingwithroby Yes I am very curious to learn this ..I am following you udemy course
@a.s8840
@a.s8840 Жыл бұрын
@@codingwithroby can you suggest me how can I integrate.😢😢
@rootifera
@rootifera 8 ай бұрын
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 8 ай бұрын
You bet! Currently I do not for a KZbin video, perhaps that could be a good addition to the Udemy course?
@rootifera
@rootifera 8 ай бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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.
@ERYE
@ERYE Жыл бұрын
How do i connect it with frontend?
@codingwithroby
@codingwithroby Жыл бұрын
Great question! Check out my full stack video : kzbin.info/www/bejne/ZqvFY56lnb6Nf68si=v_eDYYlytP04CNcj
@Gladius000
@Gladius000 Жыл бұрын
Good content. Please look up how to pronounce "schema".
@codingwithroby
@codingwithroby Жыл бұрын
Glad you enjoyed the content 🙂
@Nau-uc9tk
@Nau-uc9tk 10 ай бұрын
where i can get this code?
@codingwithroby
@codingwithroby 10 ай бұрын
I didn't make a repo before deleting the project - sorry 😞
@chikkivenu6632
@chikkivenu6632 11 ай бұрын
Your code is fool proof. But I was wondering how did you map this with your finance app? Please let me know
@codingwithroby
@codingwithroby 11 ай бұрын
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
@alexpyofficial
@alexpyofficial Ай бұрын
The background music was unnecessary.
@codingwithroby
@codingwithroby Ай бұрын
It’s not there for future videos
@alexpyofficial
@alexpyofficial Ай бұрын
@@codingwithroby Thank you brother. Now that you replied back, I am feeling bad for being rude. Great work by the way. Learned from you. ❤
@codingwithroby
@codingwithroby Ай бұрын
@@alexpyofficial lol all good dude
@hrushikeswarareddy251
@hrushikeswarareddy251 7 ай бұрын
also upload link for source code
@codingwithroby
@codingwithroby 7 ай бұрын
Yeah - that's my bad, I don't have it anymore.
@hrushikeswarareddy251
@hrushikeswarareddy251 7 ай бұрын
😅👍
@kompalalokesh5586
@kompalalokesh5586 7 ай бұрын
Background music is annoying
@codingwithroby
@codingwithroby 7 ай бұрын
Sorry my guy
@NightSwamp
@NightSwamp Ай бұрын
turn down your music it's annoying
@codingwithroby
@codingwithroby Ай бұрын
I removed it in future videos.
@rstiekema
@rstiekema 3 ай бұрын
Can I download your code from this tutorial somewhere? Keep up the good work. Btw, very helpful turorial! Thank you so much.
@sivadevil4845
@sivadevil4845 7 ай бұрын
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 7 ай бұрын
Yeah - that's my bad, I don't have it anymore.
@nawaraj.karkee
@nawaraj.karkee 5 ай бұрын
4:45, How the hell do you unhash something, ?????
@codingwithroby
@codingwithroby 5 ай бұрын
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)
@mudassiraqeel8604
@mudassiraqeel8604 6 ай бұрын
but we never used the bearer token or did we ?
@codingwithroby
@codingwithroby 6 ай бұрын
We did 🙂
@JuliusThePianoGuy
@JuliusThePianoGuy 9 ай бұрын
Very nice and structured video! Helped a lot, thanks!
@codingwithroby
@codingwithroby 9 ай бұрын
Great to hear! Glad you enjoyed it!
@marsitschool9495
@marsitschool9495 2 ай бұрын
why we need then token after sign in if authorize user with username and password still
@sandeshvora
@sandeshvora Ай бұрын
Because if user tries to login second time no password is needed...
@LamNguyen-hw9lq
@LamNguyen-hw9lq 11 ай бұрын
Hi, thank you for the tutorial, how do I define a route that is accessible only when an user is logged in?
@codingwithroby
@codingwithroby 11 ай бұрын
You will want to add dependency injection to "get_current_user"
@laszlomagyar3156
@laszlomagyar3156 Жыл бұрын
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 Жыл бұрын
Not when using Swagger - An alternative could be using Postman or another API Test platform and then you can reuse the JWT
@laszlomagyar3156
@laszlomagyar3156 Жыл бұрын
@@codingwithroby Thank you!
@LiftAndC0de
@LiftAndC0de Жыл бұрын
Fantastic video, can you share the Github repo?
@codingwithroby
@codingwithroby Жыл бұрын
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 🙂
@queenqueents-l9s
@queenqueents-l9s 8 ай бұрын
i watched many videos , you explained ,everything perfect , thank you so much , just watched it too late :( ,
@codingwithroby
@codingwithroby 8 ай бұрын
Glad it helped!
@suen-tech
@suen-tech Жыл бұрын
Keep going.
@codingwithroby
@codingwithroby Жыл бұрын
Thank you, that’s the plan!
Deploy FastAPI on AWS Lambda | In 9 MINUTES
9:02
Eric Roby
Рет қаралды 29 М.
Incredible: Teacher builds airplane to teach kids behavior! #shorts
00:32
Fabiosa Stories
Рет қаралды 10 МЛН
Поветкин заставил себя уважать!
01:00
МИНУС БАЛЛ
Рет қаралды 6 МЛН
Officer Rabbit is so bad. He made Luffy deaf. #funny #supersiblings #comedy
00:18
Funny superhero siblings
Рет қаралды 5 МЛН
规则,在门里生存,出来~死亡
00:33
落魄的王子
Рет қаралды 10 МЛН
JWT Authentication for React and FastAPI (easy with code)
11:51
Session Vs JWT: The Differences You May Not Know!
7:00
ByteByteGo
Рет қаралды 159 М.
How To Create a Python API With FastAPI - Full Tutorial
25:39
Tech With Tim
Рет қаралды 46 М.
FastAPI Authentication with  JWT (JSON Web Tokens)
56:18
Bek Brace
Рет қаралды 98 М.
Quickly Authenticate Users with FastAPI and Token Authentication
46:05
Akamai Developer
Рет қаралды 111 М.
Incredible: Teacher builds airplane to teach kids behavior! #shorts
00:32
Fabiosa Stories
Рет қаралды 10 МЛН