Use jwt token when calling graphql

This commit is contained in:
projectmoon 2021-06-11 14:24:49 +00:00
parent eeacf01cbe
commit 01e90ea0a1
3 changed files with 34 additions and 6 deletions

View File

@ -22,11 +22,14 @@ struct GetUserVariable;
struct RoomsForUser; struct RoomsForUser;
pub async fn get_user_variable( pub async fn get_user_variable(
jwt_token: &str,
room_id: &str, room_id: &str,
user_id: &str, user_id: &str,
variable_name: &str, variable_name: &str,
) -> Result<i64, UiError> { ) -> Result<i64, 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 = get_user_variable::Variables { let variables = get_user_variable::Variables {
room_id: room_id.to_owned(), room_id: room_id.to_owned(),
user_id: user_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( pub async fn rooms_for_user(
jwt_token: &str,
user_id: &str, user_id: &str,
) -> Result<Vec<rooms_for_user::RoomsForUserUserRoomsRooms>, UiError> { ) -> Result<Vec<rooms_for_user::RoomsForUserUserRoomsRooms>, 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 { let variables = rooms_for_user::Variables {
user_id: user_id.to_owned(), user_id: user_id.to_owned(),
}; };

View File

@ -10,6 +10,9 @@ pub enum UiError {
#[error("error: {0}")] #[error("error: {0}")]
ApiError(String), ApiError(String),
#[error("login token invalid or expired")]
NotLoggedIn,
#[error("error: {0}")] #[error("error: {0}")]
JsError(String), JsError(String),
} }

View File

@ -26,7 +26,13 @@ fn view_room(room: &Room) -> Html {
} }
async fn load_rooms(dispatch: &WebUiDispatcher) -> Result<(), UiError> { 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 { for room in rooms {
dispatch.send(Action::AddRoom(Room { dispatch.send(Action::AddRoom(Room {
@ -76,11 +82,24 @@ impl Component for YewduxRoomList {
} }
fn view(&self) -> Html { fn view(&self) -> Html {
let the_future = self.link.callback(move |_| {});
let dispatch = Arc::new(self.dispatch.clone()); 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(); 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 { spawn_local(async move {
do_jwt_stuff(&*dispatch).await; do_jwt_stuff(&*dispatch).await;
}); });