|
7 | 7 | from itsdangerous import URLSafeTimedSerializer |
8 | 8 |
|
9 | 9 | from common.exceptions import AuthError, InvalidError, RequestError |
10 | | -from common.redis import cache |
| 10 | +from common.redis import add_verification, get_verification |
11 | 11 | from database.user import add_user, email_exists, fetch_user, get_user_info, username_exists |
12 | 12 |
|
13 | 13 | hasher = PasswordHasher( |
@@ -64,31 +64,19 @@ def register(email, username, password): |
64 | 64 | "password": hashed |
65 | 65 | } |
66 | 66 |
|
67 | | - # We use a pipeline here to ensure these instructions are atomic |
68 | | - pipeline = cache.pipeline() |
69 | | - |
70 | | - pipeline.hset(f"register:{code}", mapping=data) |
71 | | - pipeline.expire(f"register:{code}", timedelta(hours=1)) |
72 | | - |
73 | | - pipeline.execute() |
| 67 | + add_verification(data, code) |
74 | 68 |
|
75 | 69 | return code |
76 | 70 |
|
77 | 71 | @staticmethod |
78 | | - def register_verify(token): |
79 | | - cache_key = f"register:{token}" |
| 72 | + def register_verify(code): |
| 73 | + result = get_verification(code) |
80 | 74 |
|
81 | | - if not cache.exists(cache_key): |
| 75 | + if result is None: |
82 | 76 | raise AuthError("Token expired or does not correspond to registering user") |
83 | 77 |
|
84 | | - result = cache.hgetall(cache_key) |
85 | | - stringified = {} |
86 | | - |
87 | | - for key, value in result.items(): |
88 | | - stringified[key.decode()] = value.decode() |
89 | | - |
90 | | - id = add_user(stringified["email"], stringified["username"], stringified["password"]) |
91 | | - return User(id, stringified["email"], stringified["username"], stringified["password"]) |
| 78 | + id = add_user(result["email"], result["username"], result["password"]) |
| 79 | + return User(id, result["email"], result["username"], result["password"]) |
92 | 80 |
|
93 | 81 | @staticmethod |
94 | 82 | def login(email, password): |
|
0 commit comments