Log in to Django using your Google account instead of your own. I didn't understand it even after reading the ReadMe of the module to be used, so make a note.
The version is the one I used.
It is assumed that python manage.py runserver
is ready to run.
Also, make sure that you have already created a google app and obtained an ID and secret key.
First, install python-social-auth
pip install python-social-auth
Then write the settings to settings.py
and urls.py.
Add the following to project / setting.py
INSTALLED_APPS = (
...
'social.apps.django_app.default',
...
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'social.backends.twitter.TwitterOAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your secret id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'your secret key'
project_name/urls.py
urlpatterns = patterns('',
...
url('', include('social.apps.django_app.urls', namespace='social')),
url('', include('django.contrib.auth.urls', namespace='auth')),
...
)
Next, we will edit the template. This time, we will replace the login / user registration on the management screen with Google authentication.
First, create a login screen for the management screen and override it with the existing one.
Create templates / admin / login.html
and select this file Please copy and paste. Rewrite the content block in the copied file. (Originally, it seems to override login.html not entirely, but partially, but I don't know, so I'm overriding the whole ...)
{% block content %}
<center>
{% if user and not user.is_anonymous %}
<h1><a href="{% url 'auth:logout' %}?next={{ request.path }}">Logout</a></h1>
{% else %}
<h1><a href="{% url 'social:begin' 'google-oauth2' %}?next={% url 'admin:index' %}">Login with Google</a></h1>
{% endif %}
</center>
{% endblock %}
That's all for the procedure. Let's actually run it and check it. Below is a list of points that are easy to stumble.
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
That's it. Since I wrote it after creating it, there may be omissions. I would appreciate it if you could let me know if you notice anything.
Add the following to settings.py and it's OK
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_EMAILS = ['email address']
Add the following if you want to validate with the domain of the email address instead of the email address
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['domain']
It's hard to social login to admin and see nothing, so I wanted to enable is_staff and is_superuser from the beginning, so I'll add my own function to pipeline. This time, we will rewrite is_staff and is_superuser of the created user.
project_name/custome_pipeline.py
def set_superuser(user, *args, **kwargs):
user.is_staff = True
user.is_superuser = True
user.save()
After that, add the following to settings.py. The point is to insert the created process after create_user
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'project_name.custom_pipeline.set_superuser', #Path to the prepared function
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
)
Recommended Posts