I'm trying to implement password reset by sending an email to the user with a link which will redirect him/her to a new password form.
I took by example this question and this site.
But my problem is a bit different. I don't have a local database containing the users, so I cannot perform operations over their attributes. I receive user data via an API (user id, user email, user password).
So, which is the best way to generate a unique link to send via email to user so that this link would tell me who the user is and allow me to reset his/her password? And also, how could I redirect it in urls.py? I wish that this link could be used only a single time.
My views.py is like this:
def password_reset_form(request):
if request.method == 'GET':
form = PasswordResetForm()
else:
form = PasswordResetForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
content_dict = {
'email': email,
'domain': temp_data.DOMAIN,
'site_name': temp_data.SITE_NAME,
'protocol': temp_data.PROTOCOL,
}
subject = content_dict.get('site_name')+ ' - Password Reset'
content = render_to_string('portal/password_reset_email.html', content_dict)
send_mail(subject, content, temp_data.FIBRE_CONTACT_EMAIL, [email])
return render(request, 'portal/password_reset_done.html', {'form': form,})
return render(request, 'portal/password_reset_form.html', {'form': form,})
And the template the e-mail I'm sending is:
{% autoescape off %}
You're receiving this e-mail because we got a request to reset the password for your user account at {{ site_name }}.
Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}/[***some unique link***]
{% endblock %}
If you didn't request a password reset, let us know.
Thank you.
The {{ site_name }} team.
{% endautoescape %}
Thanks, guys.