-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
0eeb681
commit a945c10
Showing
8 changed files
with
81 additions
and
12 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
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,15 @@ | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.response import Response | ||
from django.contrib.auth import authenticate | ||
|
||
class CustomAuthToken(ObtainAuthToken): | ||
def post(self, request, *args, **kwargs): | ||
email = request.data.get('email') | ||
password = request.data.get('password') | ||
user = authenticate(request, username=email, password=password) | ||
if user is not None: | ||
token, created = Token.objects.get_or_create(user=user) | ||
return Response({'token': token.key}) | ||
else: | ||
return Response({'error': 'Invalid Credentials'}, status=400) |
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,13 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
from django.contrib.auth import get_user_model | ||
|
||
class EmailBackend(ModelBackend): | ||
def authenticate(self, request, username=None, password=None, **kwargs): | ||
UserModel = get_user_model() | ||
try: | ||
user = UserModel.objects.get(email=username) | ||
except UserModel.DoesNotExist: | ||
return None | ||
if user.check_password(password) and self.user_can_authenticate(user): | ||
return user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from rest_framework import serializers | ||
from usuarios.models import Usuario | ||
from .models import Usuario | ||
|
||
class UsuarioSerializer(serializers.HyperlinkedModelSerializer): | ||
class UsuarioSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Usuario | ||
fields = ['id', 'email', 'password', 'ativo', 'criacao', 'alterado'] | ||
fields = ['id', 'email', 'password', 'criacao', 'alterado'] | ||
extra_kwargs = {'password': {'write_only': True}} | ||
|
||
def create(self, validated_data): | ||
user = Usuario.objects.create_user(**validated_data) | ||
return user | ||
def create(self, validated_data): | ||
user = Usuario.objects.create_user(**validated_data) | ||
return user |
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