Skip to content

Instantly share code, notes, and snippets.

@dannyroa
Created May 10, 2012 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyroa/2655940 to your computer and use it in GitHub Desktop.
Save dannyroa/2655940 to your computer and use it in GitHub Desktop.
facebookconnect middleware
from django.conf import settings
from django.contrib.auth import authenticate, login as auth_login
from django.core.urlresolvers import reverse
from facebookconnect.models import FacebookProfile
from facebookconnect.utils import facebook_login_check
class LazyFacebookLoginCheck(object):
def __get__(self, request, obj_type=None):
if not hasattr(request, '_facebook_login_check'):
facebook_check = facebook_login_check(request.COOKIES)
if facebook_check and not request.user.is_authenticated() and request.path != reverse('auth_logout'):
user = authenticate(request=request)
if user:
auth_login(request, user)
request._facebook_login_check = facebook_check
return request._facebook_check
class LazyFacebookProfile(object):
def __get__(self, request, obj_type=None):
if not hasattr(request, '_facebook_profile'):
try:
facebook_profile = FacebookProfile.objects.get(user=request.user)
request._facebook_profile = facebook_profile
except FacebookProfile.DoesNotExist:
request._facebook_profile = None
return request._facebook_profile
class FacebookConnectMiddleware(object):
def process_request(self, request):
request.__class__.facebook_login_check = LazyFacebookLoginCheck()
request.__class__.facebook_profile = LazyFacebookProfile()
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment