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.
TODO
- Add check to see if token has expired or not
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.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5448c00808737c82c85d62c1b15769ffd8ba33cd429688599e48224000ad0414 |
|
MD5 | 080662279aaade5a3d0414eb2ef98ae9 |
|
BLAKE2b-256 | 78101861c468d1a4290309600091a1c47059fe8f8f75450bae39b1d0401563e5 |
Hashes for fastapi_nextauth_jwt-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6582c66d9dfbc1b85057ae37760671574ceba2280aa88fb3410db57523cb7702 |
|
MD5 | 38fa21996da1b26c9757760f5d4a51e7 |
|
BLAKE2b-256 | b445b7cfc203d4cda038a29a6fe35042c6b0a4d3a6fbb54fd776727301b20c34 |