I have modified the default User and use this method to register a new User.
我修改了默认用户并使用此方法注册新用户。
class RegisterView(views.APIView):
def post(self, request, **kwargs):
location = Location.objects.get(id=kwargs.get('location_id'))
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
user.location = location
user.save()
subject = "Please Activate Your FootTheBall Account!"
token = self._generate()
link = HOST + PREFIX + str(user.id) + SUFFIX + token
message = 'Please use the following link to activate your account.\n\n{}'.format(link)
from_email = settings.EMAIL_HOST_USER
to_list = [user.email, 'soumasish@foottheball.com']
send_mail(subject, message, from_email, to_list, fail_silently=True)
Token.objects.create(user=user, token=token)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
This works fine and the User is registered in the database. I have verified that.
这工作正常,用户在数据库中注册。我已经证实了这一点。
Now I call this method to login the User.
现在我调用此方法来登录用户。
class LoginView(views.APIView):
def post(self, request):
user = authenticate(
email=request.data.get("email"),
password=request.data.get("password")
)
if not user:
return Response({
'status': 'Unauthorized',
'message': 'Email or password incorrect',
}, status=status.HTTP_401_UNAUTHORIZED)
login(request, user)
return Response(UserSerializer(user).data)
Despite providing the correct email and password it can't find the user and authenticate method fails. What am I doing wrong here?
尽管提供了正确的电子邮件和密码,但无法找到用户并且验证方法失败。我在这做错了什么?
1 个解决方案
#1
0
If you are using the default User model from Django auth, then users should be logged in using username
and password
, not email
and password
.
如果您使用Django auth的默认用户模型,则用户应使用用户名和密码登录,而不是使用电子邮件和密码登录。
Your serializer (which I'm assuming is a ModelSerializer over the User model) saves, so the username
field is being sent over, but you are not authenticating with it.
您的序列化程序(我假设是基于用户模型的ModelSerializer)保存,因此用户名字段正在发送,但您没有使用它进行身份验证。
There are ways for you to login with email:
您可以通过电子邮件登录:
- Creating a custom User model, which is somewhat cumbersome.
- Writing an authentication backend, which is really simple if you follow the docs.
创建自定义用户模型,这有点麻烦。
编写身份验证后端,如果您按照文档操作,这非常简单。
EDIT: I just read the first line, when you said you customized the User model. If that so, make sure you defined the USERNAME_FIELD
attribute of your class to be email
编辑:当你说你自定义用户模型时,我只读了第一行。如果是这样,请确保您将类的USERNAME_FIELD属性定义为电子邮件
#1
0
If you are using the default User model from Django auth, then users should be logged in using username
and password
, not email
and password
.
如果您使用Django auth的默认用户模型,则用户应使用用户名和密码登录,而不是使用电子邮件和密码登录。
Your serializer (which I'm assuming is a ModelSerializer over the User model) saves, so the username
field is being sent over, but you are not authenticating with it.
您的序列化程序(我假设是基于用户模型的ModelSerializer)保存,因此用户名字段正在发送,但您没有使用它进行身份验证。
There are ways for you to login with email:
您可以通过电子邮件登录:
- Creating a custom User model, which is somewhat cumbersome.
- Writing an authentication backend, which is really simple if you follow the docs.
创建自定义用户模型,这有点麻烦。
编写身份验证后端,如果您按照文档操作,这非常简单。
EDIT: I just read the first line, when you said you customized the User model. If that so, make sure you defined the USERNAME_FIELD
attribute of your class to be email
编辑:当你说你自定义用户模型时,我只读了第一行。如果是这样,请确保您将类的USERNAME_FIELD属性定义为电子邮件