2

I use Django with Django Rest Framework as the backend for my site. Registration is disabled, and login with password is disabled. The only way a user can register and login is with Django Social Auth, that exchanges (in this case Discord) a social token for a Django token, and in that process the user is created if they don't exist for that email.

So in Django, the user exists, with a username and email, but they don't have a password.

How can these users login to the admin panel?

1 Answer 1

2

Registration is disabled, and login with password is disabled.

Registration is not the only way of creating users. You can create superuser using createsuperuser shell command. Then you can use it to login into admin site.

Now, the question is if you want to allow all users to admin site. IMHO, you should not. Still, if you want to then you can add a custom auth backend, like this:

class CustomBackend(BaseBackend): def authenticate(self, request, username=None, token=None): try: user = User.objects.get(username=username) except User.DoesNotExist: return None check_token = Token.objects.filter(user=user, token=token).exists() if check_token: return user return None def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None 

And add the new backend in settings.py:

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend', 'path.to.CustomBackend'] 
Sign up to request clarification or add additional context in comments.

3 Comments

The is_staff and is_superuser for users is populated by their roles in the discord server, so it's ok that they have access to the admin panel in this instance. This works, as long as I set the token object filter to token=password, allowing them to input their token into the password field.
password parameter unused, token variable undefined, .. Accepted answer?
@mirek you are right. I have updated the answer. hope it helps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.