Skip to content

Commit

Permalink
Introduce signout everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 27, 2024
1 parent 06c04ba commit 2aff1c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions engine/src/auth/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ impl SessionState {

Ok(sessions)
}

// Set every session to invalid
pub async fn delete_by_user_id(user_id: i32, database: &Database) -> Result<Vec<Self>, sqlx::Error> {
let sessions = sqlx::query_as::<_, SessionState>(
"UPDATE sessions SET valid = FALSE WHERE user_id = $1",
)
.bind(user_id)
.fetch_all(&database.pool)
.await?;

Ok(sessions)
}
}
19 changes: 19 additions & 0 deletions engine/src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,22 @@ pub async fn get_sessions(state: Data<&Arc<AppState>>, cookies: &CookieJar) -> i

Json(sessions)
}

#[handler]
pub async fn delete_sessions(
cookies: &CookieJar,
state: Data<&Arc<AppState>>,
) -> impl IntoResponse {
let token = cookies.get("property.v3x.token").unwrap();
let token = Uuid::parse_str(token.value_str()).unwrap();

let session = SessionState::get_by_id(token, &state.database)
.await
.unwrap();

let sessions = SessionState::delete_by_user_id(session.user_id, &state.database)
.await
.unwrap();

Json(sessions)
}
2 changes: 1 addition & 1 deletion engine/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn serve(state: AppState) -> Result<(), poem::Error> {
let app = Route::new()
.at("/login", get(auth::login))
.at("/me", get(auth::me))
.at("/sessions", get(auth::get_sessions))
.at("/sessions", get(auth::get_sessions).delete(auth::delete_sessions))
.at("/callback", get(auth::callback))
.nest("/api", api_service)
.nest("/openapi.json", spec)
Expand Down

0 comments on commit 2aff1c3

Please sign in to comment.