• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


30 Nov, 2024

Updated at 26 Dec, 2024

How to use django admin panel authenticating user from my frontend?

Hello everyone,

I have developed an application with the backend built in Django and the frontend built in Vue.

Currently, we are retrieving a token on the frontend, which we then send in the headers to authenticate the user.

Now, we would like to apply the same approach to access the Django admin panel.

To achieve this, I have created the following endpoint and view:

URL:
path('admin/login/', CustomAuthView.as_view()),

View:

class CustomAuthView(views.View):

def get(self, request):
    if user_obj := self.user_has_permission(request):
        login(request, user_obj)
        return redirect('/admin/')
    return HttpResponseForbidden("You do not have permission to access this page")

Essentially, this view takes the token from the Authorization header, extracts the user from the token, and checks if the user is a superuser. If they are, it redirects them to the admin panel.

When testing this using Postman, I am able to retrieve the HTML content of the admin panel. However, I would like to access the normal admin panel, would it be possible?

Thanks!

3 posts - 2 participants

Read full topic