| 1 |
Advanced Usage |
| 2 |
============== |
| 3 |
|
| 4 |
|
| 5 |
Creating and Populating User instances |
| 6 |
-------------------------------------- |
| 7 |
|
| 8 |
The following adapter methods can be used to intervene in how User |
| 9 |
instances are created and populated with data |
| 10 |
|
| 11 |
- ``allauth.socialaccount.adapter.DefaultSocialAccountAdapter``: |
| 12 |
|
| 13 |
- ``is_open_for_signup(self, request, socialaccount)``: The default function |
| 14 |
returns that is the same as ``ACCOUNT_ADAPTER`` in ``settings.py``. |
| 15 |
You can override this method by returning ``True``/``False`` |
| 16 |
if you want to enable/disable socialaccount signup. |
| 17 |
|
| 18 |
- ``new_user(self, request, sociallogin)``: Instantiates a new, empty |
| 19 |
``User``. |
| 20 |
|
| 21 |
- ``save_user(self, request, sociallogin, form=None)``: Populates and |
| 22 |
saves the ``User`` instance (and related social login data). The |
| 23 |
signup form is not available in case of auto signup. |
| 24 |
|
| 25 |
- ``populate_user(self, request, sociallogin, data)``: Hook that can |
| 26 |
be used to further populate the user instance |
| 27 |
(``sociallogin.account.user``). Here, ``data`` is a dictionary of |
| 28 |
common user properties (``first_name``, ``last_name``, ``email``, |
| 29 |
``username``, ``name``) that the provider already extracted for you. |
| 30 |
|
| 31 |
|
| 32 |
Custom Redirects |
| 33 |
---------------- |
| 34 |
|
| 35 |
If redirecting to statically configurable URLs (as specified in your |
| 36 |
project settings) is not flexible enough, then you can override the |
| 37 |
following adapter methods: |
| 38 |
|
| 39 |
- ``allauth.socialaccount.adapter.DefaultSocialAccountAdapter``: |
| 40 |
|
| 41 |
- ``get_connect_redirect_url(self, request, socialaccount)`` |
| 42 |
|
| 43 |
|
| 44 |
Customizing providers |
| 45 |
--------------------- |
| 46 |
|
| 47 |
When an existing provider doesn't quite meet your needs, you might find yourself |
| 48 |
needing to customize a provider. |
| 49 |
|
| 50 |
This can be achieved by subclassing an existing provider and making your changes |
| 51 |
there. Providers are defined as django applications, so typically customizing one |
| 52 |
will mean creating a django application in your project. This application will contain your customized |
| 53 |
urls.py, views.py and provider.py files. The behaviour that can be customized is beyond |
| 54 |
the scope of this documentation. |
| 55 |
|
| 56 |
.. warning:: |
| 57 |
|
| 58 |
In your ``provider.py`` file, you will need to expose the provider class |
| 59 |
by having a module level attribute called ``provider_classes`` with your custom |
| 60 |
classes in a list. This allows your custom provider to be registered properly |
| 61 |
on the basis of the ``INSTALLED_APPS`` setting. |
| 62 |
|
| 63 |
Be sure to use a custom id property on your provider class such that its default |
| 64 |
URLs do not clash with the provider you are subclassing. |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
|
| 68 |
class GoogleNoDefaultScopeProvider(GoogleProvider): |
| 69 |
id = 'google_no_scope' |
| 70 |
|
| 71 |
def get_default_scope(self): |
| 72 |
return [] |
| 73 |
|
| 74 |
provider_classes = [GoogleNoDefaultScopeProvider] |
| 75 |
|
| 76 |
|
| 77 |
Changing provider scopes |
| 78 |
------------------------ |
| 79 |
|
| 80 |
Some projects may need more scopes than the default required for authentication purposes. |
| 81 |
|
| 82 |
Scopes can be modified via ``SOCIALACCOUNT_PROVIDERS`` in your project settings.py file. |
| 83 |
|
| 84 |
.. code-block:: python |
| 85 |
|
| 86 |
SOCIALACCOUNT_PROVIDERS = { |
| 87 |
'<ProviderNameHere>': { |
| 88 |
'SCOPE': [...] |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
You need to obtain the default scopes that allauth uses by |
| 93 |
looking in ``allauth/socialaccount/providers/<ProviderNameHere>/provider.py`` |
| 94 |
and look for ``def get_default_scope(self):`` method. Copy those default scopes |
| 95 |
into the SCOPE list shown above. |
| 96 |
|
| 97 |
Example of adding calendar.readonly scope to Google scopes:: |
| 98 |
|
| 99 |
SOCIALACCOUNT_PROVIDERS = { |
| 100 |
'google': { |
| 101 |
'SCOPE': [ |
| 102 |
'profile', |
| 103 |
'email', |
| 104 |
'openid', |
| 105 |
'https://www.googleapis.com/auth/calendar.readonly' |
| 106 |
], |
| 107 |
} |
| 108 |
} |