anexia-it / django-rest-passwordreset

An extension of django rest framework, providing a configurable password reset strategy
BSD 3-Clause "New" or "Revised" License
419 stars 148 forks source link

User matching query does not exist. #100

Open ksvoboda opened 4 years ago

ksvoboda commented 4 years ago

Hi, So, I am trying to get this working, but one thing is not working for me. When I reset password, I get response "OK", but when I try to login I get error: "User matching query does not exist." I really can't figure out why I get this error. I think I did everything right by the guide, only difference is that I don't send email, but I only print it in console. It looks like it make the user unaccesible, I still see the user in django admin. Let me know if/what code you need.

Has anyone any idea? Thanks

jamehii commented 4 years ago

That's because your User model now has a new related field "password_reset_tokens" added.

I believe all your pre-existing users in your database were created before you using this package, those users don't have the related field included. Hence, when you are querying for the users from database, no user is matching to your current new User model with the related field.

To solve this, probably deleting all your pre-existing users in database and start fresh would be the easier way ONLY if you don't wanna keep those users anymore.

ksvoboda commented 4 years ago

Thanks for your reply, but I actually deleted the whole database and migrated again before using it. I did it multiple times, but it didn’t help.

Any other idea please? Thanks

jamehii commented 4 years ago

@ksvoboda

There are 2 ways:

1st way Unregister your User model in the admin, then register again

If that doesn't work, then 2nd way:

Did you explicitly create ResetPasswordToken and link it to each of your "User" model instance ? If you didn't, that probably would cause login issue.

You can do it with "post_save" signal:

from django.db.models.signals import post_save
from django.dispatch import receiver
from django_rest_passwordreset.models import ResetPasswordToken

@reciever(post_save, sender=settings.AUTH_USER_MODEL)
on_user_post_save(sender, instance, created, **kwargs):
    if created:
        # Only do this once on each creation of user instance
        ResetPasswordToken.objects.create(user=instance)

Again, you might need to clean up your database and migrate again. Then you can try to login see if it works.

ksvoboda commented 4 years ago

So, if I understand it right, I just deleted User model in admin.py and then put it back. That didn't help. And the second way I just pasted your code in signals.py (I think you forgot def before on_user_post_save), still getting same error. Also VSCode is warning me that Class 'ResetPasswordToken' has no 'objects' member

But now, out of nowhere I get different error, now in console when trying to run the app.

 File "/Users/krystofsvoboda/Documents/GitHub/project-tracker-api/myvenv/lib/python3.7/site-packages/django/core/urlresolvers.py", line 320
    except Resolver404, e:
                      ^
SyntaxError: invalid syntax

I wasn't able to fing any working solution on Google to this problem.

Thanks

jamehii commented 4 years ago

To unregister meaning in your admin.py, you will will have something below:

unregister first

admin.site.unregister(YourUserModel)

then register again

admin.site.register(YourUserModel)

anx-cbenke commented 4 years ago

@ksvoboda have you been able to resolve this issue with @jamehii's help?