Django Api: How to create a view that does reset Password?

1

I'm developing a mobile application using the Ionic framework. There is also an API in django that communicates with a PostgreSQL Database.

I have already created an authentication system for the application and it works perfectly just like editing a user's data. My question is in the part of changing the password of a User.

I have already searched the internet for ways to solve this problem but I always find the implementation with the package django.contrib.auth. The problem is that in all ways of solving this problem, they use the forms for a web application. In my case, I just wanted the view to be able to make a request where I changed the password.

Thank you.

    
asked by anonymous 15.04.2016 / 12:35

1 answer

1

The method make_random_password BaseUserManager can be used to create new random passwords, specifying the size (default: 10) and the alphabet (default: alphanumeric, uppercase and lowercase, only ignoring some similar characters).

So, all your view needs to do is get an instance of User right, call set_password " of this user with the random password created (the method itself takes care of hasheá it) and then save it. Example:

def resetar_senha(request):
    usuario = User.objects.get(username=request.POST["username"])
    usuario.set_password(User.objects.make_random_password())
    usuario.save()
    return HttpResponseRedirect("url/de/sucesso")
    
15.04.2016 / 17:43