feat: add email to display at home page

This commit is contained in:
Fu Zi Xiang 2023-10-11 15:41:18 +08:00
parent 646fae7c6e
commit c76252be0a
No known key found for this signature in database
GPG Key ID: 7AE0884D237CEE16
3 changed files with 34 additions and 11 deletions

View File

@ -6,7 +6,9 @@ pub struct Login;
#[derive(Template)]
#[template(path = "home.html")]
pub struct Home;
pub struct Home<'a> {
pub email: &'a str,
}
#[derive(Template)]
#[template(path = "admin.html")]

View File

@ -17,8 +17,26 @@ pub fn router() -> Router<AppState> {
.route("/admin/users/:user_id", get(admin_user_details_handler))
}
pub async fn home_handler(_: UserSession) -> Result<Html<String>, RenderError> {
let s = templates::Home {}.render()?;
pub async fn login_handler() -> Result<Html<String>, RenderError> {
let s = templates::Login {}.render()?;
Ok(Html(s))
}
pub async fn home_handler(
State(state): State<AppState>,
session: UserSession,
) -> Result<Html<String>, RenderError> {
let user_email = state
.gotrue_client
.user_info(&session.access_token)
.await
.map(|user_info| user_info.email)
.unwrap_or_else(|err| {
println!("Failed to fetch user info: {:?}", err);
"".to_owned()
});
let s = templates::Home { email: &user_email }.render()?;
Ok(Html(s))
}
@ -60,8 +78,3 @@ pub async fn admin_user_details_handler(
let s = templates::UserDetails { user: &users }.render()?;
Ok(Html(s))
}
pub async fn login_handler() -> Result<Html<String>, RenderError> {
let s = templates::Login {}.render()?;
Ok(Html(s))
}

View File

@ -1,9 +1,17 @@
<!doctype html>
<html lang="en">
<body>
<h1>Welcome to the Home Page</h1>
<button id="logoutBtn">Logout</button>
<button id="adminBtn">Admin</button>
<div id="content">
<h1>User Management</h1>
<p>Welcome, {{ email }}</p>
</div>
<div id="menu">
<button id="adminBtn">Admin</button>
<button id="logoutBtn">Logout</button>
</div>
<script>
document
.getElementById("logoutBtn")