Saleem
Saleem

Reputation: 41

Django Password Reset Email sending incorrect link

I am using the auth_views login and password reset system for the accounts app in a Django project. The password reset functionality works fine on the localhost. However once deployed when trying to reset the password the email that is sent to the user's registered email account contains the incorrect reset URL. The domain part of the reset URL contains the localhost address and not the domain of the site.

The email is sent to a link like http://127.0.0.1:8000/accounts/reset/MTk/azifmz-484db716de96c7628427b41e587c1910/[![enter image description here]1]1

What I am expecting is for the sent email to contain the correct return URL specific to the domain that is sending it. e.g https://www.example.com/accounts/reset/MTk/azifmz-484db716de96c7628427b41e587c1910.

In settings.py

    LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = 'index'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
}
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST= 'smtp.gmail.com'
EMAIL_HOST_USER= '[email protected]'
EMAIL_HOST_PASSWORD= 'mypassword'
EMAIL_USE_TLS= True
EMAIL_PORT= 587

In my accounts urls.py I am using the default classed bassed views provided by django.contrib.auth. There are no custom views for the reset workflow. I am hoping to configure this workflow to avoid having to write custom views for now. urls.py

path('password_reset/', auth_views.PasswordResetView.as_view(
        template_name="accounts/password_reset_form.html", success_url="done/"), name="password_reset"),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(
        template_name = "accounts/password_reset_done.html"), name="password_reset_done"),

path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
        template_name = "accounts/password_reset_confirm.html"), name="password_reset_confirm"),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(
        template_name = "accounts/password_reset_complete.html"), name="password_reset_complete"),

Finally the html template is also pretty standard :

  <div class="jumbotron">
    <form method="POST">
      {% csrf_token %}
      {% bootstrap_form form %}
      <input type="submit" class='btn btn-primary' value="Reset Password">
    </form>
  </div>

Upvotes: 1

Views: 944

Answers (2)

Saleem
Saleem

Reputation: 41

Solved - Basically, there were three sites registered in my admin: pk1 = 127.0.0.0:8000, pk2 = example.com, and pk3= mydomain.com. Just make sure the first one, or better still the only one, in production is the domain you want.

Upvotes: 2

Leeuwtje
Leeuwtje

Reputation: 345

How are you deploying your site. If you use Ngnix as a reverse proxy, your request is now from your own server (127.0.0.1) and not your domain name. You can add the next line to your Nginx-config file:

proxy_set_header Host yourdomain.com;

This will send your domain in the request

Upvotes: 1

Related Questions