Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-allauth, how can I only allow signup/login through social?

I only want to allow people to sign up or log in with their social account. I have the social sign up and log in working, but I cant figure out how to disable the local sign up.

I've read the docs and this sounds close to what I want

ACCOUNT_FORMS (={})
    Used to override forms, for example: {‘login’: ‘myapp.forms.LoginForm’}

It seems like I can make a new sign up form and only include the social log in link, but I was hoping there is any easier way that I'm overlooking. I'm still new to this all so I tend to miss the obvious a lot still.

I also tried changing the code below to False, but that disabled social sign up as well.

allauth.account.adapter.py

def is_open_for_signup(self, request):
    """
    Checks whether or not the site is open for signups.

    Next to simply returning True/False you can also intervene the
    regular flow by raising an ImmediateHttpResponse
    """
    return True
like image 865
Brian Head Avatar asked Nov 26 '14 19:11

Brian Head


People also ask

How does Django Allauth work?

django-allauth is an integrated set of Django applications dealing with account authentication, registration, management, and third-party (social) account authentication. It is one of the most popular authentication modules due to its ability to handle both local and social logins.


1 Answers

Change templates and urlpatterns

You would have to change both the templates (login, signup, etc.) and urlpatterns provided by allauth by default, which relate to the classic signup/login flow using email.

  • Changing/reducing the available routes via the urlpatterns ensures that only the routes are available that should be there. HTTP error 404 is then shown for any attempt to hack into existing allauth default functionality (related to email) if you do it right.
  • Changing the templates can ensure that the user interface does not provide what is related to email-based authentication.

No easy option available

Unfortunately, as of today there is no easy switch or setting to simply disable email-based signup and authentication with django-allauth. More details may be on GitHub in future, see:

  • Issue #1227 ("Social only: disable all local account handling by means of a simple setting")
  • Issue #345 ("How to disable form login/signup?")

Sample: urls.py

An urls.py like this will work with the current django-allauth (v0.30.0) on Django 1.10:

from django.conf.urls import include, url

from allauth.account.views import confirm_email, login, logout
from allauth.compat import importlib
from allauth.socialaccount import providers

providers_urlpatterns = []

for provider in providers.registry.get_list():
    prov_mod = importlib.import_module(provider.get_package() + '.urls')
    providers_urlpatterns += getattr(prov_mod, 'urlpatterns', [])

urlpatterns = [
    url(r'^auth/', include(providers_urlpatterns)),
    url(r'^confirm-email/(?P<key>[-:\w]+)/$', confirm_email, name='account_confirm_email'),
    url(r'^login/$', login, name='account_login'),
    url(r'^logout/$', logout, name='account_logout'),
    url(r'^signup/$', login, name='account_signup'),  # disable email signup
]
like image 54
Peterino Avatar answered Sep 21 '22 10:09

Peterino