use fission::core::env::RouteLocation;
fn account_access(state: &AppState, current_route: &RouteLocation) -> RouteDecision {
if state.session.is_some() {
RouteDecision::Allow
} else {
RouteRedirect::replace("/sign-in")
.return_to(current_route)
.into()
}
}
let decision = account_access(view.state(), &view.env().current_route);
Router::<AppState>::new()
.with_path(view.state().current_path.clone())
.route_component(
"/account",
ProtectedRoute::new(decision, AccountRoute)
.pending(SessionLoadingRoute)
.denied(SessionUnavailableRoute),
)
.route_component("/sign-in", SignInRoute)
use fission::prelude::*;
use fission::core::env::RouteLocation;
use fission::store::{StoreErrorKind, StoreKey};
#[derive(Debug)]
enum SessionState {
Loading,
Authenticated(Session),
SignedOut,
Failed,
}
#[derive(Debug)]
struct AppState {
current_path: String,
session: SessionState,
}
impl Default for AppState {
fn default() -> Self {
Self {
current_path: "/".into(),
session: SessionState::Loading,
}
}
}
impl GlobalState for AppState {}
#[fission_reducer(LoadSession)]
fn load_session(state: &mut AppState, ctx: &mut ReducerContext<AppState>) {
state.session = SessionState::Loading;
let loaded = ctx.effects.bind(SessionLoaded, reduce!(session_loaded));
let failed = ctx.effects.bind(SessionLoadFailed, reduce!(session_load_failed));
ctx.effects
.store()
.get(StoreKey::<Session>::new("auth", "session"))
.on_ok(loaded)
.on_err(failed);
}
#[fission_reducer(SessionLoaded)]
fn session_loaded(state: &mut AppState, ctx: &mut ReducerContext<AppState>) {
match ctx.input.store_value::<Session>() {
Some(Ok(session)) => {
state.session = SessionState::Authenticated(session);
}
Some(Err(error)) if error.kind == StoreErrorKind::InvalidRequest => {
state.session = SessionState::SignedOut;
}
Some(Err(error)) => {
state.session = SessionState::Failed;
// Store `error.message` separately when the denied page should
// explain why session restoration failed.
}
None => state.session = SessionState::Failed,
}
}
#[fission_reducer(SessionLoadFailed)]
fn session_load_failed(state: &mut AppState) {
state.session = SessionState::Failed;
}
fn restored_account_access(state: &AppState, location: &RouteLocation) -> RouteDecision {
match &state.session {
SessionState::Loading => RouteDecision::Pending,
SessionState::Authenticated(_) => RouteDecision::Allow,
SessionState::SignedOut => RouteRedirect::replace("/sign-in")
.return_to(location)
.into(),
SessionState::Failed => RouteDecision::Deny,
}
}
fission_component! {
pub App<AppState> {}
view(_app, state, cx) {
let _load_session = cx.bind(LoadSession, reduce!(load_session));
let decision = restored_account_access(state, &cx.view.env().current_route);
} =>
Router::<AppState>::new()
.with_path(state.current_path.clone())
.route_component(
"/account",
ProtectedRoute::new(decision, AccountRoute)
.pending(SessionLoadingRoute)
.denied(SessionUnavailableRoute),
)
.route_component("/sign-in", SignInRoute)
}
WebApp::<AppState, _>::new(App)
.with_startup_action(LoadSession)
.run()
Decision | Built branch | Shell behavior |
|---|---|---|
Pending | pending | Waits for application state to change. |
Allow | allowed | Builds the protected component. |
Deny | denied | Shows denial; SSR responds with 403. |
Redirect | pending | Declares navigation after the build; SSR responds with 302. |