Skip to content

Commit

Permalink
Merge pull request #153 from virto-network/feat/prevent-not-signer
Browse files Browse the repository at this point in the history
Prevent access to pages that require a singer
  • Loading branch information
ail3ngrimaldi authored Oct 28, 2024
2 parents 38a3bd4 + ad2b482 commit 42de5ff
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod pages {
pub mod middlewares {
pub mod is_chain_available;
pub mod is_dao_owner;
pub mod is_signer_ready;
}
pub mod layouts {
pub mod authenticated;
Expand All @@ -34,8 +35,8 @@ pub mod hooks {
pub mod use_notification;
pub mod use_onboard;
pub mod use_our_navigator;
pub mod use_session;
pub mod use_paginator;
pub mod use_session;
pub mod use_spaces_client;
pub mod use_startup;
pub mod use_theme;
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@
"title": "⚠️Attention: This information is for you",
"middleware": {
"has_dao": "We're sorry! 😞 You cannot create more organizations associated with this account.",
"chain_unavailable": "At this time, it is not possible to complete this action."
"chain_unavailable": "At this time, it is not possible to complete this action.",
"signer_not_found": "We're sorry! 😞 You cannot perform this action, you must authenticate first."
}
},
"utils": {
Expand Down
3 changes: 2 additions & 1 deletion src/locales/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@
"title": "⚠️Atención: Está información es para ti",
"middleware": {
"has_dao": "¡Lo sentimos! 😞 No puedes crear más organizaciones asociadas a esta cuenta.",
"chain_unavailable": "En este momento no es posible completar está acción."
"chain_unavailable": "En este momento no es posible completar está acción.",
"signer_not_found": "¡Lo sentimos! 😞 No puedes realizar esta acción, primero debes autenticarte."
}
},
"errors": {
Expand Down
17 changes: 17 additions & 0 deletions src/middlewares/is_signer_ready.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::hooks::{use_accounts::UseAccountsState, use_notification::UseNotificationState};
use dioxus_std::{i18n::UseI18, translate};
pub fn is_signer_ready(
i18: UseI18,
accounts: UseAccountsState,
mut notification: UseNotificationState,
) -> impl FnOnce() -> Result<(), &'static str> {
move || {
if accounts.get_account().is_none() {
notification.handle_warning(&translate!(i18, "warnings.middleware.signer_not_found"));
Err("Failed to get account to sign")
} else {
log::debug!("Signer is ready");
Ok(())
}
}
}
12 changes: 8 additions & 4 deletions src/pages/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use crate::{
dropdown::{DropdownItem, ElementSize},
AccountButton, BankCardLine, Button, CheckboxCard, Dropdown, Icon, Input, KusamaLogo,
PaymentMethod, PaypalLogo, PolygonLogo, Tab, Title,
},
hooks::{
}, hooks::{
use_accounts::use_accounts,
use_communities::use_communities,
use_deposit::{use_deposit, DepositError, DepositTo},
use_notification::use_notification,
use_our_navigator::use_our_navigator,
use_tooltip::{use_tooltip, TooltipItem},
},
pages::onboarding::convert_to_jsvalue,
}, middlewares::is_signer_ready::is_signer_ready, pages::onboarding::convert_to_jsvalue
};
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -62,6 +60,12 @@ pub fn Deposit() -> Element {

let mut dropdown_value = use_signal::<Option<DropdownItem>>(|| None);

use_coroutine(move |_: UnboundedReceiver<()>| async move {
if let Err(_) = is_signer_ready(i18, accounts, notification)() {
nav.push(vec![], "/login");
};
});

let mut items = vec![];
for account in accounts.get().into_iter() {
let address = account.address();
Expand Down
20 changes: 19 additions & 1 deletion src/pages/initiative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@ use crate::{
molecules::{InitiativeActions, InitiativeInfo},
},
hooks::{
use_initiative::{use_initiative, ActionItem, InitiativeData, InitiativeInfoContent, InitiativeInitContent, TransferItem}, use_notification::use_notification, use_our_navigator::use_our_navigator, use_session::use_session, use_spaces_client::use_spaces_client, use_tooltip::{use_tooltip, TooltipItem}
use_accounts::use_accounts,
use_initiative::{
use_initiative, ActionItem, InitiativeData, InitiativeInfoContent,
InitiativeInitContent, KusamaTreasury, KusamaTreasuryPeriod, TransferItem,
VotingOpenGov,
},
use_notification::use_notification,
use_our_navigator::use_our_navigator,
use_session::use_session,
use_spaces_client::use_spaces_client,
use_tooltip::{use_tooltip, TooltipItem},

},
middlewares::is_signer_ready::is_signer_ready,
pages::onboarding::convert_to_jsvalue,
services::{
kreivo::{community_referenda::referendum_count, timestamp::now},
Expand Down Expand Up @@ -47,6 +59,7 @@ extern "C" {
pub fn Initiative(id: u16) -> Element {
let i18 = use_i18();
let mut initiative = use_initiative();
let accounts = use_accounts();
let session = use_session();
let nav = use_our_navigator();
let mut tooltip = use_tooltip();
Expand All @@ -57,6 +70,11 @@ pub fn Initiative(id: u16) -> Element {
use_before_render(move || {
initiative.default();
});
use_coroutine(move |_: UnboundedReceiver<()>| async move {
if let Err(_) = is_signer_ready(i18, accounts, notification)() {
nav.push(vec![], &format!("/dao/{}/initiatives", id));
};
});
rsx! {
div { class: "page--initiative",
div { class: "progress progress--steps-cube",
Expand Down
7 changes: 7 additions & 0 deletions src/pages/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
use_tooltip::{use_tooltip, TooltipItem},
use_withdraw::use_withdraw,
},
middlewares::is_signer_ready::is_signer_ready,
pages::onboarding::convert_to_jsvalue,
};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -58,6 +59,12 @@ pub fn Withdraw() -> Element {

let mut dropdown_value = use_signal::<Option<DropdownItem>>(|| None);

use_coroutine(move |_: UnboundedReceiver<()>| async move {
if let Err(_) = is_signer_ready(i18, accounts, notification)() {
nav.push(vec![], "/login");
};
});

let mut items = vec![];
for account in accounts.get().into_iter() {
let address = account.address();
Expand Down

0 comments on commit 42de5ff

Please sign in to comment.