django-social-auth 101

Django-social-auth is probably the best, at the very least one of the best, Django applications for handling 3rd party authentication systems. It can handle everything from github, twitter, facebook to open-id and browser-id. It is very flexible and easy to setup. This video will show you how to setup it up to work with github and get you started using django-social-auth.

resources

django-social-auth

settings.py

INSTALLED_APPS = (
    ...
    'social_auth',
    ...
)

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.contrib.github.GithubBackend',
    'django.contrib.auth.backends.ModelBackend',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'social_auth.context_processors.social_auth_by_type_backends',
)

SOCIAL_AUTH_ENABLED_BACKENDS = ('github',)
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'

GITHUB_APP_ID = os.environ['GITHUB_APP_ID']
GITHUB_API_SECRET = os.environ['GITHUB_APP_SECRET']

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/private/'
LOGIN_ERROR_URL = '/login-error/'

urls.py

from django.views.generic.simple import redirect_to

urlpatterns = patterns('',
    url(r'', include('social_auth.urls')),
    url(r'^login/$', redirect_to, {'url': '/login/github'}),
    url(r'^private/$', 'home.views.private'),
    ...
)

base.html

<a href="{% url socialauth_begin 'github' %}">Login with GitHub</a>