Skip to content

Commit

Permalink
Shield Axum: Add subprovider and user routes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman authored Jan 15, 2025
1 parent 4c7682b commit 5d4b3dd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/integrations/shield-axum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct RouteError(ShieldError);

impl IntoResponse for RouteError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.0.to_string()).into_response()
(StatusCode::INTERNAL_SERVER_ERROR, format!("{:?}", self.0)).into_response()
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/shield-axum/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use axum::{
};
use shield::User;

use crate::routes::{sign_in, sign_in_callback, sign_out};
use crate::routes::{sign_in, sign_in_callback, sign_out, subproviders, user};

pub fn auth_router<U: User + Clone + 'static, S: Clone + Send + Sync + 'static>() -> Router<S> {
Router::new()
.route("/subproviders", get(subproviders::<U>))
.route("/sign-in/:provider_id", post(sign_in::<U>))
.route("/sign-in/:provider_id/:subprovider_id", post(sign_in::<U>))
.route("/sign-in/callback/:provider_id", get(sign_in_callback::<U>))
Expand All @@ -16,4 +17,5 @@ pub fn auth_router<U: User + Clone + 'static, S: Clone + Send + Sync + 'static>(
get(sign_in_callback::<U>),
)
.route("/sign-out", post(sign_out::<U>))
.route("/user", get(user::<U>))
}
4 changes: 4 additions & 0 deletions packages/integrations/shield-axum/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
mod sign_in;
mod sign_in_callback;
mod sign_out;
mod subproviders;
mod user;

pub use sign_in::*;
pub use sign_in_callback::*;
pub use sign_out::*;
pub use subproviders::*;
pub use user::*;
10 changes: 10 additions & 0 deletions packages/integrations/shield-axum/src/routes/subproviders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use axum::Json;
use shield::{SubproviderVisualisation, User};

use crate::{error::RouteError, extract::ExtractShield};

pub async fn subproviders<U: User>(
ExtractShield(shield): ExtractShield<U>,
) -> Result<Json<Vec<SubproviderVisualisation>>, RouteError> {
Ok(Json(shield.subprovider_visualisations().await?))
}
26 changes: 26 additions & 0 deletions packages/integrations/shield-axum/src/routes/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
use shield::User;

use crate::extract::ExtractUser;

pub async fn user<U: User>(ExtractUser(user): ExtractUser<U>) -> Response {
// TODO: Send JSON error using some util.
match user {
Some(user) => {
// TODO: Include email addresses.
// let email_addresses = user.email_addresses().await;

Json(json!({
"id": user.id(),
"name": user.name(),
}))
.into_response()
}
None => (StatusCode::UNAUTHORIZED, "Unauthorized").into_response(),
}
}

0 comments on commit 5d4b3dd

Please sign in to comment.