【问题标题】:Django Rest Auth - UID invalid value errorDjango Rest Auth - UID 无效值错误
【发布时间】:2021-08-29 12:40:09
【问题描述】:

我有一个 DRF 服务器,我一直在尝试使用 PasswordResetForm 发送密码重置电子邮件。

我按预期收到了一封电子邮件,但在尝试发送重置密码时,我收到了:

{
    "uid": [
        "Invalid value"
    ]
}

经过一番调查,我发现在 Django 的 PasswordResetConfirmSerializer 的文件中,有一个导致该问题的条件导入 - 我正在使用 allauth,所以 它导入了错误的 @ 987654326@模块(使用其他模块):

# this imports the wrong uid_decoder
if 'allauth' in settings.INSTALLED_APPS:
    from allauth.account.forms import default_token_generator
    from allauth.account.utils import url_str_to_user_pk as uid_decoder
else:
    from django.contrib.auth.tokens import default_token_generator 
    from django.utils.http import urlsafe_base64_decode as uid_decoder

有没有比编辑 dj_rest_auth 文件更好的方法来处理这个问题?

谢谢!

电子邮件发送代码,如果需要:

from django.conf import settings
from django.contrib.auth.forms import PasswordResetForm


def send_user_invite(email):
    # send invitation to reset password & join the platform
    form_options = {
        "use_https": True,
        "from_email": getattr(settings, "DEFAULT_FROM_EMAIL"),
        # "request": request,
        "subject_template_name": "registration/password_reset_subject.txt",
        "email_template_name": "users/invite_with_password_reset.html",
        "extra_email_context": {"reset_base_url": settings.RESET_BASE_URL},
    }
    form = PasswordResetForm(data={"email": email})
    if form.is_valid():
        form.save(**form_options)

【问题讨论】:

  • 如果是,你能解决这个问题吗
  • a work around 我认为这是 dj-rest-auth 的错误
  • 也许有。将我的解决方法作为评论发布在下面。

标签: python django django-rest-framework django-rest-auth


【解决方案1】:

我的解决方法是为ResetPasswordForm 编写一个自定义子类,如下所示:

# helpers.py
from server.users.forms import SendInviteForm


def send_user_invite(email):
    # send invitation to reset password & join the platform
    form = SendInviteForm(data={"email": email})
    if form.is_valid():
        form.save(None)
from os import path

from allauth.account.forms import (
    EmailAwarePasswordResetTokenGenerator,
    ResetPasswordForm,
)
from allauth.account.utils import user_pk_to_url_str
from django.conf import settings
from django.contrib.auth import forms as admin_forms
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _



class SendInviteForm(ResetPasswordForm):
    """
    used to send an invitation to onboard the platform and reset the password
    """

    default_token_generator = EmailAwarePasswordResetTokenGenerator()

    def send_email_invite(self, email, uri, uid, token):
        context = {
            "uri": uri,
            "uid": uid,
            "token": token,
        }
        msg_plain = render_to_string("users/invite_with_password_reset.txt", context)
        msg_html = render_to_string("users/invite_with_password_reset.html", context)
        send_mail(
            "Welcome!",
            msg_plain,
            None,
            [email],
            html_message=msg_html,
        )

    def save(self, request, **kwargs):
        email = self.cleaned_data["email"]
        token_generator = kwargs.get("token_generator", self.default_token_generator)
        for user in self.users:
            temp_key = token_generator.make_token(user)
            uri = path.join(settings.CLIENT_BASE_URL, "he/welcome/reset-password")
            self.send_email_invite(email, uri, user_pk_to_url_str(user), temp_key)
        return self.cleaned_data["email"]


【讨论】:

  • 这可行,但我认为更简单的方法是降级并使用我尝试过的 2.1.5 版本,它适用于使用我之前分享的要点来重定向和使用正确的辅助解码器
猜你喜欢
  • 1970-01-01
  • 2017-06-14
  • 2018-06-08
  • 2017-04-07
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
相关资源
最近更新 更多