Skip to content

Commit

Permalink
Building a custom authentication backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Sep 23, 2023
1 parent 1f2763b commit a6540ef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
Binary file not shown.
17 changes: 17 additions & 0 deletions Module04/bookmarks/account/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth.models import User

class EmailAuthBackend():
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
return None
except (User.DoesNotExist, User.MultipleObjectsReturned):
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Binary file modified Module04/bookmarks/bookmarks/__pycache__/settings.cpython-311.pyc
Binary file not shown.
8 changes: 7 additions & 1 deletion Module04/bookmarks/bookmarks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,10 @@
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_ROOT = BASE_DIR / 'media'


AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'account.authentication.EmailAuthBackend',
]
Binary file modified Module04/bookmarks/db.sqlite3
Binary file not shown.

0 comments on commit a6540ef

Please sign in to comment.