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

Django Ninja API Endpoint with Dual Authentication #1394

Open
geacomputing opened this issue Jan 22, 2025 · 0 comments
Open

Django Ninja API Endpoint with Dual Authentication #1394

geacomputing opened this issue Jan 22, 2025 · 0 comments

Comments

@geacomputing
Copy link

Hi.

I'm using Ninja to create an API endpoint in my Django project that needs to serve both GUI-based requests from my HTML template and programmatic requests (e.g., from Python). I want to achieve parity by using the same endpoint for both internal Django users and external users.

Here's my current setup:

apiR = NinjaAPI(csrf=True, auth=django_auth )
And my endpoint:

@apiR.post('/blackops/listModelFiles')
@login_required     #<--------!
def fileList(request): 
    baseFolder = settings.STATICFILES_DIRS[0]
    json_file_path = someFille.json
    with open(json_file_path, 'r') as file:
        json_data = json.load(file)

    return json_data

My goal is to have this endpoint behave differently depending on the request source:

When accessed from within Django (after authenticating via Django), no extra credentials should be required (hence the @login_required decorator).
When accessed programmatically (e.g., from Python), the same endpoint should require explicit authentication (e.g., username and password).
How can I achieve this dual authentication behavior with Ninja and Django?

Thanks!

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

1 participant