Skip to content

Commit c830637

Browse files
authored
Fixed logic for archiving users (#42)
- In the callback function, we check if the email is associated with a archived user, if they are, we don't give them a cookie. Stopping the auth process. We also have an additional check in the /auth/me that removes the cookie if they have a cookie associated with an archived user. This handles the case when the user is already logged in, we archive them, they refresh, they should no longer be authenticated I think. - Updated the frontend so that it indicates when a user's account is archived. I know this doesn't really follow best security standards since you're supposed to say "not found", but I think in this case, it's probably good to tell users that things are archived.
1 parent 07fd925 commit c830637

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

backend/app/routes/auth.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ async def auth_callback(code: str):
6666
# Check to see if the user_info contains the email field
6767
if "email" not in user_info:
6868
return RedirectResponse(url="/?alert=missing_email")
69+
70+
try:
71+
with get_session() as session:
72+
statement = select(User).where(User.email == user_info["email"])
73+
db_user = session.exec(statement).first()
74+
if db_user and db_user.archived == 1:
75+
# User is archived, do not log in
76+
return RedirectResponse(url="/?alert=archived_user")
77+
except Exception as e:
78+
# Optionally handle DB errors
79+
return RedirectResponse(url="/?alert=database_error")
80+
6981
response = RedirectResponse(url="/?alert=login_successful")
7082
# Set the access token in the cookies
7183
response.set_cookie(
@@ -158,9 +170,10 @@ async def auth_me(request: Request, response: Response):
158170
logger.info(
159171
f"User with email '{db_user.email}' is archived. Cannot authenticate them!"
160172
)
173+
response.delete_cookie("access_token")
161174
return {
162175
"status": "failure",
163-
"message": "Failed to fetch user info",
176+
"message": "User is archived. Login prevented!",
164177
"details": str(e),
165178
}
166179

frontend/src/pages/Home.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ const Home = () => {
4949
'No email associated with the login method you chose, please use a different method.',
5050
);
5151
break;
52+
case 'archived_user':
53+
alert = messageTemplate(
54+
'danger',
55+
'Your account has been archived and cannot be used to log in. Please contact support if you believe this is an error.',
56+
);
57+
break;
58+
case 'database_error':
59+
alert = messageTemplate(
60+
'info',
61+
'Failed to login due to the system being down. Please try again later!',
62+
);
63+
break;
5264
case 'invalid_userinfo_response':
5365
alert = messageTemplate(
5466
'danger',

0 commit comments

Comments
 (0)