From 01e90ea0a1cd0b4b3b905dc9964a0799a0d85750 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 11 Jun 2021 14:24:49 +0000 Subject: [PATCH] Use jwt token when calling graphql --- web-ui/crate/src/api/dicebot.rs | 10 ++++++++-- web-ui/crate/src/error.rs | 3 +++ web-ui/crate/src/rooms.rs | 27 +++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/web-ui/crate/src/api/dicebot.rs b/web-ui/crate/src/api/dicebot.rs index 5e8359a..9e60c28 100644 --- a/web-ui/crate/src/api/dicebot.rs +++ b/web-ui/crate/src/api/dicebot.rs @@ -22,11 +22,14 @@ struct GetUserVariable; struct RoomsForUser; pub async fn get_user_variable( + jwt_token: &str, room_id: &str, user_id: &str, variable_name: &str, ) -> Result { - let client = Client::new("http://localhost:10000/graphql"); + let mut client = Client::new("http://localhost:10000/graphql"); + client.add_header("Authorization", &format!("Bearer {}", jwt_token)); + let variables = get_user_variable::Variables { room_id: room_id.to_owned(), user_id: user_id.to_owned(), @@ -39,9 +42,12 @@ pub async fn get_user_variable( } pub async fn rooms_for_user( + jwt_token: &str, user_id: &str, ) -> Result, UiError> { - let client = Client::new("http://localhost:10000/graphql"); + let mut client = Client::new("http://localhost:10000/graphql"); + client.add_header("Authorization", &format!("Bearer {}", jwt_token)); + let variables = rooms_for_user::Variables { user_id: user_id.to_owned(), }; diff --git a/web-ui/crate/src/error.rs b/web-ui/crate/src/error.rs index aa01416..c7184e0 100644 --- a/web-ui/crate/src/error.rs +++ b/web-ui/crate/src/error.rs @@ -10,6 +10,9 @@ pub enum UiError { #[error("error: {0}")] ApiError(String), + #[error("login token invalid or expired")] + NotLoggedIn, + #[error("error: {0}")] JsError(String), } diff --git a/web-ui/crate/src/rooms.rs b/web-ui/crate/src/rooms.rs index 58b96a1..bf456b8 100644 --- a/web-ui/crate/src/rooms.rs +++ b/web-ui/crate/src/rooms.rs @@ -26,7 +26,13 @@ fn view_room(room: &Room) -> Html { } async fn load_rooms(dispatch: &WebUiDispatcher) -> Result<(), UiError> { - let rooms = api::dicebot::rooms_for_user("@projectmoon:agnos.is").await?; + let jwt_token = dispatch + .state() + .jwt_token + .as_ref() + .ok_or(UiError::NotLoggedIn)?; + + let rooms = api::dicebot::rooms_for_user(jwt_token, "@projectmoon:agnos.is").await?; for room in rooms { dispatch.send(Action::AddRoom(Room { @@ -76,11 +82,24 @@ impl Component for YewduxRoomList { } fn view(&self) -> Html { - let the_future = self.link.callback(move |_| {}); - let dispatch = Arc::new(self.dispatch.clone()); - let jwt_update = self.link.callback(move |_| { + let dispatch2 = dispatch.clone(); + + let the_future = self.link.callback(move |_| { let dispatch = dispatch.clone(); + + spawn_local(async move { + //TODO make macro to report errors in some common way: + //handle_errors!(do_things(&*dispatch).await) + match load_rooms(&*dispatch).await { + Err(e) => console::log_1(&format!("Error: {:?}", e).into()), + _ => (), + } + }); + }); + + let jwt_update = self.link.callback(move |_| { + let dispatch = dispatch2.clone(); spawn_local(async move { do_jwt_stuff(&*dispatch).await; });