Added http errors for user apis
This commit is contained in:
@@ -5,12 +5,16 @@ use reqwest::StatusCode;
|
|||||||
|
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
InternalError(String),
|
InternalError(String),
|
||||||
|
OpenIdError(String),
|
||||||
|
Unauthorised
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for AppError {
|
impl IntoResponse for AppError {
|
||||||
fn into_response(self) -> axum::response::Response {
|
fn into_response(self) -> axum::response::Response {
|
||||||
let (status, message) = match self {
|
let (status, message) = match self {
|
||||||
AppError::InternalError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
AppError::InternalError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg),
|
||||||
|
AppError::OpenIdError(msg) => (StatusCode::UNPROCESSABLE_ENTITY, msg),
|
||||||
|
AppError::Unauthorised => (StatusCode::UNAUTHORIZED, "Unauthorised (did you login?)".into())
|
||||||
};
|
};
|
||||||
(status, message).into_response()
|
(status, message).into_response()
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/users.rs
12
src/users.rs
@@ -156,16 +156,16 @@ pub async fn login(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(mut info): Query<OpenId>,
|
Query(mut info): Query<OpenId>,
|
||||||
session: Session,
|
session: Session,
|
||||||
) -> Result<&'static str, AppError> {
|
) -> Result<(), AppError> {
|
||||||
if !info.is_valid().await? {
|
if !info.is_valid().await? {
|
||||||
return Ok("nok");
|
return Err(AppError::OpenIdError("Some query parameters are erronous or lacking.".into()));
|
||||||
}
|
}
|
||||||
let steamid = info
|
let steamid = info
|
||||||
.identity
|
.identity
|
||||||
.replace("https://steamcommunity.com/openid/id/", "");
|
.replace("https://steamcommunity.com/openid/id/", "");
|
||||||
let user = User::from_steamid(&pool, steamid).await?;
|
let user = User::from_steamid(&pool, steamid).await?;
|
||||||
session.insert("uuid", user.id).await?;
|
session.insert("uuid", user.id).await?;
|
||||||
Ok("ok")
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn logout(session: Session) -> Result<(), AppError> {
|
pub async fn logout(session: Session) -> Result<(), AppError> {
|
||||||
@@ -177,11 +177,11 @@ pub async fn logout(session: Session) -> Result<(), AppError> {
|
|||||||
pub async fn me(
|
pub async fn me(
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
session: Session,
|
session: Session,
|
||||||
) -> Result<Json<Option<User>>, AppError> {
|
) -> Result<Json<User>, AppError> {
|
||||||
if let Some(uuid) = session.get("uuid").await? {
|
if let Some(uuid) = session.get("uuid").await? {
|
||||||
let user = User::fetch_id(&pool, uuid).await?;
|
let user = User::fetch_id(&pool, uuid).await?;
|
||||||
Ok(Json(Some(user)))
|
Ok(Json(user))
|
||||||
} else {
|
} else {
|
||||||
Ok(Json(None))
|
Err(AppError::Unauthorised)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user