Skip to content

Commit ea022c8

Browse files
committed
Allow deleting current session by explicit ID
1 parent ecf4120 commit ea022c8

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

backend/api/routes/v0/sessions/session.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,38 @@ type PathParams = Path<SessionQuery>;
7272
#[debug_handler]
7373
pub(crate) async fn delete(
7474
Path(session_query): PathParams,
75-
token: Option<AuthToken>,
75+
AuthToken(token): AuthToken,
7676
) -> impl Response<DeleteResponse> {
77-
#[expect(
78-
unused_assignments,
79-
reason = "This will fix itself once the TODO is resolved"
80-
)]
81-
let mut response_header = None;
82-
83-
match session_query {
84-
SessionQuery::Current => {
85-
let Some(AuthToken(token)) = token else {
86-
return Err(api::Error::ResourceNotFound);
87-
};
77+
let token_hash = hash_without_salt(&token);
8878

89-
let token_hash = hash_without_salt(&token);
79+
let response_header = match session_query {
80+
// The user requested deletion of a session other than their current one.
81+
SessionQuery::Id(id) if id.as_ref() != token_hash.as_ref() => {
82+
// TODO: Implement signing out specific sessions in the account settings.
83+
return Err(api::Error::AccessDenied);
84+
}
9085

86+
// The user requested deletion of their current session.
87+
_ => {
9188
let sessions_deleted = db::transaction!(async |tx| -> TxResult<_, api::Error> {
9289
Ok(sqlx::query!(
9390
"DELETE FROM sessions
9491
WHERE token_hash = $1",
9592
token_hash.as_ref(),
9693
)
9794
.execute(tx.as_mut())
98-
.await?)
95+
.await?
96+
.rows_affected())
9997
})
100-
.await?
101-
.rows_affected();
98+
.await?;
10299

103100
if sessions_deleted == 0 {
104101
return Err(api::Error::ResourceNotFound);
105102
}
106103

107-
response_header = Some(SessionCookie::expired().to_header());
104+
Some(SessionCookie::expired().to_header())
108105
}
109-
SessionQuery::Id(_) => {
110-
// TODO: Implement signing out specific sessions in the account settings.
111-
return Err(api::Error::AccessDenied);
112-
}
113-
}
106+
};
114107

115108
Ok((
116109
StatusCode::OK,

frontend/components/DefaultHeader.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ async function signOut() {
3636
signOutLoading.value = false;
3737
});
3838
} catch (error) {
39-
if (getApiErrorCode(error) !== "RESOURCE_NOT_FOUND") {
39+
const errorCode = getApiErrorCode(error);
40+
if (!(errorCode === "AUTH_FAILED" || errorCode === "RESOURCE_NOT_FOUND")) {
4041
throw error;
4142
}
4243
}

0 commit comments

Comments
 (0)