aboutsummaryrefslogtreecommitdiffstats
path: root/backend/authentication
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-02-19 09:01:38 +0300
committerGravatar Hassan Abouelela <[email protected]>2021-02-19 09:09:24 +0300
commit7a16a6b129f754a5486c441f2602a8d593edb85f (patch)
tree4153446b809706370824682ca0925e0ad2378ba4 /backend/authentication
parentAdds Production Constant (diff)
Adds Token Refresh Route
Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'backend/authentication')
-rw-r--r--backend/authentication/user.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/backend/authentication/user.py b/backend/authentication/user.py
index f40c68c..a1d78e5 100644
--- a/backend/authentication/user.py
+++ b/backend/authentication/user.py
@@ -1,7 +1,11 @@
import typing as t
+import jwt
from starlette.authentication import BaseUser
+from backend.constants import SECRET_KEY
+from backend.discord import fetch_user_details
+
class User(BaseUser):
"""Starlette BaseUser implementation for JWT authentication."""
@@ -23,3 +27,16 @@ class User(BaseUser):
@property
def discord_mention(self) -> str:
return f"<@{self.payload['id']}>"
+
+ @property
+ def decoded_token(self) -> dict[str, any]:
+ return jwt.decode(self.token, SECRET_KEY, algorithms=["HS256"])
+
+ async def refresh_data(self) -> None:
+ """Fetches user data from discord, and updates the instance."""
+ self.payload = await fetch_user_details(self.decoded_token.get("token"))
+
+ updated_info = self.decoded_token
+ updated_info["user_details"] = self.payload
+
+ self.token = jwt.encode(updated_info, SECRET_KEY, algorithm="HS256")