Added storage for auth keys

This commit is contained in:
xeovalyte 2024-04-02 19:50:03 +02:00
parent 14bad374b0
commit 6aadc89530
No known key found for this signature in database
2 changed files with 53 additions and 5 deletions

View File

@ -66,9 +66,11 @@ pub fn App() -> impl IntoView {
<Route path="/" view=move || { <Route path="/" view=move || {
view! { view! {
// only show the outlet if the signin was successfull // only show the outlet if the signin was successfull
<Show when=move || !websocket.loading.get() fallback=|| view! { <p>"Connection to database..."</p>}>
<Show when=move || websocket.authenticated.get() fallback=login::Login> <Show when=move || websocket.authenticated.get() fallback=login::Login>
<Outlet/> <Outlet/>
</Show> </Show>
</Show>
} }
}> }>
<Route path="/" view=Home /> <Route path="/" view=Home />

View File

@ -1,6 +1,12 @@
use crate::util; use crate::util;
use leptos::*; use leptos::*;
use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; use leptos_use::{
core::ConnectionReadyState,
storage::use_local_storage,
use_websocket,
utils::{FromToStringCodec, StringCodec},
UseWebsocketReturn,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use std::rc::Rc; use std::rc::Rc;
@ -76,6 +82,8 @@ pub struct SurrealContext {
pub ready_state: Signal<ConnectionReadyState>, pub ready_state: Signal<ConnectionReadyState>,
pub authenticated: ReadSignal<bool>, pub authenticated: ReadSignal<bool>,
pub set_authenticated: WriteSignal<bool>, pub set_authenticated: WriteSignal<bool>,
pub loading: ReadSignal<bool>,
pub set_loading: WriteSignal<bool>,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
@ -107,6 +115,8 @@ impl SurrealContext {
ready_state: Signal<ConnectionReadyState>, ready_state: Signal<ConnectionReadyState>,
authenticated: ReadSignal<bool>, authenticated: ReadSignal<bool>,
set_authenticated: WriteSignal<bool>, set_authenticated: WriteSignal<bool>,
loading: ReadSignal<bool>,
set_loading: WriteSignal<bool>,
) -> Self { ) -> Self {
Self { Self {
message, message,
@ -114,6 +124,8 @@ impl SurrealContext {
ready_state, ready_state,
authenticated, authenticated,
set_authenticated, set_authenticated,
loading,
set_loading,
} }
} }
@ -185,6 +197,7 @@ pub fn init_surrealdb() {
// Create reactive signals for the websocket connection to surrealDB // Create reactive signals for the websocket connection to surrealDB
let (participants, set_participants) = create_signal::<Vec<Participant>>(vec![]); let (participants, set_participants) = create_signal::<Vec<Participant>>(vec![]);
let (authenticated, set_authenticated) = create_signal::<bool>(false); let (authenticated, set_authenticated) = create_signal::<bool>(false);
let (loading, set_loading) = create_signal::<bool>(true);
// Bind the websocket connection to a context // Bind the websocket connection to a context
let websocket = SurrealContext::new( let websocket = SurrealContext::new(
@ -193,14 +206,39 @@ pub fn init_surrealdb() {
ready_state, ready_state,
authenticated, authenticated,
set_authenticated, set_authenticated,
loading,
set_loading,
); );
provide_context(websocket); provide_context(websocket.clone());
provide_context(ParticipantsContext { provide_context(ParticipantsContext {
read: participants, read: participants,
write: set_participants, write: set_participants,
}); });
create_effect(move |prev_value| {
let status = ready_state.get();
if prev_value != Some(status.clone()) && status == ConnectionReadyState::Open {
let (token, _, _) = use_local_storage::<String, FromToStringCodec>("surrealdb-token");
if token.get().is_empty() {
set_loading.set(false);
return status;
}
let request = SurrealRequest {
id: SurrealId::Integer(0),
method: String::from("authenticate"),
params: vec![SurrealParams::String(token.get())],
};
websocket.send(&json!(request).to_string());
};
status
});
// Watch for a message recieved from the surrealDB connection // Watch for a message recieved from the surrealDB connection
create_effect(move |prev_value| { create_effect(move |prev_value| {
let msg = message.get(); let msg = message.get();
@ -245,15 +283,23 @@ fn surrealdb_response(response: String) {
} }
/// Function to execute when DB signin is succesful /// Function to execute when DB signin is succesful
fn use_surrealdb(_response: SurrealResponse) { fn use_surrealdb(response: SurrealResponse) {
util::toast::add_toast( util::toast::add_toast(
"Succesfully signed into DB".to_string(), "Succesfully signed into DB".to_string(),
"success".to_string(), "success".to_string(),
); );
let (_, set_token, _) = use_local_storage::<String, FromToStringCodec>("surrealdb-token");
match response.result {
SurrealResult::String(str) => set_token.set(str),
_ => (),
};
let websocket = expect_context::<SurrealContext>(); let websocket = expect_context::<SurrealContext>();
websocket.set_authenticated.set(true); websocket.set_authenticated.set(true);
websocket.set_loading.set(false);
let request = SurrealRequest { let request = SurrealRequest {
id: SurrealId::Integer(1), id: SurrealId::Integer(1),