From c91ea9234a78a3e32cd0dfeef489a6b3fb88587a Mon Sep 17 00:00:00 2001 From: Fu Zi Xiang Date: Tue, 10 Oct 2023 16:57:02 +0800 Subject: [PATCH] feat: show user list on frontend --- admin_frontend/README.md | 1 + admin_frontend/dev/README.md | 1 + admin_frontend/src/access_token.rs | 4 +-- admin_frontend/src/templates.rs | 6 ++++ admin_frontend/src/web_app.rs | 16 +++++++-- admin_frontend/templates/home.html | 3 +- admin_frontend/templates/users.html | 51 +++++++++++------------------ 7 files changed, 46 insertions(+), 36 deletions(-) diff --git a/admin_frontend/README.md b/admin_frontend/README.md index 2f98520f..e82b107d 100644 --- a/admin_frontend/README.md +++ b/admin_frontend/README.md @@ -1,4 +1,5 @@ # Admin Frontend + - Start the whole stack: `docker compose up -d` - Go to [web server](localhost) - Quick rebuild only frontend after editing `docker compose up -d --no-deps --build admin_frontend` diff --git a/admin_frontend/dev/README.md b/admin_frontend/dev/README.md index 725ba098..4cab1543 100644 --- a/admin_frontend/dev/README.md +++ b/admin_frontend/dev/README.md @@ -1,4 +1,5 @@ # Development + - Run the dev docker compose in the previous directory: `docker compose --file docker-compose-dev.yml up -d` - cp `dev.env` to `.env` - run the frontend: `cargo watch -x run -w .` diff --git a/admin_frontend/src/access_token.rs b/admin_frontend/src/access_token.rs index d7267c2a..ec43eb59 100644 --- a/admin_frontend/src/access_token.rs +++ b/admin_frontend/src/access_token.rs @@ -23,9 +23,9 @@ where let token = jar .get("access_token") .ok_or(AccessTokenRejection::NoAccessToken)? - .to_string(); + .value(); - Ok(WebAccessToken(token)) + Ok(WebAccessToken(token.to_string())) } } diff --git a/admin_frontend/src/templates.rs b/admin_frontend/src/templates.rs index 184563ac..776a5ba1 100644 --- a/admin_frontend/src/templates.rs +++ b/admin_frontend/src/templates.rs @@ -11,3 +11,9 @@ pub struct Home; #[derive(Template)] #[template(path = "admin.html")] pub struct Admin; + +#[derive(Template)] +#[template(path = "users.html")] +pub struct Users<'a> { + pub users: &'a [gotrue_entity::User], +} diff --git a/admin_frontend/src/web_app.rs b/admin_frontend/src/web_app.rs index 40f7afc2..21589207 100644 --- a/admin_frontend/src/web_app.rs +++ b/admin_frontend/src/web_app.rs @@ -30,8 +30,20 @@ pub async fn admin_users_handler( State(state): State, access_token: WebAccessToken, ) -> Result, RenderError> { - let users = state.gotrue_client.admin_list_user(&access_token.0).await; - todo!() + let users = state + .gotrue_client + .admin_list_user(&access_token.0) + .await + .map_or_else( + |err| { + // Log the error and return an empty vector. + println!("Failed to fetch users: {:?}", err); + vec![] + }, + |r| r.users, + ); + let s = templates::Users { users: &users }.render()?; + Ok(Html(s)) } pub async fn login_handler() -> Result, RenderError> { diff --git a/admin_frontend/templates/home.html b/admin_frontend/templates/home.html index cac8b8be..b1fe2f71 100644 --- a/admin_frontend/templates/home.html +++ b/admin_frontend/templates/home.html @@ -9,7 +9,8 @@ .getElementById("logoutBtn") .addEventListener("click", function () { // Remove the cookie by setting its value to an empty string and its expiry date to the past. - document.cookie = "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; + document.cookie = + "access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; // Redirect to the login page. window.location.href = "/web/login"; diff --git a/admin_frontend/templates/users.html b/admin_frontend/templates/users.html index 09773c27..e1d665bb 100644 --- a/admin_frontend/templates/users.html +++ b/admin_frontend/templates/users.html @@ -1,36 +1,25 @@ - + - + +

User List

+
    -

    User List

    -
      + + + + + + - + {% for user in users %} + + + + + + {% endfor %} +
      IdEmailCreated At
      {{ user.id|escape }}{{ user.email|escape }}{{ user.created_at|escape }}
      - + +