Email rework and additional notifications
This commit is contained in:
1 parent
a22d8becbb
commit
4fb09e8fd7
7 files changed
+98
-27
No files matched your search
@@ -9,7 +9,7 @@ from flask_jwt_extended import create_access_token
|
||||
from app.extensions import db_connection
|
||||
from app.extensions import jwt_redis_blocklist
|
||||
|
||||
from app.mail_utils import send_mail
|
||||
from app.mail.mail import send_mail
|
||||
|
||||
|
||||
class UserService:
|
||||
@@ -62,7 +62,7 @@ class UserService:
|
||||
print(f"Error: {e}")
|
||||
return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500
|
||||
|
||||
send_mail("Successfully registered!", email, "Congratulations! Your account has been successfully created.\nThis mail also serves as a test that the email address is correct")
|
||||
send_mail("register", email)
|
||||
|
||||
return {"Success": "User created successfully"}, 200
|
||||
|
||||
@@ -81,10 +81,11 @@ class UserService:
|
||||
try:
|
||||
with db_connection.cursor(dictionary=True) as cursor:
|
||||
|
||||
cursor.execute("select id, password from user where username = %s", (username,))
|
||||
cursor.execute("select id, email, password from user where username = %s", (username,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
user_id = result['id']
|
||||
email = result['email']
|
||||
password_hash = result['password']
|
||||
|
||||
if user_id is None:
|
||||
@@ -97,13 +98,15 @@ class UserService:
|
||||
|
||||
token = create_access_token(identity=user_id, expires_delta=expire)
|
||||
|
||||
send_mail("login", email)
|
||||
|
||||
return {"token": token}, 200
|
||||
|
||||
except Error as e:
|
||||
return {"Failed": f"Failed to login. Error: {e}"}, 500
|
||||
|
||||
@staticmethod
|
||||
def logout(jti, exp) -> Tuple[Union[dict, str], int]:
|
||||
def logout(jti, exp, user_id) -> Tuple[Union[dict, str], int]:
|
||||
"""
|
||||
Logs out a user by invalidating the provided JWT.
|
||||
|
||||
@@ -114,7 +117,9 @@ class UserService:
|
||||
:return: Tuple containing a dictionary and an HTTP status code.
|
||||
:rtype: Tuple[Union[dict, str], int]
|
||||
"""
|
||||
|
||||
UserService.__invalidate_token(jti, exp)
|
||||
UserService.__send_email("logout", id=user_id)
|
||||
|
||||
return {"Success": "Successfully logged out"}, 200
|
||||
|
||||
@@ -128,6 +133,8 @@ class UserService:
|
||||
:return: Tuple containing a dictionary and an HTTP status code.
|
||||
:rtype: Tuple[Union[dict, str], int]
|
||||
"""
|
||||
|
||||
UserService.__send_email("delete", id=user_id)
|
||||
|
||||
try:
|
||||
with db_connection.cursor() as cursor:
|
||||
@@ -215,6 +222,38 @@ class UserService:
|
||||
|
||||
return {"Success": "Password successfully updated"}, 200
|
||||
|
||||
@staticmethod
|
||||
def __send_email(message: str, username: str = None, id: str = None, email: str = None):
|
||||
if email is not None:
|
||||
send_mail(message, email)
|
||||
return
|
||||
|
||||
if username is not None:
|
||||
try:
|
||||
with db_connection.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("select email from user where username = %s", (username,))
|
||||
result = cursor.fetchone()
|
||||
email = result['email']
|
||||
send_mail("logout", email)
|
||||
|
||||
except Error as e:
|
||||
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
|
||||
return
|
||||
|
||||
if id is not None:
|
||||
try:
|
||||
with db_connection.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("select email from user where id = %s", (id,))
|
||||
result = cursor.fetchone()
|
||||
email = result['email']
|
||||
send_mail("logout", email)
|
||||
|
||||
except Error as e:
|
||||
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
|
||||
return
|
||||
|
||||
raise ValueError("Invalid input data to send mail")
|
||||
|
||||
@staticmethod
|
||||
def __invalidate_token(jti: str, exp: int):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user