Skip to content

Commit 981dfcf

Browse files
committed
improvements with app layout
1 parent e8c5d78 commit 981dfcf

6 files changed

Lines changed: 60 additions & 15 deletions

File tree

src/adapters/grpc_api/misc_api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ impl Misc for MiscGrpcApi {
5353

5454
let password_salt = get_env("AVORED_PASSWORD_SALT")?;
5555

56-
println!("Pass salt: {}", password_salt);
56+
// println!("Pass salt: {}", password_salt);
5757

5858
let password_hash = storable_admin_user.password_hash.get_password_hash(&password_salt)?;
5959

60-
println!("Pass hash: {}", password_hash);
60+
// println!("Pass hash: {}", password_hash);
6161
storable_admin_user.logged_in_user = "ApplicationSetupProcess".to_string();
6262
storable_admin_user.is_super_admin = true;
6363
storable_admin_user.password_hash = password_hash;
6464

65-
println!("Got a setup request: {:?}", storable_admin_user);
65+
// println!("Got a setup request: {:?}", storable_admin_user);
6666

6767
// Think of implementing a generic result handler
6868
let setup_result = match self.misc_use_case.setup(storable_admin_user).await {

src/infra/repositories/user_repository.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl UserRepository for SurrealUserRepository {
2323
// let object = response
2424
let results = response.take::<Vec<Value>>(0).ok()?;
2525

26-
println!("Results: {:#?}", results);
26+
// println!("Results: {:#?}", results);
2727

2828
let user = match results.first() {
2929
Some(value) => value,

src/infra/setup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl FromRef<AppState> for LeptosOptions {
4242
fn load_env() -> Result<()>{
4343
dotenvy::dotenv()?;
4444

45-
println!("here");
45+
// println!("here");
4646
match get_env("APP_ENV")?.as_str() {
4747
"prod" => dotenvy::from_filename_override(".env.prod")?,
4848
"stag" => dotenvy::from_filename_override(".env.stag")?,

src/pages/app_layout.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub fn AppLayout() -> impl IntoView {
2121
}
2222
};
2323

24+
let auth_context = use_context::<crate::pages::protected_routes::AuthContext>().expect("AuthContext should be provided");
25+
let full_name = auth_context.full_name;
26+
let is_super_admin = auth_context.is_super_admin;
27+
2428
view! {
2529
<div class="flex h-screen bg-gray-100">
2630
// Sidebar
@@ -66,7 +70,7 @@ pub fn AppLayout() -> impl IntoView {
6670
</a>
6771
<a href="/admin/settings" class="flex items-center gap-3 px-3 py-2 rounded-lg text-gray-300 hover:bg-slate-800 hover:text-white transition-colors">
6872
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
69-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
73+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924-1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
7074
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
7175
</svg>
7276
<span>"Settings"</span>
@@ -111,12 +115,14 @@ pub fn AppLayout() -> impl IntoView {
111115

112116
<div class="flex items-center gap-3 border-l pl-4 border-gray-200">
113117
<div class="text-right hidden sm:block">
114-
<div class="text-sm font-medium text-gray-900">"Admin User"</div>
115-
<div class="text-xs text-gray-500">"Super Admin"</div>
118+
<div class="text-sm font-medium text-gray-900">{full_name}</div>
119+
<div class="text-xs text-gray-500">
120+
{move || if is_super_admin.get() { "Super Admin" } else { "Admin" }}
121+
</div>
116122
</div>
117123
<img
118124
class="h-8 w-8 rounded-full border border-gray-300 cursor-pointer"
119-
src="https://ui-avatars.com/api/?name=Admin+User&background=0D8ABC&color=fff"
125+
src=move || format!("https://ui-avatars.com/api/?name={}&background=0D8ABC&color=fff", full_name.get())
120126
alt="User Profile"
121127
on:click=on_logout_click
122128
/>

src/pages/auth/login_page.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@ pub fn LoginPage() -> impl IntoView {
6363
if !token.is_empty() {
6464
#[cfg(target_arch = "wasm32")]
6565
{
66+
use crate::pages::protected_routes::AuthData;
6667
use gloo_storage::{LocalStorage, Storage};
6768
let _ = LocalStorage::set("avored_admin_token", token.clone());
68-
auth_context.auth_token.set(token);
69-
auth_context.is_logged_in.set(true);
69+
if let Ok(auth_data) = serde_json::from_str::<AuthData>(&token) {
70+
auth_context.auth_token.set(auth_data.token);
71+
auth_context.is_logged_in.set(true);
72+
auth_context.full_name.set(auth_data.admin_user.full_name);
73+
auth_context.is_super_admin.set(auth_data.admin_user.is_super_admin);
74+
}
7075

7176
let navigate = leptos_router::hooks::use_navigate();
7277
navigate("/admin/dashboard", Default::default());

src/pages/protected_routes.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
1-
use leptos::prelude::*;
1+
use leptos::{prelude::*};
22
use leptos_router::hooks::use_navigate;
33
// use gloo_storage::{LocalStorage, Storage};
44
// use anyhow::Result;
55
#[cfg(target_arch = "wasm32")]
66
use gloo_storage::Storage;
77

8+
89
#[derive(Clone, Default)]
910
pub struct AuthContext {
1011
pub is_logged_in: RwSignal<bool>,
1112
pub auth_token: RwSignal<String>,
13+
pub full_name: RwSignal<String>,
14+
pub is_super_admin: RwSignal<bool>,
15+
}
16+
17+
#[derive(Clone, Default, serde::Serialize, serde::Deserialize, Debug)]
18+
pub struct AdminUserContext {
19+
pub full_name: String,
20+
pub email: String,
21+
pub profile_image: String,
22+
pub is_super_admin: bool,
23+
pub created_by: String,
24+
pub updated_by: String,
25+
pub created_at: String,
26+
pub updated_at: String,
27+
28+
}
29+
30+
#[derive(Clone, Default, serde::Serialize, serde::Deserialize, Debug)]
31+
pub struct AuthData {
32+
pub admin_user: AdminUserContext,
33+
pub token: String,
1234
}
1335

1436
// Function to provide the AuthContext
1537
pub fn provide_auth_context() {
1638
let is_logged_in = RwSignal::new(false);
1739
let auth_token = RwSignal::new(String::new());
40+
let full_name = RwSignal::new(String::new());
41+
let is_super_admin = RwSignal::new(false);
42+
1843
// On the client, read from local storage
1944
#[cfg(target_arch = "wasm32")]
2045
{
@@ -24,12 +49,21 @@ pub fn provide_auth_context() {
2449
// Also need to ensure gloo_storage is available or use runtime check if needed, but strict cfg is safer.
2550
// Using a block to contain the usage.
2651
if let Ok(value) = gloo_storage::LocalStorage::get::<String>("avored_admin_token") {
27-
auth_token.set(value.clone());
28-
is_logged_in.set(!value.is_empty());
52+
if let Ok(auth_data) = serde_json::from_str::<AuthData>(&value) {
53+
auth_token.set(auth_data.token.clone());
54+
is_logged_in.set(!auth_data.token.is_empty());
55+
full_name.set(auth_data.admin_user.full_name.clone());
56+
is_super_admin.set(auth_data.admin_user.is_super_admin);
57+
}
2958
}
3059
});
3160
}
32-
provide_context(AuthContext { is_logged_in, auth_token });
61+
provide_context(AuthContext {
62+
is_logged_in,
63+
auth_token,
64+
full_name,
65+
is_super_admin,
66+
});
3367
}
3468

3569
#[component]

0 commit comments

Comments
 (0)