Added storage for auth keys #11

Merged
xeovalyte merged 1 commits from feature/surrealdb-session into main 2024-04-02 19:51:03 +02:00
2 changed files with 53 additions and 5 deletions
Showing only changes of commit 6aadc89530 - Show all commits

View File

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

View File

@ -1,6 +1,12 @@
use crate::util;
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_json::json;
use std::rc::Rc;
@ -76,6 +82,8 @@ pub struct SurrealContext {
pub ready_state: Signal<ConnectionReadyState>,
pub authenticated: ReadSignal<bool>,
pub set_authenticated: WriteSignal<bool>,
pub loading: ReadSignal<bool>,
pub set_loading: WriteSignal<bool>,
}
#[derive(Serialize, Deserialize, Clone)]
@ -107,6 +115,8 @@ impl SurrealContext {
ready_state: Signal<ConnectionReadyState>,
authenticated: ReadSignal<bool>,
set_authenticated: WriteSignal<bool>,
loading: ReadSignal<bool>,
set_loading: WriteSignal<bool>,
) -> Self {
Self {
message,
@ -114,6 +124,8 @@ impl SurrealContext {
ready_state,
authenticated,
set_authenticated,
loading,
set_loading,
}
}
@ -185,6 +197,7 @@ pub fn init_surrealdb() {
// Create reactive signals for the websocket connection to surrealDB
let (participants, set_participants) = create_signal::<Vec<Participant>>(vec![]);
let (authenticated, set_authenticated) = create_signal::<bool>(false);
let (loading, set_loading) = create_signal::<bool>(true);
// Bind the websocket connection to a context
let websocket = SurrealContext::new(
@ -193,14 +206,39 @@ pub fn init_surrealdb() {
ready_state,
authenticated,
set_authenticated,
loading,
set_loading,
);
provide_context(websocket);
provide_context(websocket.clone());
provide_context(ParticipantsContext {
read: 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
create_effect(move |prev_value| {
let msg = message.get();
@ -245,15 +283,23 @@ fn surrealdb_response(response: String) {
}
/// Function to execute when DB signin is succesful
fn use_surrealdb(_response: SurrealResponse) {
fn use_surrealdb(response: SurrealResponse) {
util::toast::add_toast(
"Succesfully signed into DB".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>();
websocket.set_authenticated.set(true);
websocket.set_loading.set(false);
let request = SurrealRequest {
id: SurrealId::Integer(1),