Commit hash: 1372562
Clone the jarvis-monorepo
repository from our course GitHub.
You’ll find a FastAPI application under the auth/
folder that implements the basic OAuth2 flow. Let’s get this running locally!
Installation
To get started, we’ll need to install uv
, a package manager for Python. You can install it and get familiar with the basic usage on the documentation.
Running Locally
Once uv
is installed, we can now run our auth service locally. Change directory to the auth/
folder and run
uv sync
What this command does is determine what packages are needed (defined in auth/pyproject.toml
), compares them to what we currently have installed, and “syncs” them, e.g. determines if the versions we want match the constraints in the pyproject.toml
.
Now that our packages are synced, we can run the application. If you’ve used Python before, uv
makes things a bit different. Instead of python ...
, we run uv run ...
. This is because uv
also manages the Python versions on our machine. The uv run
command looks at the .python-version
file to determine which Python binary to use. If you’ve used Python before, this is a huge benefit. You no longer have to constantly switch virtual environments as you work on different projects.
To run our backend, ensure you are in the auth/
directory and execute:
uv run fastapi dev main.py
You should see logs along the lines of
...
server Server started at http://127.0.0.1:8000
server Documentation at http://127.0.0.1:8000/docs
tip Running in development mode, for production use: fastapi run
Logs:
INFO Will watch for changes in these directories: ['/Users/davidcao/work/teaching/jarvis-monorepo/auth']
INFO Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO Started reloader process [40027] using WatchFiles
INFO Started server process [40029]
INFO Waiting for application startup.
INFO Application startup complete.
Testing the service
While we have no frontend, we actually still test various endpoints on our service. FastAPI provides a nice UI for testing all your endpoints using Swagger. This page is by default located at /docs
(you can see the link in the output above, too). Navigate to this page.
Test the following flows:
- Signup a new user.
- Try to hit the profile endpoint.
- Authorize using the “authorize” button with the lock icon on the top right.
- Try hitting the profile endpoint again.
- Play around!
You’ve successfully created a user, issued an OAuth2 JWT token, and hit an authenticated endpoint with that token! A lot was abstracted away through the niceness FastAPI provides, but effectively when we authorized the user, the issued token was stored locally for us and automatically included on any request we made after. If you logout using the same button, you’ll see that your requests are no longer authorized because