dj-rest-auth Reset email link 一直指向后端
dj-rest-auth Reset email link keeps pointing to the backend
我正在将 dj-rest-auth 与 React 结合使用,我正在尝试使用前端 url 而不是后端 url。我用于帐户创建确认电子邮件的解决方案也会影响重置密码电子邮件,但只是部分影响。它不影响端口。它只尝试在有 none 的重置 link 中添加一个密钥并抛出一个错误。
所以我想知道是否有办法将 url 从端口 8000 更改为端口 3000。
这是我试过的:
class AccountAdapter(DefaultAccountAdapter):
def is_open_for_signup(self, request: HttpRequest):
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
def send_mail(self, template_prefix, email, context):
if settings.DEBUG:
context["activate_url"] = (
"http://localhost:3000/accounts/confirm-email/" + context["key"]
)
else:
context["activate_url"] = (
settings.FRONTEND_URL + "/accounts/confirm-email/" + context["key"]
)
return super().send_mail(template_prefix, email, context)
如果我删除关键部分,它不会给我错误,但会保留端口 8000 并破坏我的帐户确认电子邮件。如果我不摆脱它给我的钥匙:
django | "http://localhost:3000/accounts/confirm-email/" + context["key"]
django | KeyError: 'key'
这为我修复了它:
serializers.py
class CustomAllAuthPasswordResetForm(AllAuthPasswordResetForm):
def save(self, request, **kwargs):
current_site = get_current_site(request)
email = self.cleaned_data['email']
token_generator = kwargs.get('token_generator',
default_token_generator)
for user in self.users:
temp_key = token_generator.make_token(user)
# save it to the password reset model
# password_reset = PasswordReset(user=user, temp_key=temp_key)
# password_reset.save()
# send the password reset email
path = reverse(
'password_reset_confirm',
args=[user_pk_to_url_str(user), temp_key],
)
url = build_absolute_uri(None, path) # PASS NONE INSTEAD OF REQUEST
context = {
'current_site': current_site,
'user': user,
'password_reset_url': url,
'request': request,
}
if app_settings.AUTHENTICATION_METHOD != app_settings.AuthenticationMethod.EMAIL:
context['username'] = user_username(user)
get_adapter(request).send_mail('account/email/password_reset_key',
email, context)
return self.cleaned_data['email']
class CustomPasswordResetSerializer(PasswordResetSerializer):
@property
def password_reset_form_class(self):
return CustomAllAuthPasswordResetForm
settings.py
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER':
'seniorpark.users.api.serializers.CustomPasswordResetSerializer',
}
我正在将 dj-rest-auth 与 React 结合使用,我正在尝试使用前端 url 而不是后端 url。我用于帐户创建确认电子邮件的解决方案也会影响重置密码电子邮件,但只是部分影响。它不影响端口。它只尝试在有 none 的重置 link 中添加一个密钥并抛出一个错误。
所以我想知道是否有办法将 url 从端口 8000 更改为端口 3000。 这是我试过的:
class AccountAdapter(DefaultAccountAdapter):
def is_open_for_signup(self, request: HttpRequest):
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
def send_mail(self, template_prefix, email, context):
if settings.DEBUG:
context["activate_url"] = (
"http://localhost:3000/accounts/confirm-email/" + context["key"]
)
else:
context["activate_url"] = (
settings.FRONTEND_URL + "/accounts/confirm-email/" + context["key"]
)
return super().send_mail(template_prefix, email, context)
如果我删除关键部分,它不会给我错误,但会保留端口 8000 并破坏我的帐户确认电子邮件。如果我不摆脱它给我的钥匙:
django | "http://localhost:3000/accounts/confirm-email/" + context["key"]
django | KeyError: 'key'
这为我修复了它:
serializers.py
class CustomAllAuthPasswordResetForm(AllAuthPasswordResetForm):
def save(self, request, **kwargs):
current_site = get_current_site(request)
email = self.cleaned_data['email']
token_generator = kwargs.get('token_generator',
default_token_generator)
for user in self.users:
temp_key = token_generator.make_token(user)
# save it to the password reset model
# password_reset = PasswordReset(user=user, temp_key=temp_key)
# password_reset.save()
# send the password reset email
path = reverse(
'password_reset_confirm',
args=[user_pk_to_url_str(user), temp_key],
)
url = build_absolute_uri(None, path) # PASS NONE INSTEAD OF REQUEST
context = {
'current_site': current_site,
'user': user,
'password_reset_url': url,
'request': request,
}
if app_settings.AUTHENTICATION_METHOD != app_settings.AuthenticationMethod.EMAIL:
context['username'] = user_username(user)
get_adapter(request).send_mail('account/email/password_reset_key',
email, context)
return self.cleaned_data['email']
class CustomPasswordResetSerializer(PasswordResetSerializer):
@property
def password_reset_form_class(self):
return CustomAllAuthPasswordResetForm
settings.py
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER':
'seniorpark.users.api.serializers.CustomPasswordResetSerializer',
}