-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
90 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework import serializers | ||
from django.contrib.auth import authenticate | ||
|
||
|
||
class EmailAuthTokenSerializer(serializers.Serializer): | ||
email = serializers.CharField(label=_("Email")) | ||
password = serializers.CharField( | ||
label="Password", | ||
style={'input_type': 'password'}, | ||
trim_whitespace=False | ||
) | ||
|
||
def validate(self, attrs): | ||
email = attrs.get('email') | ||
password = attrs.get('password') | ||
|
||
if email and password: | ||
user = authenticate(email=email, password=password) | ||
|
||
if user: | ||
# From Django 1.10 onwards the `authenticate` call simply | ||
# returns `None` for is_active=False users. | ||
# (Assuming the default `ModelBackend` authentication backend.) | ||
if not user.is_active: | ||
msg = 'User account is disabled.' | ||
raise serializers.ValidationError(msg) | ||
else: | ||
msg = 'Unable to log in with provided credentials.' | ||
raise serializers.ValidationError(msg) | ||
else: | ||
msg = 'Must include "email" and "password".' | ||
raise serializers.ValidationError(msg) | ||
|
||
attrs['user'] = user | ||
return attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.contrib.auth import get_user_model # gets the user_model django default or your own custom | ||
from django.contrib.auth.backends import ModelBackend | ||
from django.db.models import Q | ||
|
||
|
||
class CustomAuthenticationBackend(ModelBackend): # requires to define two functions authenticate and get_user | ||
|
||
def authenticate(self, email=None, password=None, **kwargs): | ||
UserModel = get_user_model() | ||
|
||
try: | ||
# below line gives query set,you can change the queryset as per your requirement | ||
user = UserModel.objects.filter(email__iexact=email).distinct() | ||
|
||
except UserModel.DoesNotExist: | ||
return None | ||
|
||
if user.exists(): | ||
user_obj = user.first() | ||
if user_obj.check_password(password): | ||
return user_obj | ||
return None | ||
else: | ||
return None | ||
|
||
def get_user(self, user_id): | ||
UserModel = get_user_model() | ||
try: | ||
return UserModel.objects.get(pk=user_id) | ||
except UserModel.DoesNotExist: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters