Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix auth store

Brendan Allanc56a75703bcb
diff --git a/apps/desktop/src-tauri/src/auth.rs b/apps/desktop/src-tauri/src/auth.rs
index 6afdf16af..68338f723 100644
--- a/apps/desktop/src-tauri/src/auth.rs
+++ b/apps/desktop/src-tauri/src/auth.rs
@@ -7,7 +7,7 @@ use tauri_plugin_store::{with_store, StoreCollection};
pub struct AuthStore {
pub token: String,
pub expires: i32,
- pub plan: Plan,
+ pub plan: Option<Plan>,
}
#[derive(Serialize, Deserialize, Type)]
@@ -22,35 +22,11 @@ impl AuthStore {
.try_state::<StoreCollection<Wry>>()
.ok_or("Store not found")?;
with_store(app.clone(), stores, "store", |store| {
- let Some(store_value) = store.get("auth").cloned() else {
+ let Some(store) = store.get("auth").cloned() else {
return Ok(None);
};
- // Try to deserialize into the new structure
- match serde_json::from_value::<Self>(store_value.clone()) {
- Ok(auth_store) => Ok(Some(auth_store)),
- Err(_) => {
- // If deserialization fails, it might be due to the old structure
- // Try to deserialize into a temporary struct without the plan field
- #[derive(Deserialize)]
- struct OldAuthStore {
- token: String,
- expires: i32,
- }
-
- let old_auth: OldAuthStore = serde_json::from_value(store_value)?;
-
- // Create a new AuthStore with default Plan values
- Ok(Some(Self {
- token: old_auth.token,
- expires: old_auth.expires,
- plan: Plan {
- upgraded: false,
- last_checked: 0,
- },
- }))
- }
- }
+ Ok(serde_json::from_value(store)?)
})
.map_err(|e| e.to_string())
}
@@ -77,16 +53,27 @@ impl AuthStore {
return Err(error_msg);
}
- let plan_response: serde_json::Value = response.json().await.map_err(|e| e.to_string())?;
+ #[derive(Deserialize)]
+ struct Response {
+ upgraded: bool,
+ }
+
+ let plan_response: Response = response.json().await.map_err(|e| e.to_string())?;
- auth.plan.upgraded = plan_response["upgraded"].as_bool().unwrap_or(false);
- auth.plan.last_checked = chrono::Utc::now().timestamp() as i32;
+ auth.plan = Some(Plan {
+ upgraded: plan_response.upgraded,
+ last_checked: chrono::Utc::now().timestamp() as i32,
+ });
let _ = Self::set(app, Some(auth))?;
Ok(())
}
+ pub fn is_upgraded(&self) -> bool {
+ self.plan.as_ref().map_or(false, |plan| plan.upgraded)
+ }
+
pub fn set(app: &AppHandle, value: Option<Self>) -> Result<(), String> {
let stores = app
.try_state::<StoreCollection<Wry>>()
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index f67673441..0a5b6862b 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -1547,7 +1547,7 @@ async fn upload_rendered_video(
};
// Check if user has an upgraded plan
- if !auth.plan.upgraded {
+ if !auth.is_upgraded() {
// Fetch and update plan information
if let Err(e) = AuthStore::fetch_and_update_plan(&app).await {
println!("Failed to update plan information: {}", e);
@@ -1558,7 +1558,7 @@ async fn upload_rendered_video(
auth = AuthStore::get(&app).unwrap().unwrap();
// Re-check upgraded status after refresh
- if !auth.plan.upgraded {
+ if !auth.is_upgraded() {
// Open upgrade window instead of returning an error
open_upgrade_window(app).await;
return Ok(UploadResult::UpgradeRequired);
@@ -1636,7 +1636,7 @@ async fn upload_screenshot(
return Ok(UploadResult::NotAuthenticated);
};
- if !auth.plan.upgraded {
+ if !auth.is_upgraded() {
println!("User plan not upgraded. Fetching and updating plan information.");
// Fetch and update plan information
if let Err(e) = AuthStore::fetch_and_update_plan(&app).await {
@@ -1662,7 +1662,7 @@ async fn upload_screenshot(
};
// Re-check upgraded status after refresh
- if !auth.plan.upgraded {
+ if !auth.is_upgraded() {
println!("User plan still not upgraded after refresh.");
open_upgrade_window(app).await;
return Ok(UploadResult::UpgradeRequired);
@@ -2003,7 +2003,7 @@ async fn check_upgraded_and_update(app: AppHandle) -> Result<bool, String> {
let auth = AuthStore::get(&app).map_err(|e| e.to_string())?;
- Ok(auth.map_or(false, |a| a.plan.upgraded))
+ Ok(auth.map_or(false, |a| a.is_upgraded()))
}
#[tauri::command]
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index 96d26e91e..da7548dac 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -328,7 +328,7 @@ export type AspectRatio = "wide" | "vertical" | "square" | "classic" | "tall"
export type Audio = { duration: number; sample_rate: number; channels: number }
export type AudioConfiguration = { mute: boolean; improve: boolean }
export type AudioMeta = { path: string }
-export type AuthStore = { token: string; expires: number; plan: Plan }
+export type AuthStore = { token: string; expires: number; plan: Plan | null }
export type BackgroundConfiguration = { source: BackgroundSource; blur: number; padding: number; rounding: number; inset: number; crop: Crop | null }
export type BackgroundSource = { type: "wallpaper"; id: number } | { type: "image"; path: string | null } | { type: "color"; value: [number, number, number] } | { type: "gradient"; from: [number, number, number]; to: [number, number, number]; angle?: number }
export type Bounds = { x: number; y: number; width: number; height: number }