Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django allauth signup without password

Is it possible to create user accounts with email as username and no other details such as password using allauth? The intention is to make the signup process as easy as possible. Can password be skipped on signup and be updated on email confirmation?. I have tried this scenario in python shell (./manage.py shell) and had successfull outputs.

In [1]: from django.contrib.auth.models import User

In [2]: User.objects.create(username='nopass')
Out[2]: <User: nopass>

In [3]: User.objects.all()
Out[3]: [<User: userone>, <User: nopass>] 
In [4]: usr=User.objects.all()[1]
In [5]: usr.set_password('pwdnotset')
In [6]: usr.save()
In [7]: from django.contrib.auth import authenticate
In [8]: authenticate(username='nopass',password='pwdnotset')
Out[8]: <User: nopass>

I have referred to this link and found that there was no such settings for allauth at that time. However reply was posted at 2013. It would be helpful if a way to create user without password on signup with some allauth configuration is devised. Thanks in advance.

like image 228
cutteeth Avatar asked Feb 23 '16 14:02

cutteeth


2 Answers

I think creating a signup form with an overridden signup() which sets an unusable password to user and saying allauth to use that form for signup process will solve this issue. Create a Signup form by overriding signup() like one given below.

class UserCreationForm(forms.ModelForm):
    username = forms.CharField(label=_("username"))
    # declare other fields also
    ...
    def signup(self, request, user):
        user.username = self.cleaned_data['username']
        # make sure you are saving all needed data for user model.
        user.set_unusable_password()
        user.save()

in settings.py, tell allauth to use that form for signup

ACCOUNT_SIGNUP_FORM_CLASS = 'yourapp.forms.UserCreationForm'

Below given is an ipdb stack trace for above context.

     48         import ipdb;ipdb.set_trace();
---> 49         user.email = self.cleaned_data['email']
     50         user.username = self.cleaned_data['username']

ipdb> user
<User: test_user>

ipdb> user.save()
*** IntegrityError: NOT NULL constraint failed: user.password

ipdb> user.set_unusable_password()
ipdb> user.save()
ipdb> user
<User: test_user>
ipdb> 
like image 89
cutteeth Avatar answered Sep 27 '22 02:09

cutteeth


I don't see a way to do this in allauth as of yet, except by customizing the code for your project.

Contrary to what other answers say, the code in allauth.account.forms makes the SignUpForm inherit from your custom sign-up form:

# in allauth.account.forms.py
class BaseSignupForm(_base_signup_form_class()):

BaseSignupForm is used for both "standard" sign-up and social account sign-up. In "standard" sign-up, it subclasses as SignupForm and adds the password fields:

# in allauth.account.forms.py
class SignupForm(BaseSignupForm):
def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    self.fields['password1'] = PasswordField(label=_("Password"))
    if app_settings.SIGNUP_PASSWORD_ENTER_TWICE:
        self.fields['password2'] = PasswordField(
            label=_("Password (again)"))

So, I tried to hide the field in the view template, but no luck. Maybe using a FormHelper in crispyforms is the way.

like image 35
harrouet Avatar answered Sep 26 '22 02:09

harrouet