Skip to content

Commit c2e9528

Browse files
authored
docs: fix incorrect Depends() usage and update deprecated config fields (#2)
Fix documentation examples that used `Depends()` without arguments, which doesn't work because JWTHarmony.__init__ requires req/res parameters. Update all login endpoints to use `Depends(JWTHarmonyBare)` which properly handles dependency injection. Also update example configuration to use v0.2.0 field names (removed authjwt_ prefix) for consistency with current API. Changes: - Update README.md login examples to use JWTHarmonyBare - Fix all docs/examples/*.py to use correct dependency pattern - Update config fields: authjwt_* → new field names - Fix CONTRIBUTING.md and MIGRATION.md examples
1 parent c8b60fe commit c2e9528

6 files changed

Lines changed: 29 additions & 27 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ When adding new features, include practical examples:
262262
```python
263263
# ✅ Good: Clear example with context
264264
@app.post("/login")
265-
def login(Authorize: JWTHarmony[User] = Depends()):
265+
def login(Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)):
266266
"""Login endpoint that creates JWT tokens."""
267267
user = User(id="123", username="john")
268268
access_token = Authorize.create_access_token(user_claims=user)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pip install fastapi-jwt-harmony[asymmetric]
3838
```python
3939
from fastapi import FastAPI, Depends
4040
from pydantic import BaseModel
41-
from fastapi_jwt_harmony import JWTHarmony, JWTHarmonyDep
41+
from fastapi_jwt_harmony import JWTHarmony, JWTHarmonyDep, JWTHarmonyBare
4242

4343
app = FastAPI()
4444

@@ -68,7 +68,7 @@ JWTHarmony.configure(
6868
# )
6969

7070
@app.post("/login")
71-
def login(Authorize: JWTHarmony[User] = Depends()):
71+
def login(Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)):
7272
# Authenticate user (your logic here)
7373
user = User(id="123", username="john", email="john@example.com")
7474

@@ -133,7 +133,7 @@ JWTHarmony.configure(
133133
)
134134

135135
@app.post("/login")
136-
def login(response: Response, Authorize: JWTHarmony[User] = Depends()):
136+
def login(response: Response, Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)):
137137
user = User(id="123", username="john", email="john@example.com")
138138
access_token = Authorize.create_access_token(user_claims=user)
139139

docs/MIGRATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def protected(auth: JWTHarmony[User] = Depends(JWTHarmonyDep)):
279279

280280
# Simple token creation
281281
@app.post("/login")
282-
def login(auth: JWTHarmony[User] = Depends()):
282+
def login(auth: JWTHarmony[User] = Depends(JWTHarmonyBare)):
283283
user = User(id="123", username="john")
284284
token = auth.create_access_token(user_claims=user)
285285
return {"access_token": token}

docs/examples/basic_usage.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fastapi import Depends, FastAPI, HTTPException
1010
from pydantic import BaseModel
1111

12-
from fastapi_jwt_harmony import JWTHarmony, JWTHarmonyConfig, JWTHarmonyDep, JWTHarmonyRefresh
12+
from fastapi_jwt_harmony import JWTHarmony, JWTHarmonyBare, JWTHarmonyConfig, JWTHarmonyDep, JWTHarmonyRefresh
1313

1414
app = FastAPI(title='JWT Harmony Basic Example')
1515

@@ -37,10 +37,10 @@ class LoginRequest(BaseModel):
3737
JWTHarmony.configure(
3838
User,
3939
{
40-
'authjwt_secret_key': 'your-super-secret-key-change-in-production', # pragma: allowlist secret
41-
'authjwt_token_location': {'headers'}, # Use headers for this example
42-
'authjwt_access_token_expires': 15 * 60, # 15 minutes
43-
'authjwt_refresh_token_expires': 30 * 24 * 60 * 60, # 30 days
40+
'secret_key': 'your-super-secret-key-change-in-production', # pragma: allowlist secret
41+
'token_location': {'headers'}, # Use headers for this example
42+
'access_token_expires': 15 * 60, # 15 minutes
43+
'refresh_token_expires': 30 * 24 * 60 * 60, # 30 days
4444
},
4545
)
4646

@@ -49,10 +49,10 @@ class LoginRequest(BaseModel):
4949
# JWTHarmony.configure(
5050
# User,
5151
# JWTHarmonyConfig(
52-
# authjwt_secret_key="your-super-secret-key-change-in-production", # pragma: allowlist secret
53-
# authjwt_token_location={"headers"}, # Use headers for this example
54-
# authjwt_access_token_expires=15 * 60, # 15 minutes
55-
# authjwt_refresh_token_expires=30 * 24 * 60 * 60, # 30 days
52+
# secret_key="your-super-secret-key-change-in-production", # pragma: allowlist secret
53+
# token_location={"headers"}, # Use headers for this example
54+
# access_token_expires=15 * 60, # 15 minutes
55+
# refresh_token_expires=30 * 24 * 60 * 60, # 30 days
5656
# )
5757
# )
5858

@@ -66,7 +66,7 @@ def authenticate_user(username: str, password: str) -> User | None:
6666

6767

6868
@app.post('/auth/login')
69-
def login(credentials: LoginRequest, Authorize: JWTHarmony[User] = Depends()) -> dict[str, Any]:
69+
def login(credentials: LoginRequest, Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)) -> dict[str, Any]:
7070
"""Login endpoint that returns JWT tokens."""
7171
user = authenticate_user(credentials.username, credentials.password)
7272
if not user:

docs/examples/cookie_auth.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from fastapi_jwt_harmony import (
1515
JWTHarmony,
16+
JWTHarmonyBare,
1617
JWTHarmonyConfig,
1718
JWTHarmonyDep,
1819
JWTHarmonyException,
@@ -41,13 +42,13 @@ class LoginRequest(BaseModel):
4142
JWTHarmony.configure(
4243
User,
4344
JWTHarmonyConfig(
44-
authjwt_secret_key='your-super-secret-key-change-in-production', # pragma: allowlist secret
45-
authjwt_token_location=frozenset({'cookies'}), # Store tokens in cookies
46-
authjwt_cookie_csrf_protect=True, # Enable CSRF protection
47-
authjwt_cookie_secure=False, # Set to True in production with HTTPS
48-
authjwt_cookie_samesite='strict', # CSRF protection
49-
authjwt_access_token_expires=15 * 60, # 15 minutes
50-
authjwt_refresh_token_expires=30 * 24 * 60 * 60, # 30 days
45+
secret_key='your-super-secret-key-change-in-production', # pragma: allowlist secret
46+
token_location=frozenset({'cookies'}), # Store tokens in cookies
47+
cookie_csrf_protect=True, # Enable CSRF protection
48+
cookie_secure=False, # Set to True in production with HTTPS
49+
cookie_samesite='strict', # CSRF protection
50+
access_token_expires=15 * 60, # 15 minutes
51+
refresh_token_expires=30 * 24 * 60 * 60, # 30 days
5152
),
5253
)
5354

@@ -67,7 +68,7 @@ def authenticate_user(username: str, password: str) -> User | None:
6768

6869

6970
@app.post('/auth/login')
70-
def login(credentials: LoginRequest, response: Response, Authorize: JWTHarmony[User] = Depends()) -> dict[str, Any]:
71+
def login(credentials: LoginRequest, response: Response, Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)) -> dict[str, Any]:
7172
"""Login endpoint that sets secure cookies."""
7273
user = authenticate_user(credentials.username, credentials.password)
7374
if not user:

docs/examples/websocket_auth.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from fastapi_jwt_harmony import (
1616
JWTHarmony,
17+
JWTHarmonyBare,
1718
JWTHarmonyConfig,
1819
JWTHarmonyDep,
1920
JWTHarmonyException,
@@ -46,9 +47,9 @@ class LoginRequest(BaseModel):
4647
JWTHarmony.configure(
4748
User,
4849
JWTHarmonyConfig(
49-
authjwt_secret_key='your-super-secret-key', # pragma: allowlist secret
50-
authjwt_token_location=frozenset({'headers', 'cookies'}), # Support both
51-
authjwt_access_token_expires=60 * 60, # 1 hour for WebSocket connections
50+
secret_key='your-super-secret-key', # pragma: allowlist secret
51+
token_location=frozenset({'headers', 'cookies'}), # Support both
52+
access_token_expires=60 * 60, # 1 hour for WebSocket connections
5253
),
5354
)
5455

@@ -105,7 +106,7 @@ def authenticate_user(username: str, password: str) -> User | None:
105106

106107

107108
@app.post('/auth/login')
108-
def login(credentials: LoginRequest, Authorize: JWTHarmony[User] = Depends()) -> dict[str, Any]:
109+
def login(credentials: LoginRequest, Authorize: JWTHarmony[User] = Depends(JWTHarmonyBare)) -> dict[str, Any]:
109110
"""Login endpoint for getting JWT token."""
110111
user = authenticate_user(credentials.username, credentials.password)
111112
if not user:

0 commit comments

Comments
 (0)