A fastapi dependency used to decode jwt tokens generated by nextauth,
Project description
fastapi-nextauth-jwt
This project contains a FastAPI dependency that can be used to decrypt and validate JWTs generated by NextAuth. The purpose of this project is to make it easy to use a FastAPI backend in projects that use Next.js and NextAuth in the frontend.
Besides JWT decryption and validation, NextAuth compatible cross-site request forgery (CSRF) protection is also implemented.
Installation
General usage:
pip install fastapi-nextauth-jwt
Development:
pip install "fastapi-nextauth-jwt[test]"
Usage
from typing import Annotated
from fastapi import FastAPI, Depends
from fastapi_nextauth_jwt import NextAuthJWT
app = FastAPI()
JWT = NextAuthJWT(
secret="y0uR_SuP3r_s3cr37_$3cr3t",
)
@app.get("/")
async def return_jwt(jwt: Annotated[dict, Depends(JWT)]):
return jwt
There are a few configuration options available in the NextAuthJWT constructor, but the most important one is secret
,
which should be equivalent to NEXTAUTH_SECRET
on the Next.js side.
It is also possible to enable or disable CSRF protection using csrf_prevention_enabled
.
If this is not set, this will looks at the ENV environment variable. If this is dev
then CSRF protection will be disabled.
You should also set the NEXTAUTH_URL
environment variable, as it is used to determine
whether or not secure cookies are being used. Or you can set the cookie names manually.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fastapi_nextauth_jwt-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3599de656df0a892fc5c542783aa263134582636fd40e7f0e4ca6267fb4a6331 |
|
MD5 | 90fa2c8fd2cd9f49e69e7c637e7fe2cb |
|
BLAKE2b-256 | 1c9d52f41285b6709a0d3c2f4747a99f3e62c8479057706035642b19d1da562e |
Hashes for fastapi_nextauth_jwt-1.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fa25051128daabe18366be07f5ef93a09155fc061b97cfd40d530d05937a644 |
|
MD5 | a099736e55cf71c00a46c781407f20e5 |
|
BLAKE2b-256 | ce2c1ddbe0cff514abd4dee249b217cb26a41a35a903eb91033bdded52a25d9e |