When not logged in redirection to login page
Every page is locked behind a jwt, when it is not supplied neither other pages not api calls will work
This commit is contained in:
58
frontend/src/auth.rs
Normal file
58
frontend/src/auth.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use gloo_net::http::Request;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct AuthState {
|
||||
pub is_authenticated: bool,
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct ProtectedRouteProps {
|
||||
pub children: Children,
|
||||
}
|
||||
|
||||
#[component(ProtectedRoute)]
|
||||
pub fn protected_route(props: &ProtectedRouteProps) -> Html {
|
||||
let is_authenticated = use_state(|| None::<bool>);
|
||||
|
||||
{
|
||||
let is_authenticated = is_authenticated.clone();
|
||||
use_effect_with((), move |_| {
|
||||
let is_authenticated = is_authenticated.clone();
|
||||
spawn_local(async move {
|
||||
match Request::get("/api/users/current")
|
||||
.credentials(web_sys::RequestCredentials::Include)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(resp) => {
|
||||
let status = resp.status();
|
||||
web_sys::console::log_1(&format!("Auth check: status {}", status).into());
|
||||
if status == 200 {
|
||||
is_authenticated.set(Some(true));
|
||||
} else {
|
||||
is_authenticated.set(Some(false));
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Auth check error: {:?}", err).into());
|
||||
is_authenticated.set(Some(false));
|
||||
}
|
||||
}
|
||||
});
|
||||
|| ()
|
||||
});
|
||||
}
|
||||
|
||||
match *is_authenticated {
|
||||
None => html! { <div>{"Loading..."}</div> },
|
||||
Some(true) => props.children.clone().into(),
|
||||
Some(false) => {
|
||||
html! {
|
||||
<Redirect<crate::Route> to={crate::Route::Login} />
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
mod pages;
|
||||
mod auth;
|
||||
use crate::pages::*;
|
||||
use crate::auth::ProtectedRoute;
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
|
||||
@@ -28,15 +30,43 @@ enum Route {
|
||||
|
||||
fn switch(route: Route) -> Html {
|
||||
match route {
|
||||
Route::Home => html! { <basic_pages::Home/>},
|
||||
Route::Home => html! {
|
||||
<ProtectedRoute>
|
||||
<basic_pages::Home/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::NotFound => html! { <basic_pages::NotFound/> },
|
||||
Route::Ticket => html! { <ticket::SubmitTicket/> },
|
||||
Route::TicketById { id } => html! { <ticket::TicketByID id={id}/> },
|
||||
Route::AllTickets => html! { <ticket::AllTickets/> },
|
||||
Route::Register => html! { <user::Register/> },
|
||||
Route::Ticket => html! {
|
||||
<ProtectedRoute>
|
||||
<ticket::SubmitTicket/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::TicketById { id } => html! {
|
||||
<ProtectedRoute>
|
||||
<ticket::TicketByID {id}/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::AllTickets => html! {
|
||||
<ProtectedRoute>
|
||||
<ticket::AllTickets/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::Register => html! {
|
||||
<ProtectedRoute>
|
||||
<user::Register/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::Login => html! { <user::Login/> },
|
||||
Route::AllUsers => html! {<user::AllUsers/>},
|
||||
Route::UserByID { id } => html! { <user::UserByID id={id}/> },
|
||||
Route::AllUsers => html! {
|
||||
<ProtectedRoute>
|
||||
<user::AllUsers/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
Route::UserByID { id } => html! {
|
||||
<ProtectedRoute>
|
||||
<user::UserByID {id}/>
|
||||
</ProtectedRoute>
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user