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

Asked

Viewed 538 times

1

I am developing a mobile application using the Ionic framework. There is also a Django API that communicates with a Postgresql database.

I have already created an authentication system for the application and it works perfectly as well as editing a user’s data. My question is on the part of changing a User’s password.

I’ve looked on the internet for ways to solve this problem but I always find the implementation with package Django.contrib.auth. The problem is that in all ways to solve this problem, they use Forms for a web application. In my case, I just wanted to have the view so I could make a request where I changed the password.

Thank you.

1 answer

1

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

So all your view needs to do is get an instance of User right, call the set_password of that user with the created random password (the method itself hashes it) and then saves 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")
  • Thanks! I’ve already found a package that has some features and helps a lot in authentication anyway. https://github.com/sunscrapers/djoser

Browser other questions tagged

You are not signed in. Login or sign up in order to post.