Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/core/shield/src/shield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ impl<U: User> Shield<U> {
}
}

provider_forms.sort_by(|a, b| a.id.cmp(&b.id));

method_forms.push(ActionMethodForm {
id: method_id.clone(),
provider_forms,
});
}

method_forms.sort_by(|a, b| a.id.cmp(&b.id));

Ok(ActionForms {
id: action_id.to_owned(),
name: action_name.unwrap_or(action_id.to_owned()),
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/shield-dioxus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ server = ["dioxus/server"]
anyhow.workspace = true
dioxus = { workspace = true, features = ["fullstack", "router"] }
serde_json.workspace = true
serde_qs = "1.0.0"
shield.workspace = true
tracing.workspace = true
2 changes: 2 additions & 0 deletions packages/integrations/shield-dioxus/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod integration;
mod query;
mod router;
mod routes;
mod style;

pub use integration::*;
pub use query::*;
pub use router::*;
pub use routes::call;
pub use style::*;
18 changes: 18 additions & 0 deletions packages/integrations/shield-dioxus/src/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::{collections::HashMap, ops::Deref};

#[derive(Clone, Debug)]
pub struct Query(HashMap<String, String>);

impl Query {
pub fn parse(query: &str) -> Self {
Self(serde_qs::from_str::<HashMap<String, String>>(query).unwrap_or_default())
}
}

impl Deref for Query {
type Target = HashMap<String, String>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
8 changes: 4 additions & 4 deletions packages/integrations/shield-dioxus/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::routes::Action;

#[derive(Clone, Debug, PartialEq, Routable)]
pub enum ShieldRouter {
#[route("", Action)]
ActionIndex,
#[route("/:action_id")]
Action { action_id: String },
#[route("?:..query", Action)]
ActionIndex { query: String },
#[route("/:action_id?:..query")]
Action { action_id: String, query: String },
}
5 changes: 4 additions & 1 deletion packages/integrations/shield-dioxus/src/routes/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use dioxus::prelude::*;
use serde_json::Value;
use shield::{ActionForms, ResponseType};

use crate::ErasedDioxusStyle;
use crate::{query::Query, style::ErasedDioxusStyle};

#[derive(Clone, PartialEq, Props)]
pub struct ActionProps {
#[props(default = "index".to_owned())]
action_id: String,
query: String,
}

#[component]
Expand All @@ -22,6 +23,8 @@ pub fn Action(props: ActionProps) -> Element {
let response_read = response.read();
let response = response_read.as_ref().unwrap();

use_context_provider(|| Query::parse(&props.query));

match response {
Ok(forms) => style.render(forms),
Err(err) => rsx! { "{err}" },
Expand Down
9 changes: 2 additions & 7 deletions packages/methods/shield-email/src/actions/sign_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,19 @@ impl<U: User + 'static> Action<EmailProvider, ()> for EmailSignInAction<U> {
.map_err(|err| ShieldError::Validation(err.to_string()))?;

let token = Alphanumeric.sample_string(&mut rand::rng(), 32);
let expires_at = Utc::now() + self.options.expires_in;

let email_auth_token = self
.storage
.create_email_auth_token(CreateEmailAuthToken {
email: data.email.to_lowercase(),
token: hash_token(&token, &self.options.secret),
expired_at: expires_at.into(),
expired_at: (Utc::now() + self.options.expires_in).into(),
})
.await?;

self.options
.sender
.send(
&email_auth_token.email,
&email_auth_token.token,
email_auth_token.expired_at,
)
.send(&email_auth_token.email, &token, email_auth_token.expired_at)
.await?;

Ok(Response::new(ResponseType::Default).session_action(SessionAction::unauthenticate()))
Expand Down
21 changes: 14 additions & 7 deletions packages/methods/shield-email/src/actions/sign_in_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use chrono::Utc;
use serde::Deserialize;
use shield::{
Action, ActionMethod, CreateEmailAddress, CreateUser, Form, Input, InputType, InputTypeEmail,
InputTypeSubmit, InputValue, MethodSession, Request, Response, ResponseType, SessionAction,
ShieldError, SignInCallbackAction, User, erased_action,
InputTypeSubmit, InputTypeText, InputValue, MethodSession, Request, Response, ResponseType,
SessionAction, ShieldError, SignInCallbackAction, User, erased_action,
};

use crate::{
Expand Down Expand Up @@ -51,7 +51,7 @@ impl<U: User + 'static> Action<EmailProvider, ()> for EmailSignInCallbackAction<
}

fn method(&self) -> ActionMethod {
ActionMethod::Get
ActionMethod::Post
}

fn condition(
Expand All @@ -74,19 +74,23 @@ impl<U: User + 'static> Action<EmailProvider, ()> for EmailSignInCallbackAction<
required: Some(true),
..Default::default()
}),
value: None,
value: Some(InputValue::Query {
key: "email".to_owned(),
}),
addon_start: None,
addon_end: None,
},
Input {
name: "token".to_owned(),
label: Some("Token".to_owned()),
r#type: InputType::Email(InputTypeEmail {
r#type: InputType::Text(InputTypeText {
placeholder: Some("Token".to_owned()),
required: Some(true),
..Default::default()
}),
value: None,
value: Some(InputValue::Query {
key: "token".to_owned(),
}),
addon_start: None,
addon_end: None,
},
Expand Down Expand Up @@ -147,7 +151,10 @@ impl<U: User + 'static> Action<EmailProvider, ()> for EmailSignInCallbackAction<
}
};

Ok(Response::new(ResponseType::Default).session_action(SessionAction::authenticate(user)))
Ok(Response::new(ResponseType::Redirect(
self.options.sign_in_redirect.clone(),
))
.session_action(SessionAction::authenticate(user)))
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/methods/shield-email/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use secrecy::SecretString;
use crate::sender::Sender;

#[derive(Builder, Clone)]
#[builder(state_mod(vis = "pub(crate)"))]
#[builder(on(String, into), state_mod(vis = "pub(crate)"))]
pub struct EmailOptions {
#[builder(into)]
pub(crate) secret: SecretString,
Expand All @@ -17,4 +17,7 @@ pub struct EmailOptions {

#[builder(default = TimeDelta::minutes(10))]
pub(crate) expires_in: TimeDelta,

#[builder(default = "/")]
pub(crate) sign_in_redirect: String,
}
2 changes: 1 addition & 1 deletion packages/storage/shield-memory/src/methods/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl EmailStorage<User> for MemoryStorage {
.find(|email_auth_token| {
email_auth_token.email == email
&& email_auth_token.token == token
&& email_auth_token.expired_at < Utc::now()
&& email_auth_token.expired_at > Utc::now()
})
.cloned())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/shield-sea-orm/src/methods/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl EmailStorage<User> for SeaOrmStorage {
email_auth_token::Entity::find()
.filter(email_auth_token::Column::Email.eq(email))
.filter(email_auth_token::Column::Token.eq(token))
.filter(email_auth_token::Column::ExpiredAt.lte(Utc::now()))
.filter(email_auth_token::Column::ExpiredAt.gt(Utc::now()))
.one(&self.database)
.await
.map_err(|err| StorageError::Engine(err.to_string()))
Expand Down
28 changes: 17 additions & 11 deletions packages/styles/shield-bootstrap/src/dioxus/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,33 @@ pub fn Form(props: FormProps) -> Element {
).expect("TODO: handle error");

let result = call(action_id, method_id, provider_id, data).await;
info!("{:?}", result);

// TODO: Handle error.
if let Ok(response) = result {
match response {
ResponseType::Default => {},
ResponseType::Redirect(to) => {
navigator.push(to);
},
ResponseType::RedirectToAction { action_id } => {
navigator.push(ShieldRouter::Action { action_id });
match result {
Ok(response) => {
info!("{:?}", response);

match response {
ResponseType::Default => {},
ResponseType::Redirect(to) => {
navigator.push(to);
},
ResponseType::RedirectToAction { action_id } => {
navigator.push(ShieldRouter::Action { action_id, query: "".to_owned() });
}
}
}
Err(err) => {
// TODO: Handle error.
error!("{err}");
}
}
}
}
},

for input in props.form.inputs {
FormInput {
input: input
input: input,
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/styles/shield-bootstrap/src/dioxus/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dioxus::prelude::*;
use shield::{Input, InputValue};
use shield_dioxus::Query;

#[derive(Clone, PartialEq, Props)]
pub struct FormInputProps {
Expand All @@ -8,6 +9,8 @@ pub struct FormInputProps {

#[component]
pub fn FormInput(props: FormInputProps) -> Element {
let query = use_context::<Query>();

rsx! {
div {
class: "mb-3",
Expand All @@ -28,7 +31,9 @@ pub fn FormInput(props: FormInputProps) -> Element {
type: props.input.r#type.as_str(),
value: props.input.value.map(|value| match value {
InputValue::Origin => "TODO: origin".to_owned(),
InputValue::Query {key} => format!("TODO: query param {key}"),
InputValue::Query { key } => {
query.get(&key).cloned().unwrap_or_default()
},
InputValue::String { value } => value.clone(),
}),
placeholder: props.input.label,
Expand Down
Loading