FastAPI Authentication with JWT (JSON Web Tokens)

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • This tutorial will teach you how to create authentication in a FastAPI application using JSON Web Tokens.
    Enjoy.
    ✨ FastAPI Website:
    fastapi.tiango...
    ✨ JWT Website:
    jwt.io
    ​​
    ✨ Source Code:
    github.com/Bek...
    🔗 Social Media
    --------------------------
    Facebook : / bekbrace​​​​
    Twitter : / bekbrace
    Instagram : / bek_brace
    DEV profile : ttps://dev.to/bekbrace​​​​
    GitHub profile : github.com/Bek...
    Website : bekbrace.com
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 206

  • @carlosvelazquez7979
    @carlosvelazquez7979 ปีที่แล้ว +19

    Wooo, what a great great Video !!!!!, Excellent I do appreciate it !!!, I've been developing softWare for 40 years ( since I was 14 ), few videos like this one !!, and I've watched lots !!!

    • @BekBrace
      @BekBrace  ปีที่แล้ว +2

      Thank you very much for such kind words 🙏🙂

  • @MohamedBadr-xc7xq
    @MohamedBadr-xc7xq 2 ปีที่แล้ว +33

    Great tutorial man, no animation or crazy graphics, just intelligent man explaining programming! Hats off to you brother

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thank you very much

  • @juanmanero4631
    @juanmanero4631 2 ปีที่แล้ว +4

    you are the FASTAPI king of youtube , man !!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +2

      hehe - Thanks for the compliment :)

    • @juanmanero4631
      @juanmanero4631 2 ปีที่แล้ว

      @@BekBrace the truth ma man

  • @luizferez89
    @luizferez89 2 ปีที่แล้ว +2

    Amazing , thanks a lot I was waiting FastAPI, thank you for answering the request, you really deserve more views

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you

  • @marcus7387
    @marcus7387 2 ปีที่แล้ว +3

    Friday night tutorial! Thank you Bek, great work

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thank you

    • @marcus7387
      @marcus7387 2 ปีที่แล้ว

      @@BekBrace please can you make tutorial on FastAPI and machine learning?

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      @@marcus7387 sure, it’s doable 🙂

  • @АртемЕршов-р9е
    @АртемЕршов-р9е 4 หลายเดือนก่อน +1

    Это отличное видео! Всё чётко и подробно!

    • @BekBrace
      @BekBrace  4 หลายเดือนก่อน

      Огромное тебе спасибо, друг 🙏

  • @gerardmunoz6075
    @gerardmunoz6075 2 ปีที่แล้ว +4

    Thanks Bek Brace for great tutorial, it was difficult for me as beginner, but I learned a few things too and will continue to learn fastapi because it's the best

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Glad to hear that - thanks for watching

  • @pawelsb
    @pawelsb 2 ปีที่แล้ว +2

    Awesome quality content as always, keep it up Bek ✨

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thanks so much Pawel 🙂 🙏

  • @DevBishwasBh
    @DevBishwasBh 2 ปีที่แล้ว +7

    Wow, you already have an awesome tutorial on FastAPI Authentication. I am waiting for a tutorial on integrating it with Next Js [possible with Next Auth].

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +2

      There will be :)

    • @DevBishwasBh
      @DevBishwasBh 2 ปีที่แล้ว +2

      ​@@BekBrace Great! That tutorial will begin my renewed webdev journey with FastAPI and Next Js. Thanks for the efforts 😊

  • @shamansharif
    @shamansharif ปีที่แล้ว +1

    Just subscribed. This helped me a lot. I can't thank you enough.
    Love from Bangladesh.

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Tha k you my friend, I appreciate your support to the channel

  • @MuhammedBasil
    @MuhammedBasil 2 ปีที่แล้ว +2

    Amazing Tutorial. Crisp and Clear. Thank you !.

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thank you so much Mohamed - Happy Ramadan to you

    • @MuhammedBasil
      @MuhammedBasil 2 ปีที่แล้ว

      @@BekBrace i just tried the same now. No hiccups, very easy to follow. Thanks again mate.
      Happy Ramadan to you too.

  • @DeepakTandale-z9f
    @DeepakTandale-z9f 8 หลายเดือนก่อน

    Hey, Bek have just subscribed your channel for developing & learning software skiils,
    I like your teaching skills that there is silent background music with your slowly teaching techniques & explanation + hands-on.
    your videos on Django is on of the great Tutorial for me ...!
    Thanks man...

    • @BekBrace
      @BekBrace  7 หลายเดือนก่อน +1

      Thank you! Stay sharp and good luck to your programming journey, man

  • @davidmiricho385
    @davidmiricho385 2 ปีที่แล้ว +1

    Great video on FAST API. Loving it

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you so much, David

  • @ChrisShawUK
    @ChrisShawUK 2 ปีที่แล้ว +1

    great video Bek. Super clear to follow all the steps

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you very much, Chris 🙏

  • @yura2110
    @yura2110 2 ปีที่แล้ว +1

    keep going ! this was a really clean video !!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you so much Yura !

  • @MycalA-z7j
    @MycalA-z7j ปีที่แล้ว +1

    Thank you man,it was a nice one,really learnt and understood what i was working on

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thank you very much, my friend

  • @pramudyadika_
    @pramudyadika_ 6 หลายเดือนก่อน +1

    thanks! ur explanation is crystal clear!

    • @BekBrace
      @BekBrace  6 หลายเดือนก่อน

      Thank you very much 🙂

  • @lfcamacho
    @lfcamacho 2 ปีที่แล้ว +6

    great video! as mentioned before by other people, the only issue with this, is the authorization at the end... you can simply type any key instead of the generated token, even a single character and it will grant you access to the Post method. How can we make sure the entered token is the same one we generated?

    • @rahulsharma-cb7kk
      @rahulsharma-cb7kk 2 ปีที่แล้ว +1

      Yes that's the problem the route are not authenticated anyone can access it

  • @JemilMarcosTyC
    @JemilMarcosTyC ปีที่แล้ว

    Thanks a lot! very helpfull, BUT, if I try this on postman, no matter what string I put on Authorization it will bypass security and let me post... I also added the retraint to be a token bearer to even list the penguins, tigers and stuff and it let me through

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      I am aware of the issue, once i have the time i will check out deeper and write the solution in the description - thanks for the heads-up though

  • @vini_yz
    @vini_yz ปีที่แล้ว +1

    Really great tutorial man! It helped me a lot. Thanks for the content

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Good to hear my friend

  • @michaelehrenreich
    @michaelehrenreich 11 หลายเดือนก่อน +1

    Excellent, I enjoyed this video very much!

    • @BekBrace
      @BekBrace  11 หลายเดือนก่อน

      Thank you very much 🙏☺️ Always glad to receive such feedback 🙏

  • @jdratlif
    @jdratlif 2 ปีที่แล้ว +2

    JWTs have built-in expiration times. Why did you make a custom field? pyJWT will throw an exception for expired token.

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      I think I was trying to add more logic, but you’re right it would’ve been better to keep it simple and leave it to pyjwt to handle it 😁

  • @monasser6
    @monasser6 2 ปีที่แล้ว +2

    Great course, thank you Bek Brace

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thank You

  • @avinashpatel6745
    @avinashpatel6745 2 ปีที่แล้ว +5

    Hello sir, thank you for great tutorial
    can you please show in other video how to show this with a frontend framework instead of showing in postman or fastapi swagger ?
    very good explanation, i believe you deserve more views. Regards from Pune, India

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +6

      Thank you so much Avinash , your words mean a lot really. It’s an awesome idea, I can make a FastAPI Vue tutorial with JWT authentication - just added this to the three months plan for channels vids. Thanks 😊

    • @avinashpatel6745
      @avinashpatel6745 2 ปีที่แล้ว +1

      @@BekBrace Thank you sir for responding. God bless you

  • @kadiryaren5564
    @kadiryaren5564 ปีที่แล้ว +1

    just one word! Perfect!

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thank you very much

  • @piuskariuki5295
    @piuskariuki5295 11 หลายเดือนก่อน +1

    This is friggin AWESOME

    • @BekBrace
      @BekBrace  11 หลายเดือนก่อน +1

      Thank you so much 🙂🙏

  • @mawe4585
    @mawe4585 2 ปีที่แล้ว +1

    Thanks for the tutorial. Sadly all tutorial about JWT and Authorization in general for FastAPI, leave out the part where you use the user-information in later calls.
    Like adding the author of the blog-post in this example.

  • @ahmedqassem4326
    @ahmedqassem4326 2 ปีที่แล้ว +2

    Thanks man!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Welcome :)

    • @ahmedqassem4326
      @ahmedqassem4326 2 ปีที่แล้ว

      @@BekBrace can fastapi be integrated with another language ?

  • @georgesamuel3774
    @georgesamuel3774 2 ปีที่แล้ว +1

    Great turtorial bro , keep it up ❤️❤️

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you 🙏

  • @guruyaya
    @guruyaya 2 ปีที่แล้ว +3

    Nice tutorial. There's one thing I didn't get: on def user_signup(user: UserSchema = Body(default=None)): - I tried it without the Boy default value, and it worked just the same. What's the use of the Body default value there?
    Thanks in advance

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Hey Yair thanks for your comment.
      You can delete the default if you want to keep it simple with no extra arguments, that’s why you’ll have the same result; however if the userschema body is set to be a different default value, it will behave differently. I just put it to be visually clear if anyone wants to change the default value.

    • @guruyaya
      @guruyaya 2 ปีที่แล้ว

      @@BekBrace so this helps me to set the default behavior, if no json is sent. Got it.

  • @amandubey5287
    @amandubey5287 ปีที่แล้ว +1

    Thank you so much for this, you gained a sub

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thank you so much my friend

  • @kriskrawiec5513
    @kriskrawiec5513 ปีที่แล้ว +1

    Thanks man, very helpful

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      You're very welcome

  • @tanjimulislam4762
    @tanjimulislam4762 2 ปีที่แล้ว +2

    Thank you!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      You are very welcome 🙂

  • @dab_tube
    @dab_tube ปีที่แล้ว +1

    Easy clear and concise

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thank you very much

  • @jnone2488
    @jnone2488 ปีที่แล้ว +1

    what is the differences between JWT, JOT and access token ?
    I will wait for the answers pls. Thanks!

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Hello my friend.
      Sure.
      JWT (JSON Web Token) and JOT (JSON Object Token) are both types of access tokens used for authorization and authentication, while access token is a more general term used for any token that grants access to a protected resource.

  • @keycuevasmelgarejo
    @keycuevasmelgarejo 2 ปีที่แล้ว +2

    Great!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Thank you

  • @salmoseproverbiosilustrados
    @salmoseproverbiosilustrados 2 ปีที่แล้ว +1

    I enjoyed a lot. Thanks

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you, Ricardo!

  • @portiseremacunix
    @portiseremacunix 2 ปีที่แล้ว +2

    Thanks. I have the 403 error at the last stage to test the JWT bearer. Can I see the source code somewhere?

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Hi Kirby, thanks for commenting
      Yes, I've added it in the description.
      Here's the link: github.com/BekBrace/FASTAPI-and-JWT-Authentication

  • @lerealiste3338
    @lerealiste3338 ปีที่แล้ว +1

    It’s doesn't work. It's getting access to post with random symbols instead token.

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      It should work, this is odd. Do you have an error message ?

  • @jcpalumboai1
    @jcpalumboai1 2 ปีที่แล้ว +1

    Great video! Thanks

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      I'm glad 🙂

  • @sziller_eu
    @sziller_eu ปีที่แล้ว +1

    In the last step, does it give an error if I slightly change the token pasted? Also how can I read data from the token once inside a function?

  • @basilihuoma5300
    @basilihuoma5300 ปีที่แล้ว +1

    This is super cool, thanks.

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thanks my friend

  • @hritikcodes876
    @hritikcodes876 2 ปีที่แล้ว +1

    hey i don't understand the use of Config class in Schemas, can you please explain the benefit of adding Config class in a Schema?

  • @LearnWithAshiii
    @LearnWithAshiii 11 หลายเดือนก่อน

    You have Done a Great Job
    respect from Pakistan ❤

    • @BekBrace
      @BekBrace  11 หลายเดือนก่อน

      Thank you very much 🙏😊

  • @superspamtube7464
    @superspamtube7464 ปีที่แล้ว +1

    After finishing this video my login route doesnt work. But the signup works and I can POST new blog posts and get all and get one post works. I have a import error "Import jwt could not be resolved". I know this video is one year old but maybe you can assist with that.

    • @BekBrace
      @BekBrace  ปีที่แล้ว +2

      I will do my best finding the solution 🙂

  • @ndreuh
    @ndreuh ปีที่แล้ว +1

    Great video!
    Say, why do you set Schema = Body() in some functions, what's that for?

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      For standardization purposes nothing more

  • @mjavint
    @mjavint ปีที่แล้ว +1

    great course my friend

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Thank you very much my friend

  • @TusharBhut-mk6mn
    @TusharBhut-mk6mn ปีที่แล้ว +1

    Can you make video on different authentication for both user and admin, Where user authorized can only use [GET] method, while admin authorized can use all the method [crud] ?

  • @jimlyjimlesson4502
    @jimlyjimlesson4502 2 ปีที่แล้ว +1

    Hi Bek, thanks fr ur lesson. very interesting and a very good speak! But i have an problem and can't find it. If i tried to enter the post with Dependencies, i get alwyas an Error: jwt_bearer.py", line 17, in __call__
    raise credentials.credentials
    TypeError: exceptions must derive from BaseException

    • @podcastmarhaba
      @podcastmarhaba 2 ปีที่แล้ว

      What dependencies are you trying to post with ?

  • @sawojarianski6991
    @sawojarianski6991 ปีที่แล้ว +1

    How do You placed sample data "Tiger, Koala, etc. " , Is this a special addon for that functionality ?

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      But it's clear how i did it in the tutorial

  • @STEFFIGEORGE-tw8wz
    @STEFFIGEORGE-tw8wz ปีที่แล้ว +1

    Thank you so much!!!!

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      You're welcome 🤗

  • @vfb6265
    @vfb6265 ปีที่แล้ว +1

    You miss this code in jwtBearer Class :
    async def __call__(self, request: Request):
    [...]
    if credentials:
    [....]
    if not self.verify_jwt(credentials.credentials):
    raise HTTPException(status_code=403, detail="Message here !")
    return credentials.credentials
    else:
    raise HTTPException(status_code=403,detail="Message here !")

  • @kz_cbble9670
    @kz_cbble9670 6 หลายเดือนก่อน +1

    Even if i post the wrong acces token in authorize , it still allows me to post.. what's wrong?

    • @AcezeroGame
      @AcezeroGame 6 หลายเดือนก่อน

      I had same issue and turns out we forgot to add verify_jwt in jwt_bearer file. Simply add an if statement below if not credentials statement.
      Just check if not self.verify_jwt with credentials.credentials.

  • @startmusicworld666
    @startmusicworld666 2 ปีที่แล้ว +1

    Hello Sir, I got this error can u provide solution for this ----->JWT_SECRET = config("secret"), raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
    decouple.UndefinedValueError: secret not found. Declare it as envvar or define a default value.

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Ok, maybe you can download the source code and check out what went wrong? This way you will teach yourself while taking notes ☺️

  • @MaxoSall
    @MaxoSall 4 หลายเดือนก่อน

    I have yet to see any tutorials about actual atheoriaztion and permissions in fastapi

    • @BekBrace
      @BekBrace  4 หลายเดือนก่อน

      You're welcome

  • @htwray
    @htwray 2 ปีที่แล้ว +2

    HI Bek Brace, this is an excellent tutorial. I am very new in using JWT authentication. Just want to know if there are any token refresh mechanism in pyjwt package? Or any other recommendation on the token refresh ? Thanks in advance!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Hey 😊👋 thanks for your comment
      I'm pretty sure there are, I'm gonna check that and be back to you with an answer to your question

    • @htwray
      @htwray 2 ปีที่แล้ว

      Thank you for your help! I will also study for it.

  • @md.redwanhossain6288
    @md.redwanhossain6288 ปีที่แล้ว +1

    can you kindly cover refresh token and token blacklisting in another video?

  • @apexyu164
    @apexyu164 ปีที่แล้ว +1

    why you need to add Body(default = None) line 66 in 39:55 ? whats the purpose of that ?

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Really nothing more than showing how a standard template. You can omit it and it will still work.

  • @kiransingh2935
    @kiransingh2935 ปีที่แล้ว +1

    How does fastAPI know to call the verify_jwt function on the handler? Is that some special baked in logic?

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Exactly that

  • @jasenmackie1028
    @jasenmackie1028 2 ปีที่แล้ว +1

    Thanks for the video. Is the code available anywhere? I could not get the token on user signup and not sure why. Would like to compare my code. Thanks!

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Hey Jasen, thanks for commenting.
      Yes, I've added the source code in the description.
      Here's the link: github.com/BekBrace/FASTAPI-and-JWT-Authentication

  • @orlanino
    @orlanino 2 ปีที่แล้ว

    What about authorization, please? Also how do you propagate the authentication and authorization to backend microservices?

  • @b20lopez
    @b20lopez ปีที่แล้ว +1

    Hi Bek!
    I am looking for "API KEYS"
    You know, a key I can provide to customers to they communicate to the backend.
    I need API keys to deduct credits from their account.
    Is that the same API KEYS I am looking for? Where I just have to remove the Expiration?
    I watch a video that API keys can be sniffed.. so I wonder if this is the same API key i am looking to implement

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      I'll look into this and get back to you

  • @CodingByAmp
    @CodingByAmp ปีที่แล้ว +1

    thank you , follower

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      thank you for watching

  • @jordixboy
    @jordixboy 2 ปีที่แล้ว +1

    how would u get user_id from the jwt in the controller?

  • @johnniefujita
    @johnniefujita 2 ปีที่แล้ว +3

    it is important to mention that on that category the fastest frameworks are around 6x faster than fastapi. But it is indeed fast enough and very fast for python. I have been working in companies migrating their services for fastapi from django and flask mainly

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Oh 6x faster 😯 ? Thanks for the information, Johnnie.

    • @davidcostigan8816
      @davidcostigan8816 2 ปีที่แล้ว

      interesting

    • @erjiaxiao6577
      @erjiaxiao6577 ปีที่แล้ว

      Hi Johnnie, so you mean fastest framework in Java and Go etc. ?

    • @johnniefujita
      @johnniefujita ปีที่แล้ว

      @@erjiaxiao6577 yes... java, go, rust, c++ and even node frameworks are faster. But we are usually bounded by other factors when dealing with rest api backend servers. So that does not really become a decision factor. At least.

    • @erjiaxiao6577
      @erjiaxiao6577 ปีที่แล้ว

      @@johnniefujita I see.. I check some benchmark and find that python nowadays could has its position with these contestants. good news for those who like python. thank you for the info!

  • @josetuc
    @josetuc 2 ปีที่แล้ว +1

    It's a great tutorial, but... HOUSTON WE HAVE A PROBLEM =(( : when I added dependencies=[Depends(jwtBearer())]...
    In my console I can read this error: TypeError: HTTPBearer.__init__() got an unexpected keyword argument 'auto_Error'... Could you help me please? Thanks a lot

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      Hey Gomez 👋 I’ll check it out and will be back to you with an answer to this issue

    • @josetuc
      @josetuc 2 ปีที่แล้ว

      @@BekBrace thanks a lot my dear friend, you're the Best!!!

    • @alexandere9723
      @alexandere9723 2 ปีที่แล้ว

      @@BekBrace write is small, auto_error = auto_Error

  • @razapanjwani6502
    @razapanjwani6502 7 หลายเดือนก่อน

    How does the server tell client side that a user is logged in?

  • @도현김-x4n
    @도현김-x4n 2 ปีที่แล้ว +1

    The api works even though it is not an issued token. I do not know the reason. why...??

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      It should work for reading [Get] verb, but not for writing [POST] - is that the case ?

    • @도현김-x4n
      @도현김-x4n 2 ปีที่แล้ว

      @@BekBrace Both [post] and [get] are the same case.

  • @devpriyashivani1855
    @devpriyashivani1855 10 หลายเดือนก่อน

    Tried multiple times but finally got the error: { "detail": "Invalid token or expired token." }

    • @BekBrace
      @BekBrace  9 หลายเดือนก่อน

      Oh sorry to hear that, but I can advise you to make sure that your JWT token you are using for authentication is valid and has not expired, make sure to generate a new token if the existing one has expired.

  • @amarek5841
    @amarek5841 2 ปีที่แล้ว

    Very nice 👍 thanks for the good job

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thanks 🙏

  • @yashgajewar9019
    @yashgajewar9019 2 ปีที่แล้ว

    I followed the tutorial but whenever I try to post data I get an error that token is invalid has anyone faced the same issue??

  • @MycalA-z7j
    @MycalA-z7j ปีที่แล้ว

    hi bek,if i want to update user information using PUT method of a specific user,how can i use the token that was generated in login and user the same token in PUT method to represent that same user and update only the details of that specific user

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Hey, thank you for this question :)
      Actually, you can update user information using the PUT method for a specific user by the following:
      1- Token-Based Authentication: Set up token-based authentication in your FastAPI application. When a user logs in successfully, generate a JWT token and return it to the client. The client should store this token securely.
      2- Protect Routes: Protect the routes or endpoints that require authentication and user information updates. Only authenticated users with valid JWT tokens should be able to access these routes.
      3- Client-Side: When making a PUT request to update user information, include the JWT token in the request header as an "Authorization" header with the "Bearer" prefix:
      Authorization: Bearer
      As far as thee Server-Side (FastAPI), you need to:
      1- Authenticate the user: Verify that the provided JWT token is valid and decode it to extract the user's identity (usually the user's ID).
      2- Authorize the update: Ensure that the user making the request has the necessary permissions to update the user information. Typically, users can only update their own information, so you should check if the user ID extracted from the token matches the user being updated.
      3- Update the user information: If authentication and authorization checks pass, proceed to update the user's information in your database based on the data provided in the PUT request.

  • @mycala6
    @mycala6 ปีที่แล้ว

    Can anyone help me out.
    I have one doubt suppose you have 2 users (student and admin)how to use the generated token during login, only for the student specified tag user I logged in and it's methods and that token should not be able to be used by the admin methods,
    To perform operations on admin again we need to create token for that.

    • @BekBrace
      @BekBrace  11 หลายเดือนก่อน

      Hey!
      You can try to implement role-based access control (RBAC) in your authentication and authorization system for the student and the admin, each of which will have separate tokens and permissions.

  • @jeanleloup2205
    @jeanleloup2205 2 ปีที่แล้ว

    Thank you for the tutorial it was very good. What about put and delete requests ? Thank you

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thanks Jean
      The same as we post, follow the same rules , and as for creating the routes you can check out my FastAPI course, you’ll find your answer there

  • @mehul4mak
    @mehul4mak 11 หลายเดือนก่อน

    @21.24 how come you are getting I'd 4 and 5 if you are posting Id 0?

    • @BekBrace
      @BekBrace  11 หลายเดือนก่อน +1

      Great question. The id 0 in the curl link doesn't necessarily mean the actual id number of the item added, it starts at 0 and increments based on previously added items, in this case as explained I've added cats twice, that's why the GET request showed it twice at I'd 4 and 5.

  • @enchance
    @enchance ปีที่แล้ว

    Can you do a video on the FastAPI Users package with Google Auth? I think having OAuth2 authentication is something people are really after because of its ease of use.

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      Yes, i will do that with oauth2 soon

    • @enchance
      @enchance ปีที่แล้ว

      @@BekBrace Awesome! Will be looking out for it.

  • @dogaada6619
    @dogaada6619 2 ปีที่แล้ว +1

    Hello sir, thanks for great tutorial. I would like to ask you about jwtBearer Process. I applied your code in another project but the problem is which value I entered to jwtBearer is not important to authorize the endpoint. I would like to secure my public endpoint with verify_token. Also I created /login endpoint and after a username and password process for authenticate I can get access_token. As I mentioned in above, I would like to use this access_token to authorize in different public endpoints. Would you help me about it?

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Sure i can help. you can use this access_token for auth in different public endpoints. How do you want to start?

    • @dogaada6619
      @dogaada6619 2 ปีที่แล้ว

      I would like asking about I can secure with token the endpoints ok but the problem is instead of access_token with any value which I enter the authentication point I can authorize the endpoint. Normally I think I have to give an acces_token which I get from the login endpoint, after that I can be authorised the post method which is secured with token. So I confused about that how I could secure with only access_token not any value. Also I looked into source code of you shared, when I apply it to my project. I cannot authorize with access_token and any value

    • @ickynho7
      @ickynho7 2 ปีที่แล้ว

      @@dogaada6619 simply add new field key value like role: admin, and a condition if its admin then allow.

  • @AiF0
    @AiF0 ปีที่แล้ว +1

    Man can you give me your keyborad's name and great video

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      Thank you my friend.
      Keyboard's name is Genesis Thor 300

  • @carlosvelazquez7979
    @carlosvelazquez7979 ปีที่แล้ว

    I found how to verify the correct token within this code

  • @DevArjun
    @DevArjun 2 ปีที่แล้ว

    Sir How To Add Current User With Post as well like post : 1, title: snakes, text: they are cool, author: current_user

  • @BestChan2610
    @BestChan2610 ปีที่แล้ว

    Can you help integrate this into a frontend framework like Svelte?

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      Don't have much experience with Svelte, sorry

  • @amandubey5287
    @amandubey5287 ปีที่แล้ว +1

    I just have two questions like why are we putting
    1) why are we doing this decoded_token["expiry"] >= time.time()
    2)Why are we returning the tokens via signJWT() method two times one time via signup and the other time via signup, can you explain that to me

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      Thanks for your questions, my friend.
      Answering your questions:
      1- The decodeJWT function takes the token and decodes it with the aid of the jwt module and then stores it in a decoded_token variable. So what we want is to return decoded_token if the expiry time is valid which is exactly the answer to your question, otherwise, we return None.
      2- In signup, I returned signJWT only once not twice :
      @app.post("/user/signup", tags=["user"])
      async def create_user(user: UserSchema = Body(...)):
      users.append(user)
      return signJWT(user.email)

  • @wihanuys
    @wihanuys ปีที่แล้ว

    Hi @BekBrace, I use this tutorial, but I can enter any string in the authorize window in FastAPI docs and it will accept it as a valid authentication. there must be an error somewhere in the video, could you please verify it

    • @BekBrace
      @BekBrace  ปีที่แล้ว +1

      Ok

    • @wihanuys
      @wihanuys ปีที่แล้ว

      @@BekBrace Thank you, I copied it excatly, but I can enter any string, into the authorization window, even if it is not a JWT token, and I have access to all the protected routes

  • @Alexey-f7d
    @Alexey-f7d 2 ปีที่แล้ว +1

    Thanks

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you!

  • @earvinjamesdantes5800
    @earvinjamesdantes5800 2 ปีที่แล้ว +1

    How will I implement the register with a confirm password?

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว +1

      With a very simple conditional to check if the pass entered 2nd time == 1st time, then continue; else raiseerror (not identical password ) - that’s the main idea, I’m sure you can search for the syntax

    • @earvinjamesdantes5800
      @earvinjamesdantes5800 2 ปีที่แล้ว

      @@BekBrace Ohhhhh I thought you're gonna have to create like a new schema/model with the confirmationPassword as its field. Thank youuuuu!

  • @narutokun5093
    @narutokun5093 2 ปีที่แล้ว +1

    👏 👏 👏 👏 👏 👏

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank you :)

  • @bebomohamed8860
    @bebomohamed8860 ปีที่แล้ว

    thanks alot 🌹🌹

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      You're welcome 😊

  • @alwaysdjango
    @alwaysdjango ปีที่แล้ว

    Pls how do we identify the authenticated user??

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      what do you mean by "identify" ?

    • @alwaysdjango
      @alwaysdjango ปีที่แล้ว

      @@BekBrace Like how do you get the authenticated users credenttials from the secured routes.

  • @arknexter
    @arknexter ปีที่แล้ว

    Link to github doesn't work

    • @BekBrace
      @BekBrace  ปีที่แล้ว

      It is working fine

    • @arknexter
      @arknexter ปีที่แล้ว

      Strange. Now it works

  • @phatvo2739
    @phatvo2739 2 ปีที่แล้ว

    can you help me doc about authen with social media

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      What’s happening ?

  • @kz_cbble9670
    @kz_cbble9670 6 หลายเดือนก่อน

    37:46

    • @podcastmarhaba
      @podcastmarhaba 6 หลายเดือนก่อน

      ?

    • @BekBrace
      @BekBrace  6 หลายเดือนก่อน

      Ok .. what ?

    • @kz_cbble9670
      @kz_cbble9670 6 หลายเดือนก่อน

      @@BekBrace sorry i should have added a ignore message.. these are just timestamps for my use

  • @امینجمالی-خ9ص
    @امینجمالی-خ9ص 6 หลายเดือนก่อน

    i wish it was more deep

    • @BekBrace
      @BekBrace  6 หลายเดือนก่อน

      It's as deep as it gets 😉

  • @mr.deep.
    @mr.deep. 2 ปีที่แล้ว +1

    Thanks

    • @BekBrace
      @BekBrace  2 ปีที่แล้ว

      Thank You