Ссылка на вход в Django AccountActivationTokenGenerator работает несколько раз
Я использовал AccountActivationTokenGenerator с механизмом Django SingIn и SignUp.
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.email_verified)
)
account_activation_token = AccountActivationTokenGenerator()
class PasswordResetTokenGenerator:
...
def check_token(self, user, token):
"""
Check that a password reset token is correct for a given user.
"""
if not (user and token):
return False
# Parse the token
try:
ts_b36, _ = token.split("-")
except ValueError:
return False
try:
ts = base36_to_int(ts_b36)
except ValueError:
return False
# Check that the timestamp/uid has not been tampered with
if not constant_time_compare(self._make_token_with_timestamp(user, ts), token):
# RemovedInDjango40Warning: when the deprecation ends, replace
# with:
# return False
if not constant_time_compare(
self._make_token_with_timestamp(user, ts, legacy=True),
token,
):
return False
# Check the timestamp is within limit.
if (self._num_seconds(self._now()) - ts) > settings.PASSWORD_RESET_TIMEOUT:
return False
return True
Я могу успешно сгенерировать ссылку и отправить ее пользователю по электронной почте, а пользователь может использовать ссылку для входа в систему. Однако ссылка работает каждый раз, когда пользователь нажимает на нее.
Я просто хочу, чтобы ссылка была действительна только один раз, и когда она используется, она должна быть недействительна для следующих попыток.
@api_view(['GET'])
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def activate(request, uidb64, token, project_uuid='None', backend='videoo.registration.views.EmailBackend'):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
User = get_user_model()
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.email_verified = True
logging.info("Retrieving and storing user language (in SignUp): {0}".format(request.LANGUAGE_CODE))
user.language = request.LANGUAGE_CODE
user.save()
Как я могу создать и использовать ссылку, чтобы пользователь мог войти в систему только один раз, используя ссылку?