From 0947062d9014f7e283c6b30fb899261edecfb5c1 Mon Sep 17 00:00:00 2001 From: dignow Date: Thu, 22 Jun 2023 12:18:03 +0800 Subject: [PATCH] simple refact, store user info Signed-off-by: dignow --- src/hbbs_http/account.rs | 34 ++-------------------------------- src/ui_interface.rs | 6 +----- 2 files changed, 3 insertions(+), 37 deletions(-) diff --git a/src/hbbs_http/account.rs b/src/hbbs_http/account.rs index 116c0ad02..a2ef53029 100644 --- a/src/hbbs_http/account.rs +++ b/src/hbbs_http/account.rs @@ -4,7 +4,6 @@ use hbb_common::{ log, ResultType, }; use reqwest::blocking::Client; -use serde::ser::SerializeStruct; use serde_derive::{Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::{ @@ -81,7 +80,7 @@ pub enum UserStatus { Unverified = -1, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct UserPayload { pub name: String, pub email: Option, @@ -90,9 +89,6 @@ pub struct UserPayload { pub info: UserInfo, pub is_admin: bool, pub third_auth_type: Option, - // helper field for serialize - #[serde(default)] - pub ser_store_local: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -121,30 +117,6 @@ pub struct AuthResult { pub auth_body: Option, } -impl serde::Serialize for UserPayload { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - if self.ser_store_local { - let mut state = serializer.serialize_struct("UserPayload", 1)?; - state.serialize_field("name", &self.name)?; - state.serialize_field("status", &self.status)?; - state.end() - } else { - let mut state = serializer.serialize_struct("UserPayload", 7)?; - state.serialize_field("name", &self.name)?; - state.serialize_field("email", &self.email)?; - state.serialize_field("note", &self.note)?; - state.serialize_field("status", &self.status)?; - state.serialize_field("info", &self.info)?; - state.serialize_field("is_admin", &self.is_admin)?; - state.serialize_field("third_auth_type", &self.third_auth_type)?; - state.end() - } - } -} - impl OidcSession { fn new() -> Self { Self { @@ -250,12 +222,10 @@ impl OidcSession { "access_token".to_owned(), auth_body.access_token.clone(), ); - auth_body.user.ser_store_local = true; LocalConfig::set_option( "user_info".to_owned(), - serde_json::to_string(&auth_body.user).unwrap_or_default(), + serde_json::json!({ "name": auth_body.user.name, "status": auth_body.user.status }).to_string(), ); - auth_body.user.ser_store_local = false; } OIDC_SESSION .write() diff --git a/src/ui_interface.rs b/src/ui_interface.rs index 834a2a581..e298e1167 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -822,11 +822,7 @@ pub fn account_auth_cancel() { #[cfg(feature = "flutter")] pub fn account_auth_result() -> String { - let mut auth_result = account::OidcSession::get_result(); - if let Some(auth) = auth_result.auth_body.as_mut() { - auth.user.ser_store_local = false; - } - serde_json::to_string(&auth_result).unwrap_or_default() + serde_json::to_string(&account::OidcSession::get_result()).unwrap_or_default() } #[cfg(feature = "flutter")]