0
votes

I'm setting up user authentication in Django with Django rest auth. Most things are working well but I can't get the password reset emails to send, either in the dummy backend or with Mailgun.

I followed this tutorial to set up the basic auth. Everything in the tutorial works great but it doesn't explain how to do password reset.

I have an account with Mailgun and am trying to use the sandbox to send mails.

# api/urls.py
from django.urls import include, path

urlpatterns = [
    path('rest-auth/', include('rest_auth.urls')),
    path('rest-auth/registration/', include('rest_auth.registration.urls')),
    path('users/', include('users.urls')),
]

I'm requesting a password reset for a registered user via the browsable API form: http://127.0.0.1:8000/api/v1/rest-auth/password/reset/

I enter an email address in the form and press 'POST'. The page displays this:

POST /api/v1/rest-auth/password/reset/
HTTP 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "detail": "Password reset e-mail has been sent."
}

However the email is not sent!

The mailgun logs show no activity - no email has been sent.

When I look on the Network tab in the browser, I don't see a post request.

Here's my setup using Mailgun:

EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xxxx'
EMAIL_USE_TLS = True

(I've put my real sandbox and password in the actual file)

First, with this in settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

I expect the email text to be logged to the terminal where the server is running with .'/manage.py runserver. However nothing is logged.

Then, with this in settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

I would expect the emails to be sent by Mailgun.

What obvious dumb thing have I missed?

I think that Django rest auth includes a default email template so I shouldn't have to create that manually should I?

Is there something extra I have to do for the admin page at http://127.0.0.1:8000/api/v1/rest-auth/password/reset/ to do its thing? http://127.0.0.1:8000/api/v1/rest-auth/login/ works fine, I can log in and see my token returned from the server.

Very grateful for any ideas!

1
This looks like you entered an email address that doesn't exist. (Yes, the response still says an email was sent in this case.) Can you confirm that the email address exists in the user model and in the EmailAddress model?yofee
Thanks! But I think the issue was that the POST request wasn't even being sent to the server. I've rebuilt the app several times from scratch following different sets of instructions and this is now working. I think why it didn't work before will remain a mystery...Little Brain

1 Answers

1
votes

I don't have a good answer for this but I gave up on using django-rest-auth for password reset and am using the django-contrib-auth urls and templates instead. This doesn't fully match my React front end but at least it provides the user functionality.