mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-18 23:55:16 +00:00
handle login/logout via frontend
This commit is contained in:
1 parent
6859a05188
commit
13f7c8f3c8
6 files changed
+81
-75
No files matched your search
+11
-1
@@ -6,14 +6,16 @@ use fal_client::creds::fal_api_key::FalApiKey;
|
||||
use log::{error, info};
|
||||
use serde_derive::Deserialize;
|
||||
use tauri::State;
|
||||
use crate::core::state::data_dir::app_data_root::AppDataRoot;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn midjourney_clear_credentials_command(
|
||||
root: State<'_, AppDataRoot>,
|
||||
creds_manager: State<'_, MidjourneyCredentialManager>,
|
||||
) -> SimpleResponse {
|
||||
info!("midjourney_clear_credentials_command called");
|
||||
|
||||
clear_creds(&creds_manager)
|
||||
clear_creds(&root, &creds_manager)
|
||||
.map_err(|err| {
|
||||
error!("Error clearing creds: {:?}", err);
|
||||
"error clearing creds"
|
||||
@@ -23,9 +25,17 @@ pub async fn midjourney_clear_credentials_command(
|
||||
}
|
||||
|
||||
fn clear_creds(
|
||||
root: &AppDataRoot,
|
||||
creds: &MidjourneyCredentialManager,
|
||||
) -> AnyhowResult<()> {
|
||||
|
||||
creds.clear_credentials()?;
|
||||
creds.persist_to_disk()?;
|
||||
|
||||
let creds_path = root.credentials_dir().get_midjourney_state_path();
|
||||
if creds_path.exists() {
|
||||
std::fs::remove_file(creds_path)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -33,7 +33,7 @@ pub fn filter_cloudflare_errors(status_code: u16, body: &str) -> Result<(), Clou
|
||||
</html>
|
||||
*/
|
||||
if is_cloudflare {
|
||||
// TODO
|
||||
// TODO: Include location header.
|
||||
return Err(CloudflareError::MovedPermanently301);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -59,8 +59,8 @@ export const AccountSettingsPane = ({
|
||||
<MidjourneyAccountBlock />
|
||||
<SoraAccountBlock />
|
||||
<FalApiKeyBlock />
|
||||
<OpenAIApiKeyBlock/>
|
||||
{/*
|
||||
<OpenAIApiKeyBlock/>
|
||||
<hr />
|
||||
|
||||
<h2>Coming Soon...</h2>
|
||||
|
||||
+50
-72
@@ -1,106 +1,84 @@
|
||||
import { Button } from "@storyteller/ui-button";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { useEffect, useState } from "react";
|
||||
import { MidjourneyGetCredentialInfo, MidjourneyGetCredentialInfoSuccess } from "@storyteller/tauri-api";
|
||||
import { faSpinnerThird } from "@fortawesome/pro-solid-svg-icons";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
|
||||
export const MidjourneyAccountBlock = () => {
|
||||
/*
|
||||
const [midjourneySession, setMidjourneySession] = useState<CheckSoraSessionResult| undefined>(undefined);
|
||||
const [midjourneySession, setMidjourneySession] = useState<MidjourneyGetCredentialInfoSuccess| undefined>(undefined);
|
||||
const [isCheckingMidjourneySession, setIsCheckingMidjourneySession] = useState(false);
|
||||
|
||||
useSoraLoginListener(() => {
|
||||
setIsCheckingMidjourneySession(true);
|
||||
CheckSoraSession()
|
||||
.then((result) => {
|
||||
setMidjourneySession(result);
|
||||
localStorage.setItem(
|
||||
cacheKey,
|
||||
JSON.stringify({ session: result, timestamp: Date.now() })
|
||||
);
|
||||
})
|
||||
.finally(() => setIsCheckingMidjourneySession(false));
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const fetchSession = async () => {
|
||||
setIsCheckingMidjourneySession(true);
|
||||
try {
|
||||
const cached = localStorage.getItem(cacheKey);
|
||||
if (cached) {
|
||||
const { session, timestamp } = JSON.parse(cached);
|
||||
if (Date.now() - timestamp < cacheTtlMs) {
|
||||
setMidjourneySession(session);
|
||||
setIsCheckingMidjourneySession(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const result = await CheckSoraSession();
|
||||
const result = await MidjourneyGetCredentialInfo();
|
||||
setMidjourneySession(result);
|
||||
localStorage.setItem(
|
||||
cacheKey,
|
||||
JSON.stringify({ session: result, timestamp: Date.now() })
|
||||
);
|
||||
} catch (e) {
|
||||
console.error("Error fetching Sora session", e);
|
||||
setMidjourneySession(undefined);
|
||||
} finally {
|
||||
setIsCheckingMidjourneySession(false);
|
||||
}
|
||||
};
|
||||
fetchSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [cacheKey, cacheTtlMs]);
|
||||
*/
|
||||
|
||||
const handleMidjourneyButton = async () => {
|
||||
//if (isCheckingMidjourneySession) return;
|
||||
//if (midjourneySession?.state === SoraSessionState.Valid) {
|
||||
// setIsCheckingMidjourneySession(true);
|
||||
// await LogoutSoraSession();
|
||||
// setMidjourneySession({state: SoraSessionState.NotSetUp});
|
||||
// localStorage.removeItem(cacheKey);
|
||||
// setIsCheckingMidjourneySession(false);
|
||||
//} else {
|
||||
// setIsCheckingMidjourneySession(true);
|
||||
// try {
|
||||
// await invoke("open_sora_login_command");
|
||||
// } catch (e) {
|
||||
// } finally {
|
||||
// setIsCheckingMidjourneySession(false);
|
||||
// }
|
||||
//}
|
||||
try {
|
||||
await invoke("open_midjourney_login_command");
|
||||
} catch (e) {
|
||||
console.error("Error fetching Midjourney session", e);
|
||||
setMidjourneySession(undefined);
|
||||
} finally {
|
||||
setIsCheckingMidjourneySession(false);
|
||||
}
|
||||
};
|
||||
fetchSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const handleMidjourneyButton = async () => {
|
||||
if (midjourneySession?.payload?.can_clear_state) {
|
||||
await clearState();
|
||||
setMidjourneySession(undefined);
|
||||
//fetchSession();
|
||||
} else {
|
||||
await openLogin();
|
||||
}
|
||||
};
|
||||
|
||||
const clearState = async() => {
|
||||
try {
|
||||
await invoke("midjourney_clear_credentials_command");
|
||||
} catch (e) {
|
||||
console.error("Error clearing Midjourney credentials", e);
|
||||
}
|
||||
}
|
||||
|
||||
const openLogin = async() => {
|
||||
try {
|
||||
await invoke("midjourney_open_login_command");
|
||||
} catch (e) {
|
||||
console.error("Error opening Midjourney login", e);
|
||||
}
|
||||
}
|
||||
|
||||
return(
|
||||
<div className="flex justify-between items-center">
|
||||
<span>Midjourney Account:</span>
|
||||
<pre>{"email@domain.com (later)"}</pre>
|
||||
<pre>{midjourneySession?.payload?.maybe_email || "Not logged in"}</pre>
|
||||
<Button
|
||||
//variant={
|
||||
// midjourneySession?.state === SoraSessionState.Valid && !isCheckingMidjourneySession
|
||||
// ? "destructive"
|
||||
// : midjourneySession?.state === SoraSessionState.NotSetUp
|
||||
// ? "primary"
|
||||
// : "secondary"
|
||||
//}
|
||||
variant={
|
||||
midjourneySession?.payload?.can_clear_state && !isCheckingMidjourneySession
|
||||
? "destructive"
|
||||
: midjourneySession?.payload?.can_clear_state
|
||||
? "primary"
|
||||
: "secondary"
|
||||
}
|
||||
className="h-[30px]"
|
||||
onClick={handleMidjourneyButton}
|
||||
//disabled={isCheckingMidjourneySession}
|
||||
disabled={isCheckingMidjourneySession}
|
||||
>
|
||||
{/*{isCheckingMidjourneySession ? (
|
||||
{isCheckingMidjourneySession ? (
|
||||
<FontAwesomeIcon
|
||||
icon={faSpinnerThird}
|
||||
className="animate-spin text-sm"
|
||||
/>
|
||||
) : midjourneySession?.state === SoraSessionState.Valid ? (
|
||||
) : midjourneySession?.payload?.can_clear_state ? (
|
||||
"Disconnect"
|
||||
) : (
|
||||
"Connect"
|
||||
)}*/}
|
||||
Connect
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -12,6 +12,7 @@ export * from "./lib/fal/FalHunyuanImageTo3d.js";
|
||||
export * from "./lib/fal/FalKlingImageToVideo.js";
|
||||
export * from "./lib/fal/GetFalApiKey.js";
|
||||
export * from "./lib/fal/SetFalApiKey.js";
|
||||
export * from "./lib/midjourney/MidjourneyGetCredentialInfo.js";
|
||||
export * from "./lib/openai/GetOpenAIApiKey.js";
|
||||
export * from "./lib/openai/SetOpenAIApiKey.js";
|
||||
export * from "./lib/providers/GetProviderOrder.js";
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { CommandResult } from "../common/CommandStatus";
|
||||
|
||||
export interface MidjourneyGetCredentialInfoPayload {
|
||||
maybe_email?: string;
|
||||
can_clear_state: boolean;
|
||||
}
|
||||
|
||||
export interface MidjourneyGetCredentialInfoSuccess extends CommandResult {
|
||||
payload: MidjourneyGetCredentialInfoPayload;
|
||||
}
|
||||
|
||||
export const MidjourneyGetCredentialInfo = async (): Promise<MidjourneyGetCredentialInfoSuccess> => {
|
||||
const result = await invoke("midjourney_get_credential_info_command");
|
||||
console.log(">>> mj result", result);
|
||||
return result as MidjourneyGetCredentialInfoSuccess;
|
||||
};
|
||||
Reference in new issue
Block a user