1- use leptos:: prelude:: * ;
1+ use leptos:: { prelude:: * } ;
22use leptos_router:: hooks:: use_navigate;
33// use gloo_storage::{LocalStorage, Storage};
44// use anyhow::Result;
55#[ cfg( target_arch = "wasm32" ) ]
66use gloo_storage:: Storage ;
77
8+
89#[ derive( Clone , Default ) ]
910pub 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
1537pub 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