Skip to content

Instantly share code, notes, and snippets.

@zachinglis
Created September 24, 2008 04:25
Show Gist options
  • Save zachinglis/12483 to your computer and use it in GitHub Desktop.
Save zachinglis/12483 to your computer and use it in GitHub Desktop.
## django
from django.contrib.auth.models import User
from django.core.validators import email_re
class BasicBackend:
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
class EmailBackend(BasicBackend):
def authenticate(self, username=None, password=None):
#If username is an email address, then try to pull it up
if email_re.search(username):
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
return None
else:
#We have a non-email address username we should try username
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return None
if user.check_password(password):
return user
## ruby
class User < ActiveRecord::Base
EMAIL_REGEX = /..../
def self.authenticate(username, password)
user = EMAIL_REGEX.match(username) ? User.find_by_email(username) : User.find_by_username(username)
return user if user and user.check_password(password) # return user if user exists and user.check_password.. or return nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment