FastAPI Authentication Example With OAuth2, JSON Web Tokens and Tortoise ORM

Поділитися
Вставка
  • Опубліковано 19 сер 2024
  • In this video, I will show you how to implement authentication in your FastAPI apps. OAuth2 will be the type of authentication I demonstrate because it's very common among APIs, and I will use JSON web tokens (JWT) for the token in the OAuth flow. In addition to authentication, I'll use Tortoise ORM for the user model.
    Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted....
    Get the code here: prettyprinted....
    Twitter: / pretty_printed
    Github: github.com/pre...

КОМЕНТАРІ • 86

  • @ofranc6204
    @ofranc6204 5 місяців тому +3

    I am watching this 3 years later and it's still the best tutorial on this topic that I have found. Thanks for this.

  • @10a3asd
    @10a3asd 3 роки тому +6

    Ugh man, this is great. I'm learning OAuth2 and FastAPI at the same time and this was incredibly helpful to see the pieces work from the developer side.

    • @wendel1210
      @wendel1210 3 роки тому

      me too, open connect too

  • @mbalslow
    @mbalslow 3 роки тому +22

    Fantastic video, very clear and to the point! Thank you. For a follow-up video (could simply build on top of this code), it would be great to see how to implement expiration in the tokens and a (maybe automatic) refresh logic. Maybe with a dummy (just plain) HTML login page. Anyways, thanks for the great content.

    • @suyashsingh675
      @suyashsingh675 2 роки тому

      Did you find any reading material @mathias ?

  • @Smoonwalkerm
    @Smoonwalkerm 3 роки тому +13

    awesome!! I was waiting for you to upload fastapi videos!!

  • @jarvis7698
    @jarvis7698 Рік тому +1

    Not only teaches authentication but also how a tutorial should be. Excellent demonstration & to the point. Thanks a lot for this :)

  • @tonidelacalle
    @tonidelacalle 11 місяців тому

    Excelent video. I wasn't expecting to get so much knowledge in just 40 minutes. And very well explained!!! Thanks a million.

  • @HG-mt3vl
    @HG-mt3vl 3 роки тому +6

    This is what makes your video stands out from other low quality content producers

  • @Vijay-Yarramsetty
    @Vijay-Yarramsetty 3 роки тому +3

    so structured, so modular. lately found this channel. you are way good in explaining things. Thanks a lot.

  • @jacksonfeng4327
    @jacksonfeng4327 Рік тому

    Best video on fastapi oauth❤

  • @TheAremoh
    @TheAremoh 3 роки тому +10

    Awesome as always. How about Some front-end registration and login form to help understand this better. Just me asking for more though.
    Thanks for this video.

  • @jccorman5848
    @jccorman5848 Рік тому

    This video actually gave me some insights into Pydantic, which I'm still trying to wrap my head around. So a tutorial using Pydantic more explicitly would be great. But you have a great back catalog already.

  • @ndk87
    @ndk87 3 роки тому +2

    Thanks so much! Your videos really helped me some years ago when I was coding in Python flask, and just as I start working on FastAPI you begin making videos in that area too 🙌🙌

  • @HitraNtheProgrammer
    @HitraNtheProgrammer 3 роки тому

    Very educational! I'm glad I ran into your channel

  • @juanfranciscolopez9968
    @juanfranciscolopez9968 3 роки тому

    Very nice video and excellent channel!! looking forward to see how to protect FastAPI with OAuth2 but just acting as a Resource Server (and getting the token from and external Authorization Server). Thanks for sharing!

  • @rizkydarmadi3196
    @rizkydarmadi3196 2 роки тому

    very useful video, God bless you. love from jakarta

  • @jonxslays
    @jonxslays 2 роки тому

    This video helped me a ton thanks dude.

  • @airoomstyles
    @airoomstyles Рік тому

    great video, thanks!

  • @freepythoncode
    @freepythoncode 11 місяців тому

    Thank you so much 🙂❤

  • @felixnyamongo
    @felixnyamongo Рік тому

    Bro you are awesome

  • @Fsoza2008
    @Fsoza2008 3 роки тому

    Real good videos man! Been real helpful

  • @ItzSenaCrazy
    @ItzSenaCrazy 3 роки тому

    Well explained. Ty very much!

  • @djanthony6662
    @djanthony6662 3 роки тому

    Awesome, thank you Anthony!

  • @adriancroitor5154
    @adriancroitor5154 3 роки тому +5

    Thank you. You always provide a good start point for development.
    Do you think it is a good ideea to use tortoise-orm in production ?
    Some devs say sqlalchemy is better for speeding up your api. I can configure sqlalchemy for a sync def, but failed configuring it for async.

  • @MADAHAKO
    @MADAHAKO 3 роки тому

    VERY cognitive! Thank you!

  • @Avico78
    @Avico78 3 роки тому +1

    great tutorials , will u consider to create tutorial for fastapi+ frontend ? it could be very interesting to see how u use th jwt + login screen .

  • @bernardacquah3885
    @bernardacquah3885 Рік тому

    can't seem to find any records in my DB.
    I know it's there though because it detects when I try to add a duplicate.
    What am I doing wrong?

  • @johnmichaelbitancor913
    @johnmichaelbitancor913 3 роки тому

    Thanks Anthony!

  • @JordanCote
    @JordanCote 3 роки тому +2

    Very nice introduction! Do you have a video explaining how we could extend from this example, attaching this with some other OAuth2.0 providers like Google? Thanks!

  • @ziaddwagy8973
    @ziaddwagy8973 2 роки тому +1

    thanks and that's awesome work . one more question now should pass the user in all new additional endpoints ??

  • @athulkrishnadiyil1735
    @athulkrishnadiyil1735 3 роки тому

    Please do a video on a chat / Meeting app setup using Fastapi+ MongoDB asyncioMotorClient

  • @wa.6006
    @wa.6006 2 роки тому

    Thanks!

  • @ambejar13
    @ambejar13 3 роки тому +1

    LOVE THIS

  • @serg888fert4
    @serg888fert4 Місяць тому

    hi, sir. I get missing.
    TypeError: OAuth2AuthorizationCodeBearer.__init__() missing 1 required positional argument: 'authorizationUrl'
    help me please.

  • @techiecurries1355
    @techiecurries1355 Рік тому

    How can authenticate a fastapi which doesn't have a username and password. I mean no login credentials..

  • @rangabharath4253
    @rangabharath4253 3 роки тому +1

    Awesome thanks so much

  • @aureliojmnz2959
    @aureliojmnz2959 2 роки тому

    Awesome...this has helped so much. Any two-factor authentication, or 2FA implementation in the future?

  • @sheddtutt7875
    @sheddtutt7875 2 роки тому

    I want to implement auth0 token on my existing app but I really need to know if token will increase in size / length if I store more user informations on it e.g permissions (to be used on frontend)

  • @emiliod90
    @emiliod90 3 роки тому +2

    Hey dude, if you ever work out how to do the Authorisation Code Grant with Proof Key using FastAPI I will gladly pay 👍

  • @danielschmider5069
    @danielschmider5069 3 роки тому

    your payload should definitely include the expiration date of the token

  • @thesongmade
    @thesongmade 2 роки тому

    Great video, but i cant manage to user if condition with awaits, it only return the default detail message, Ex: user = await User.get(username=username), if not user statment will not work, only inside a try catch structure, can someone help me?

  • @harrisonfok8267
    @harrisonfok8267 3 роки тому +1

    It didn't generate the sqlite files for me for some reasons. Do you know why?

    • @ABK6969
      @ABK6969 9 місяців тому

      Make sure you're running `hypercorn main:app --reload` and not `hypercorn example:app --reload`

  • @davutlu60
    @davutlu60 3 роки тому

    Thank you so much for the video.. I want to try it with postgres but I could not find a way.. any recommendation is welcome..

  • @_rameez
    @_rameez 2 роки тому

    Great video.
    One think i don't understand. We have to login into the database before accessing any tables.
    Lets say i am using react and fastapi.
    User form data will be sent to fastapi's /login endpoint.
    Here i will try to create a db connection based on forms username and password. If sucess then create a sessionpool. or return exception.
    But you are directly accessing the db. HOW

  • @vashistnarayansingh5995
    @vashistnarayansingh5995 3 роки тому

    how fast api handles multiple requests, as we have threading as a parameter in flask to handle multiple request how does fast api do it?

  • @yourdadsbestfriend7101
    @yourdadsbestfriend7101 3 роки тому

    more fastapi !!

  • @oj43085
    @oj43085 Рік тому

    Nice.

  • @TheAremoh
    @TheAremoh 3 роки тому

    At the right time

  • @carlosaugusto-mo2hb
    @carlosaugusto-mo2hb 2 роки тому

    I was able to perform the whole process of validation using PostMan , however I don’t know what username and password to input when I click in the button AUTHORIZE in swagger.
    Where did you register that ?

  • @hackwithharsha5228
    @hackwithharsha5228 3 роки тому

    Thank you !! If possible, can someone explain difference between uvicorn, gunicorn, hypercorn ?

  • @Jesufemi_O
    @Jesufemi_O 3 роки тому

    awesome video! can you do something on microservices soon ?

  • @bokistotel
    @bokistotel 3 роки тому

    Can you make an example using a OAuth2AuthorizationCodeBearer ?

  • @thecoasterfollower3564
    @thecoasterfollower3564 Рік тому

    You had something in the authorization box of the postman token request but you didnt show it, doing it the way you showed wont work unless you had something there and forgot to say anything

  • @clearthinking5441
    @clearthinking5441 11 місяців тому

    is there a way to do it with email rather than username?

  • @tusharsnn
    @tusharsnn 3 роки тому

    how to get bash inside windows pc like you have ?

  • @puresocial3594
    @puresocial3594 2 роки тому

    great

  • @harrisonfok8267
    @harrisonfok8267 3 роки тому

    How come my VS code doesn't have code recommendations when I'm typing? Which extension did you install?

  • @rahulsriram6295
    @rahulsriram6295 3 роки тому

    Please make a similar video on Django Rest Framework

    • @prettyprinted
      @prettyprinted  3 роки тому

      I'll look into it. Thanks for watching.

  • @saurabh210284
    @saurabh210284 3 роки тому

    Thank you. Is there a way we can implement LDAP authentication ?

  • @legion_prex3650
    @legion_prex3650 13 днів тому

    great vid! May i ask, what is the tokenURL exactly for?
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    its not written in the docs and people in tutorials always say, just put it there. I believe, nobody knows, what it means. Its just for the open api scheme and swagger ui, i think.

  • @johnmcway6120
    @johnmcway6120 Рік тому +1

    wow, god bless django for abstracting away this boilerplate.

  • @promisesheggs8863
    @promisesheggs8863 3 роки тому

    is there a way to integrate google authentication with fast API?

  • @ralfvenchbillones3312
    @ralfvenchbillones3312 3 роки тому +2

    Waiting for fastapi playlist.

  • @davutlu60
    @davutlu60 3 роки тому

    Thank you for the tutorial. I was wondering if there is a particular method to exclude "hash_parword" in returning user data in pydantic user model..

  • @romankocherezhchenko34
    @romankocherezhchenko34 2 роки тому

    Do JWTs support RBAC?

  • @bhaveshmevada8424
    @bhaveshmevada8424 3 роки тому

    Sir can i make facebook messenger chatbot using fastapi?

  • @MD-vm7tv
    @MD-vm7tv 3 роки тому

    Hi, Anthony,
    I'm a freelancer! Experienced with web scraping but I want to switch to web development. I don't have a college degree, I have a little knowledge about Django, Flask, Celery, and React.
    Should I go for Backend with Python or Frontend with React or something else? Can you consider to recommend a path for me?

    • @cepuofficial9025
      @cepuofficial9025 3 роки тому

      I will recommend frontend since the job for that path would be more abundant

  • @harryodendaal345
    @harryodendaal345 3 роки тому

    at 1:48 how do you get your json data in the web browser to be styled like that?

    • @zachblake6457
      @zachblake6457 3 роки тому +1

      thats how it looks in firefox

    • @harryodendaal345
      @harryodendaal345 3 роки тому

      @@zachblake6457 o snap just tested it and jup usually use chroms but thats really cool

  • @advrxh
    @advrxh 3 роки тому

    09:50

  • @hamodeyDooba
    @hamodeyDooba 3 роки тому +1

    i am watching and I feel like , ye just copy paste copy paste copy paste !!!!
    wtf with content creators

  • @mrjaimees
    @mrjaimees Рік тому

    Hi,
    I can't access to sqlite3 db.sqlite3, appear this message sqlite3: The term 'sqlite3' is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    somebody can help me?

  • @nigelfranciscoharosisa1579
    @nigelfranciscoharosisa1579 2 роки тому

    It is possible to authenticate from get, for instance hhtp://user/me?user&password?