Skip to content

Commit

Permalink
delete state in session and cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
KlemenSpruk committed Oct 13, 2023
1 parent 1a15cab commit d352f34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions django_project_base/account/rest/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from random import randrange

import django
import swapper
Expand All @@ -10,7 +11,6 @@
from django.db.models import ForeignKey, Model, QuerySet
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter, OpenApiResponse
from dynamicforms import fields
Expand Down Expand Up @@ -424,11 +424,10 @@ def update_current_profile(self, request: Request, **kwargs) -> Response:
serializer.is_valid(raise_exception=True)
serializer.save()
email_changed = new_email and user.email != new_email
email_changed_cookie = "verify-email"
response = Response(serializer.data)
if email_changed:
code = get_random_string(length=6)
response.set_cookie(email_changed_cookie, user.pk, samesite="Lax")
code = randrange(100001, 999999)
response.set_cookie("verify-email", user.pk, samesite="Lax")
request.session[f"email-changed-{code}-{user.pk}"] = new_email
# TODO: Use system email
# TODO: SEND THIS AS SYSTEM MSG WHEN PR IS MERGED
Expand Down Expand Up @@ -460,11 +459,15 @@ def confirm_new_email(self, request: Request, **kwargs) -> Response:
user = request.user
if not request.data.get("code"):
raise ValidationError(dict(code=[_("Code required")]))
new_email = request.session.get(f"email-changed-{request.data['code']}-{user.pk}")
key = f"email-changed-{request.data['code']}-{user.pk}"
new_email = request.session.get(key)
if email := new_email:
user.email = email
user.save(update_fields=["email"])
return Response()
request.session.pop(key, None)
response = Response()
response.delete_cookie("verify-email")
return response
raise ValidationError(dict(code=[_("Invalid code")]))

@extend_schema(
Expand Down
6 changes: 3 additions & 3 deletions vue/components/user-session/user-profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const showProjectList = computed(() => (props.projectListComponent && userSessio
const changePasswordErrors = reactive({} as { [key: string]: any[] });
async function verifyEmailChanged(userData: UserDataJSON) {
if (userData[PROFILE_TABLE_PRIMARY_KEY_PROPERTY_NAME].toString() === cookies.get(
'verify-email',
).toString() && userData[PROFILE_TABLE_PRIMARY_KEY_PROPERTY_NAME]) {
const verifyMailCookie = cookies.get('verify-email');
if (_.size(verifyMailCookie) && userData[PROFILE_TABLE_PRIMARY_KEY_PROPERTY_NAME] &&
userData[PROFILE_TABLE_PRIMARY_KEY_PROPERTY_NAME].toString() === verifyMailCookie.toString()) {
const enterEmailConfirmationCode = await dfModal.message('Update email', () => [
h(
'h4',
Expand Down

0 comments on commit d352f34

Please sign in to comment.