Skip to content

Commit

Permalink
Update auth error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 29, 2024
1 parent 6c5cd33 commit 33c2d2b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
1 change: 0 additions & 1 deletion engine/src/auth/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::Arc;

use poem::{web::Data, Error, FromRequest, Request, RequestBody, Result};
use reqwest::StatusCode;
use uuid::Uuid;

use crate::state::AppState;
Expand Down
50 changes: 34 additions & 16 deletions engine/src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use poem::{
handler,
http::HeaderMap,
web::{Data, Json, Query, RealIp, Redirect},
IntoResponse,
Error, IntoResponse,
};
use reqwest::StatusCode;
use serde::Deserialize;
use std::{collections::HashSet, sync::Arc};
use url::Url;
Expand Down Expand Up @@ -109,27 +110,44 @@ pub async fn callback(

#[handler]
pub async fn me(state: Data<&Arc<AppState>>, token: AuthToken) -> impl IntoResponse {
let user = UserData::get_by_id(token.session.user_id, &state.database)
.await
.unwrap();

Json(user)
match token {
AuthToken::Active(active_user) => {
let user = UserData::get_by_id(active_user.session.user_id, &state.database)
.await
.unwrap();

Json(user).into_response()
}
_ => Error::from_string("Not Authenticated", StatusCode::UNAUTHORIZED).into_response(),
}
}

#[handler]
pub async fn get_sessions(state: Data<&Arc<AppState>>, token: AuthToken) -> impl IntoResponse {
let sessions = SessionState::get_by_user_id(token.session.user_id, &state.database)
.await
.unwrap();

Json(sessions)
match token {
AuthToken::Active(active_user) => {
let sessions =
SessionState::get_by_user_id(active_user.session.user_id, &state.database)
.await
.unwrap();

Json(sessions).into_response()
}
_ => Error::from_string("Not Authenticated", StatusCode::UNAUTHORIZED).into_response(),
}
}

#[handler]
pub async fn delete_sessions(state: Data<&Arc<AppState>>, token: AuthToken) -> impl IntoResponse {
let sessions = SessionState::invalidate_by_user_id(token.session.user_id, &state.database)
.await
.unwrap();

Json(sessions)
match token {
AuthToken::Active(active_user) => {
let sessions =
SessionState::invalidate_by_user_id(active_user.session.user_id, &state.database)
.await
.unwrap();

Json(sessions).into_response()
}
_ => Error::from_string("Not Authenticated", StatusCode::UNAUTHORIZED).into_response(),
}
}

0 comments on commit 33c2d2b

Please sign in to comment.