Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] JWTAuth() is inconsistent with django authentication? #1290

Open
neldivad opened this issue Sep 2, 2024 · 1 comment
Open

[BUG] JWTAuth() is inconsistent with django authentication? #1290

neldivad opened this issue Sep 2, 2024 · 1 comment

Comments

@neldivad
Copy link

neldivad commented Sep 2, 2024

@api.get(
    path="/hello-user", 
    response=UserSchema, 
    auth=[JWTAuth()]
)
def hello_user(request):
    return request.user

>>> 
"GET - hello_user /api/hello-user" 
Unauthorized: /api/hello-user

When disabling auth

@api.get(
    path="/hello-user", 
    response=UserSchema, 
    # auth=[JWTAuth()]
)
def hello_user(request):
    return request.user

>>>
"GET - hello_user /api/hello-user" 
[02/Sep/2024 16:50:14] "GET /api/hello-user HTTP/1.1" 200 113
{"username": "neldivad", "is_authenticated": true, "email": "[email protected]"}
# ??? Django says I'm authenticated by Ninja disagrees ???

This decorator is so frustrating to use. Different apps gets authenticated and sometimes it doesn't.

I tried logging out and logging in from admin page. Tried different browser, Tried incognito. This JWT auth is the one that has been giving me a huge issue.

@Xdynix
Copy link

Xdynix commented Sep 21, 2024

Django Ninja's auth will store the authenticated entity in request.auth. request.user is still what authenticated by your Django settings, e.g. user of current session with django.contrib.sessions.middleware.SessionMiddleware.

Example from doc:

from ninja import NinjaAPI
from ninja.security import django_auth

api = NinjaAPI(csrf=True)


@api.get("/pets", auth=django_auth)
def pets(request):
    return f"Authenticated user {request.auth}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants