2. Preserve a safe return path through sign-in
Keep the session status in GlobalState and derive one route decision from it. ProtectedRoute builds only the branch that matches the state supplied by your application: a pending screen while access is unresolved, a redirect for a signed-out visitor, or the account component when the application has marked the user as authenticated.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"src/state.rs","value":"src-state-rs"},{"index":2,"label":"src/translations.rs","value":"src-translations-rs"},{"index":3,"label":"src/screens/mod.rs","value":"src-screens-mod-rs"},{"index":4,"label":"src/screens/public_account_entry.rs","value":"src-screens-public-account-entry-rs"},{"index":5,"label":"src/screens/sign_in_route.rs","value":"src-screens-sign-in-route-rs"},{"index":6,"label":"src/screens/session_loading_route.rs","value":"src-screens-session-loading-route-rs"},{"index":7,"label":"src/screens/account_route.rs","value":"src-screens-account-route-rs"},{"index":8,"label":"src/lib.rs","value":"src-lib-rs"},{"index":9,"label":"preview/lib.rs","value":"preview-lib-rs"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
use crate::state::*;
use crate::screens::*;
use fission::prelude::*;
fission_component! {
pub App<AppState>
view(_app, state, cx) {
let account_access = account_route_decision(state, &cx.view.env().current_route);
} =>
Router::<AppState>::new()
.with_path(state.current_path.clone())
.route_component(
"/",
PublicAccountEntry,
)
.route_component(
"/account",
ProtectedRoute::new(account_access, AccountRoute)
.pending(SessionLoadingRoute),
)
.route_component("/sign-in", SignInRoute)
}
{"id":"fission-tabs-1","index":1,"label":"src/state.rs","value":"src-state-rs"}
use fission::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SessionState {
Loading,
SignedOut,
Authenticated { display_name: String },
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppState {
pub current_path: String,
pub session: SessionState,
}
impl Default for AppState {
fn default() -> Self {
Self {
current_path: "/".to_string(),
session: SessionState::Loading,
}
}
}
impl GlobalState for AppState {}
pub fn account_route_decision(
state: &AppState,
current_route: &fission::core::env::RouteLocation,
) -> RouteDecision {
match &state.session {
SessionState::Loading => RouteDecision::Pending,
SessionState::SignedOut => RouteRedirect::replace("/sign-in")
.return_to(current_route)
.into(),
SessionState::Authenticated { .. } => RouteDecision::Allow,
}
}
pub fn route_changed(
state: &mut AppState,
action: fission::core::action::ShellRouteChanged,
_cx: &mut ReducerContext<AppState>,
) {
state.current_path = action.location.pathname;
}
{"id":"fission-tabs-1","index":2,"label":"src/translations.rs","value":"src-translations-rs"}
use std::collections::HashMap;
use fission::i18n::{Locale, TranslationBundle};
use fission::prelude::*;
pub fn create_env(locale: &str) -> Env {
let mut env = Env::default();
env.locale = Locale::from(locale);
for (bundle_locale, source) in [
("en-US", include_str!("../i18n/en-US.yaml")),
("es-ES", include_str!("../i18n/es-ES.yaml")),
] {
let messages: HashMap<String, String> =
serde_yaml::from_str(source).expect("valid account-route translations");
env.i18n.add_bundle(TranslationBundle {
locale: Locale::from(bundle_locale),
messages,
});
}
env
}
{"id":"fission-tabs-1","index":3,"label":"src/screens/mod.rs","value":"src-screens-mod-rs"}
mod public_account_entry;
pub use public_account_entry::PublicAccountEntry;
mod sign_in_route;
pub use sign_in_route::SignInRoute;
mod session_loading_route;
pub use session_loading_route::SessionLoadingRoute;
mod account_route;
pub use account_route::AccountRoute;
{"id":"fission-tabs-1","index":4,"label":"src/screens/public_account_entry.rs","value":"src-screens-public-account-entry-rs"}
use crate::state::*;
use fission::prelude::*;
fission_component! {
pub PublicAccountEntry<AppState>
view(_entry, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
SemanticsRegion(
identifier: Some("harbour-public-account-entry".to_string()),
label: Some(cx.tr("public.semantics")),
child: Container(Column(gap: tokens.spacing.m, children: [
Text(cx.tr("public.title"))
.size(tokens.typography.heading1_size)
.weight(tokens.typography.font_weight_bold),
Text(cx.tr("public.body"))
.color(tokens.colors.text_secondary),
Link::to(cx.tr("signin.title"), "/sign-in"),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.surface)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.large),
)
}
{"id":"fission-tabs-1","index":5,"label":"src/screens/sign_in_route.rs","value":"src-screens-sign-in-route-rs"}
use crate::state::*;
use fission::prelude::*;
fission_component! {
pub SignInRoute<AppState>
view(_page, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
Container(Column(gap: tokens.spacing.m, children: [
Text(cx.tr("signin.title"))
.size(tokens.typography.heading1_size)
.weight(tokens.typography.font_weight_bold),
Text(cx.tr("signin.body"))
.color(tokens.colors.text_secondary),
Link::to(cx.tr("signin.return"), "/"),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.surface)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.large)
}
{"id":"fission-tabs-1","index":6,"label":"src/screens/session_loading_route.rs","value":"src-screens-session-loading-route-rs"}
use crate::state::*;
use fission::prelude::*;
fission_component! {
pub SessionLoadingRoute<AppState>
view(_page, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
SemanticsRegion(
identifier: Some("account-session-loading".to_string()),
label: Some(cx.tr("account.loading.label")),
child: Column(gap: tokens.spacing.m, children: [
Spinner(motion: Some(SpinnerMotion::Default), label: Some(cx.tr("account.loading.label"))),
Text(cx.tr("account.loading.body")),
]),
)
}
{"id":"fission-tabs-1","index":7,"label":"src/screens/account_route.rs","value":"src-screens-account-route-rs"}
use crate::state::*;
use fission::prelude::*;
fission_component! {
pub AccountRoute<AppState>
view(_page, state, cx) {
let tokens = &cx.view.env().theme.tokens;
let display_name = match &state.session {
SessionState::Authenticated { display_name } => display_name.clone(),
_ => String::new(),
};
} =>
SemanticsRegion(
identifier: Some("harbour-private-account".to_string()),
label: Some(cx.tr("account.private.label")),
child: Container(Column(gap: tokens.spacing.m, children: [
Text(cx.tr("account.private.title"))
.size(tokens.typography.heading1_size)
.weight(tokens.typography.font_weight_bold),
Text(format!("{} {display_name}.", cx.tr("account.welcome"))),
Text(cx.tr("account.private.body")),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.surface)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.large),
)
}
{"id":"fission-tabs-1","index":8,"label":"src/lib.rs","value":"src-lib-rs"}
mod app;
mod screens;
mod translations;
mod state;
use fission::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
pub fn run_desktop() -> anyhow::Result<()> {
DesktopApp::<state::AppState, _>::new(app::App)
.with_env(translations::create_env("en-US"))
.with_state_init(|state| state.session = state::SessionState::SignedOut)
.with_route_handler(state::route_changed)
.run()
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen(start)]
pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
console_error_panic_hook::set_once();
WebApp::<state::AppState, _>::new(app::App)
.with_env(translations::create_env("en-US"))
.with_state_init(|state| state.session = state::SessionState::SignedOut)
.with_route_handler(state::route_changed)
.with_title("Harbour account")
.run()
.map_err(|error| wasm_bindgen::JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":9,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod screens;
mod translations;
mod state;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
fission_component! {
pub ProtectedRoutesPreview<state::AppState>
view(_preview, state, cx) {
let tokens = &cx.view.env().theme.tokens;
} =>
Column(children: [
Container(Column(gap: tokens.spacing.s, children: [
Text(cx.tr("preview.title")),
Text(cx.tr("preview.note")),
Row(gap: tokens.spacing.s, wrap: ir_op::FlexWrap::Wrap, children: [
Button(
child: Text(cx.tr("preview.allow")),
key: "preview-allow",
on_press: reduce {
state.session = state::SessionState::Authenticated {
display_name: "Morgan Lee".to_string(),
};
cx.effects.navigate("/account");
},
).semantics_identifier("protected-routes.preview-authenticated".to_string()),
Button(
child: Text(cx.tr("preview.signed_out")),
key: "preview-signed-out",
on_press: reduce {
state.session = state::SessionState::SignedOut;
cx.effects.navigate("/account");
},
).semantics_identifier("protected-routes.preview-signed-out".to_string()),
]),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.background),
app::App,
])
}
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<state::AppState, _>::new(ProtectedRoutesPreview)
.with_env(translations::create_env("en-US"))
.with_route_handler(state::route_changed)
.with_title("Build protected routes in Rust")
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1"}
Select Preview signed-out session. The browser should leave the protected branch, show the public sign-in route, and preserve /account as a safe return path. The preview buttons are isolated in preview/lib.rs; they only set sample route state and do not implement sign-in.
Show the app previewHide the app preview
Show the app preview
Hide the app preview