Refetch logged in user on login/signup

This commit is contained in:
2024-11-03 17:13:40 -05:00
parent 8ac3a87c58
commit 2b380d7787
3 changed files with 25 additions and 7 deletions

View File

@ -3,9 +3,10 @@ use crate::models::User;
use leptos::leptos_dom::*;
use leptos::*;
use leptos_icons::*;
use crate::app::LoggedInUserResource;
#[component]
pub fn Signup() -> impl IntoView {
pub fn Signup(user: LoggedInUserResource) -> impl IntoView {
let (username, set_username) = create_signal("".to_string());
let (email, set_email) = create_signal("".to_string());
let (password, set_password) = create_signal("".to_string());
@ -19,7 +20,7 @@ pub fn Signup() -> impl IntoView {
let on_submit = move |ev: leptos::ev::SubmitEvent| {
ev.prevent_default();
let new_user = User {
let mut new_user = User {
id: None,
username: username.get(),
email: email.get(),
@ -30,10 +31,17 @@ pub fn Signup() -> impl IntoView {
log!("new user: {:?}", new_user);
spawn_local(async move {
if let Err(err) = signup(new_user).await {
if let Err(err) = signup(new_user.clone()).await {
// Handle the error here, e.g., log it or display to the user
log!("Error signing up: {:?}", err);
// Since we're not sure what the state is, manually refetch the user
user.refetch();
} else {
// Manually set the user to the new user, avoiding a refetch
new_user.password = None;
user.set(Some(new_user));
// Redirect to the login page
log!("Signed up successfully!");
leptos_router::use_navigate()("/", Default::default());