Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AUTHENTICATION_BACKENDS = (
'social_auth.backends.google.GoogleOAuth2Backend',
'django.contrib.auth.backends.ModelBackend',
)
LOGIN_REDIRECT_URL = '/'
GOOGLE_OAUTH2_CLIENT_ID = os.environ['GOOGLE_OAUTH2_CLIENT_ID']
GOOGLE_OAUTH2_CLIENT_SECRET = os.environ['GOOGLE_OAUTH2_CLIENT_SECRET']
GOOGLE_WHITE_LISTED_DOMAINS = ['incuna.com']
SOCIAL_AUTH_USER_MODEL = 'auth.User'
from django.conf.urls import *
from django.contrib import admin
from .views import AuthComplete, LoginError
admin.autodiscover()
urlpatterns = patterns('',
# some other urls
url(r'^admin/', include(admin.site.urls)),
url(r'^complete/(?P<backend>[^/]+)/$', AuthComplete.as_view()),
url(r'^login-error/$', LoginError.as_view()),
url(r'', include('social_auth.urls')),
)
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic.base import View
from social_auth.backends.exceptions import AuthFailed
from social_auth.views import complete
class AuthComplete(View):
def get(self, request, *args, **kwargs):
backend = kwargs.pop('backend')
try:
return complete(request, backend, *args, **kwargs)
except AuthFailed:
messages.error(request, "Your Google Apps domain isn't authorized for this app")
return HttpResponseRedirect(reverse('login'))
class LoginError(View):
def get(self, request, *args, **kwargs):
return HttpResponse(status=401)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment