From ca5cf1f9783fd10b2d47af42bdb0beb0219c2c83 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sun, 11 Feb 2024 00:56:11 -0500 Subject: [PATCH] Add auth --- devblog/devblog/client/src/api.rs | 2 +- devblog/devblog/client/src/helpers.rs | 18 +++++++++++++++--- devblog/devblog/client/src/main.rs | 1 + devblog/devblog/client/src/models.rs | 9 ++++++++- devblog/devblog/client/src/pages/home.rs | 4 ++-- devblog/devblog/client/src/pages/insights.rs | 12 ++++++++---- devblog/devblog/client/src/pages/posts.rs | 6 +++--- devblog/devblog/client/src/pages/sign_in.rs | 11 ++++------- devblog/devblog/client/src/pages/sign_up.rs | 7 ++++--- devblog/devblog/client/src/store.rs | 9 --------- 10 files changed, 46 insertions(+), 33 deletions(-) diff --git a/devblog/devblog/client/src/api.rs b/devblog/devblog/client/src/api.rs index 8b1bdb4..3a27d14 100644 --- a/devblog/devblog/client/src/api.rs +++ b/devblog/devblog/client/src/api.rs @@ -17,7 +17,7 @@ pub enum Api { } impl Api { - pub async fn fetch2( + pub async fn fetch( &self, hdrs: Option, body: Option, diff --git a/devblog/devblog/client/src/helpers.rs b/devblog/devblog/client/src/helpers.rs index 15c5447..927c281 100644 --- a/devblog/devblog/client/src/helpers.rs +++ b/devblog/devblog/client/src/helpers.rs @@ -1,6 +1,7 @@ +// use gloo::console::log; +use crate::store::Store; use crate::{helpers, Api}; use crate::{router::Route, User, UserField}; -// use gloo::console::log; use gloo_net::http::{Headers, Method, Response}; use serde::de::DeserializeOwned; use serde::Serialize; @@ -9,6 +10,7 @@ use wasm_bindgen::JsValue; use web_sys::{Event, HtmlInputElement, SubmitEvent}; use yew::{Callback, TargetCast, UseStateHandle}; use yew_router::navigator::Navigator; +use yewdux::Dispatch; pub fn onchange(user: &UseStateHandle, field: UserField) -> Callback { let user = user.clone(); @@ -22,21 +24,31 @@ pub fn onchange(user: &UseStateHandle, field: UserField) -> Callback, nav: Navigator, api: Api) -> Callback { +pub fn onsubmit( + user: &UseStateHandle, + nav: Navigator, + api: Api, + dispatch: Dispatch, +) -> Callback { let user = user.clone(); Callback::from(move |e: SubmitEvent| { e.prevent_default(); + let dispatch_clone = dispatch.clone(); let nav = nav.clone(); let user = user.deref().clone(); let hdrs = Headers::new(); hdrs.append("content-type", "application/json"); wasm_bindgen_futures::spawn_local(async move { let body = Some(helpers::to_jsvalue(user)); - let response = api.fetch2(Some(hdrs), body, Method::POST).await; + let response = api.fetch(Some(hdrs), body, Method::POST).await; // navigate home if the submission is successful if let Some(res) = response { if res.status() == 200 { + let obj: Store = serde_json::from_str(&res.text().await.unwrap()).unwrap(); + dispatch_clone.reduce_mut(move |store| { + store.token = obj.token; + }); nav.push(&Route::Home); } } diff --git a/devblog/devblog/client/src/main.rs b/devblog/devblog/client/src/main.rs index 76f366c..c3d1f34 100644 --- a/devblog/devblog/client/src/main.rs +++ b/devblog/devblog/client/src/main.rs @@ -10,6 +10,7 @@ use crate::router::{switch, Route}; use api::*; use components::navbar::Navbar; use models::*; +use store::Store; use stylist::yew::Global; use yew::{function_component, html, Html}; use yew_router::{BrowserRouter, Switch}; diff --git a/devblog/devblog/client/src/models.rs b/devblog/devblog/client/src/models.rs index e057aad..7165a0d 100644 --- a/devblog/devblog/client/src/models.rs +++ b/devblog/devblog/client/src/models.rs @@ -53,7 +53,6 @@ pub struct User { #[serde(rename = "userName")] pub username: String, pub email: String, - pub subscribed: bool, pub password: String, #[serde(rename = "passwordHash")] pub password_hash: String, @@ -77,3 +76,11 @@ pub enum UserField { PasswordHash, Email, } + +#[derive(Clone, Serialize, Deserialize, PartialEq, Debug, Default)] +pub struct UserInfo { + #[serde(rename = "userName")] + pub username: String, + pub subscribed: bool, + pub email: String, +} diff --git a/devblog/devblog/client/src/pages/home.rs b/devblog/devblog/client/src/pages/home.rs index 1a40272..966ea68 100644 --- a/devblog/devblog/client/src/pages/home.rs +++ b/devblog/devblog/client/src/pages/home.rs @@ -19,10 +19,10 @@ pub fn home() -> Html { use_effect_with((), move |_| { wasm_bindgen_futures::spawn_local(async move { - let res = Api::GetPost(-1).fetch2(None, None, Method::GET).await; + let res = Api::GetPost(-1).fetch(None, None, Method::GET).await; helpers::emit(&latest_post_cb, res.unwrap()).await; - let response = Api::GetPostsCount.fetch2(None, None, Method::GET).await; + let response = Api::GetPostsCount.fetch(None, None, Method::GET).await; helpers::emit(&total_posts_count_cb, response.unwrap()).await; }); }); diff --git a/devblog/devblog/client/src/pages/insights.rs b/devblog/devblog/client/src/pages/insights.rs index 718e6e1..385cdc1 100644 --- a/devblog/devblog/client/src/pages/insights.rs +++ b/devblog/devblog/client/src/pages/insights.rs @@ -1,25 +1,29 @@ use crate::{ helpers::{self, CustomCallback}, - Api, User, + store::Store, + Api, UserInfo, }; use gloo_net::http::{Headers, Method}; use stylist::Style; use yew::prelude::*; +use yewdux::prelude::*; const STYLE: &str = include_str!("styles/insights.css"); #[function_component(Insights)] pub fn insights() -> Html { let style = Style::new(STYLE).unwrap(); - let users = use_state(|| vec![User::default()]); + let users = use_state(|| vec![UserInfo::default()]); let users_cb = CustomCallback::new(&users); + let store = use_store_value::(); use_effect_with((), move |_| { wasm_bindgen_futures::spawn_local(async move { let hdrs = Headers::new(); - hdrs.append("Authorization", "Bearer "); + let auth = format!("Bearer {}", store.token.clone()); + hdrs.append("Authorization", &auth); - let res = Api::GetUsers.fetch2(None, None, Method::GET).await; + let res = Api::GetUsers.fetch(Some(hdrs), None, Method::GET).await; helpers::emit(&users_cb, res.unwrap()).await; }); }); diff --git a/devblog/devblog/client/src/pages/posts.rs b/devblog/devblog/client/src/pages/posts.rs index 932c8a4..a58f8c6 100644 --- a/devblog/devblog/client/src/pages/posts.rs +++ b/devblog/devblog/client/src/pages/posts.rs @@ -24,10 +24,10 @@ pub fn posts() -> Html { use_effect_with((), move |_| { wasm_bindgen_futures::spawn_local(async move { - let res = Api::GetPostsCount.fetch2(None, None, Method::GET).await; + let res = Api::GetPostsCount.fetch(None, None, Method::GET).await; helpers::emit(&total_posts_count_cb, res.unwrap()).await; - let res = Api::GetPagesCount.fetch2(None, None, Method::GET).await; + let res = Api::GetPagesCount.fetch(None, None, Method::GET).await; helpers::emit(&total_pages_count_cb, res.unwrap()).await; }); }); @@ -38,7 +38,7 @@ pub fn posts() -> Html { wasm_bindgen_futures::spawn_local(async move { loading_clone.set(false); let num = *page_num_clone as u32; - let res = Api::GetPage(num).fetch2(None, None, Method::GET).await; + let res = Api::GetPage(num).fetch(None, None, Method::GET).await; helpers::emit(&posts_cb, res.unwrap()).await; loading_clone.set(true); }); diff --git a/devblog/devblog/client/src/pages/sign_in.rs b/devblog/devblog/client/src/pages/sign_in.rs index 7e671ce..fe3f15b 100644 --- a/devblog/devblog/client/src/pages/sign_in.rs +++ b/devblog/devblog/client/src/pages/sign_in.rs @@ -1,7 +1,8 @@ -use crate::{components::items::text_input::TextInput, helpers, Api, User, UserField}; +use crate::{components::items::text_input::TextInput, helpers, Api, Store, User, UserField}; use stylist::Style; use yew::prelude::*; use yew_router::hooks::use_navigator; +use yewdux::prelude::*; const STYLE: &str = include_str!("styles/signIn.css"); @@ -10,17 +11,13 @@ pub fn sign_in() -> Html { let style = Style::new(STYLE).unwrap(); let user = use_state(User::default); let nav = use_navigator().unwrap(); - // let (_state, dispatch) = use_store::(); - // let onsubmit = { helpers::onsubmit(&user, nav, Api::SignIn) }; + let (_, dispatch) = use_store::(); html! {