radiac / nanodjango

Full Django in a single file - views, models, API ,with async support. Automatically convert it to a full project.
544 stars 24 forks source link

Modifying `NinjaAPI` init parameters for global authentication setting #30

Open jasalt opened 1 month ago

jasalt commented 1 month ago

Documentation https://django-ninja.dev/guides/authentication/ suggests setting auth=GlobalAuth() on NinjaAPI initialization for having authentication setting across endpoints without issuing it separately for every time.

Copying the example code here:

from ninja import NinjaAPI, Form
from ninja.security import HttpBearer

class GlobalAuth(HttpBearer):
    def authenticate(self, request, token):
        if token == "supersecret":
            return token

api = NinjaAPI(auth=GlobalAuth())

I couldn't find a way to do this while using the ninja helper which initializes NinjaAPI without parameters at: https://github.com/radiac/nanodjango/blob/612abf5d8915a47585e4447c46b4d4efb7bbd50f/nanodjango/app.py#L300

Not sure about the implications supporting this would have but for now I'll leave the helper for a moment and try import django-ninja directly (as mentioned in nanodjango docs).