From 9ba892f8d37ae652977829b45441b2ebdad00d04 Mon Sep 17 00:00:00 2001 From: xtrinch Date: Sun, 12 Jun 2016 23:57:34 +0200 Subject: [PATCH] Extend AuthTokenSerializer to allow login with custom username fields --- rest_framework/authtoken/serializers.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index df0c48b86..7f27f1c3d 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -2,18 +2,23 @@ from django.contrib.auth import authenticate from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers +from rest_framework.fields import empty +from django.contrib.auth import get_user_model class AuthTokenSerializer(serializers.Serializer): - username = serializers.CharField(label=_("Username")) password = serializers.CharField(label=_("Password"), style={'input_type': 'password'}) + def __init__(self, data=empty, **kwargs): + self.fields[get_user_model().USERNAME_FIELD] = serializers.CharField(label=_(get_user_model().USERNAME_FIELD)) + super().__init__(self, data, **kwargs) + def validate(self, attrs): - username = attrs.get('username') + username = attrs.get('%s' % get_user_model().USERNAME_FIELD) password = attrs.get('password') if username and password: - user = authenticate(username=username, password=password) + user = authenticate(**{'%s' % get_user_model().USERNAME_FIELD: username, 'password': password}) if user: if not user.is_active: @@ -23,7 +28,7 @@ class AuthTokenSerializer(serializers.Serializer): msg = _('Unable to log in with provided credentials.') raise serializers.ValidationError(msg) else: - msg = _('Must include "username" and "password".') + msg = _('Must include "%s" and "password".' % get_user_model().USERNAME_FIELD) raise serializers.ValidationError(msg) attrs['user'] = user