aboutsummaryrefslogtreecommitdiff
path: root/users/accountFunctions.py
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-04-30 00:34:22 -0400
committerBobby <[email protected]>2023-04-30 00:34:22 -0400
commitb3439b867b81a2d7cfb363b62b203ee2e64c0613 (patch)
tree79f85dd89786d95228e82977d4e22df8ecb4cc7e /users/accountFunctions.py
parent714953207a6c01d88c826206a41423a597a2ca2c (diff)
downloadthatcomputerscientist-b3439b867b81a2d7cfb363b62b203ee2e64c0613.tar.xz
thatcomputerscientist-b3439b867b81a2d7cfb363b62b203ee2e64c0613.zip
Email Verification Update w/ Token Expiration
Diffstat (limited to 'users/accountFunctions.py')
-rw-r--r--users/accountFunctions.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/users/accountFunctions.py b/users/accountFunctions.py
new file mode 100644
index 00000000..be60e29f
--- /dev/null
+++ b/users/accountFunctions.py
@@ -0,0 +1,39 @@
+from users.models import TokenStore, UserProfile
+import uuid
+import secrets
+from django.utils import timezone
+
+def generate_token():
+ uid = uuid.uuid4().hex
+ token = secrets.token_urlsafe(32)
+ print(uid, token)
+ return uid, token
+
+def store_token(token_type, user, email=None):
+ previous_tokens = TokenStore.objects.filter(user=user, token_type=token_type)
+ if previous_tokens.exists():
+ previous_tokens.delete()
+ uid, token = generate_token()
+ token_store = TokenStore.objects.create(
+ user=user,
+ email=email if email is not None else user.email,
+ uid=uid,
+ token=token,
+ token_type=token_type,
+ expires=timezone.now() + timezone.timedelta(minutes=30),
+ )
+ token_store.save()
+ return uid, token
+
+def verify_token(token_type, uid, token):
+ try:
+ token_store = TokenStore.objects.get(token_type=token_type, uid=uid, token=token)
+ if token_store.expires > timezone.now() and not token_store.verified and token_store.token_type == token_type and token_store.uid == uid and token_store.token == token:
+ token_store.verified = True
+ UserProfile.objects.filter(user=token_store.user).update(email_verified=True)
+ token_store.save()
+
+ return token_store
+ except TokenStore.DoesNotExist:
+ return None
+ \ No newline at end of file