Fix more electron APi routes and issues

This commit is contained in:
LukeGus
2025-09-11 17:18:44 -05:00
parent a4ca1512fc
commit 54fb8ffc24
8 changed files with 116 additions and 33 deletions

View File

@@ -79,18 +79,43 @@ export function AdminSettings({isTopbarOpen = true}: AdminSettingsProps): React.
React.useEffect(() => {
const jwt = getCookie("jwt");
if (!jwt) return;
// Check if we're in Electron and have a server configured
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
if (isElectron) {
// In Electron, check if we have a configured server
const serverUrl = (window as any).configuredServerUrl;
if (!serverUrl) {
console.log('No server configured in Electron, skipping API calls');
return;
}
}
getOIDCConfig()
.then(res => {
if (res) setOidcConfig(res);
})
.catch((err) => {
console.error('Failed to fetch OIDC config:', err);
toast.error(t('admin.failedToFetchOidcConfig'));
// Only show error if it's not a "no server configured" error
if (!err.message?.includes('No server configured')) {
toast.error(t('admin.failedToFetchOidcConfig'));
}
});
fetchUsers();
}, []);
React.useEffect(() => {
// Check if we're in Electron and have a server configured
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
if (isElectron) {
const serverUrl = (window as any).configuredServerUrl;
if (!serverUrl) {
console.log('No server configured in Electron, skipping registration status check');
return;
}
}
getRegistrationAllowed()
.then(res => {
if (typeof res?.allowed === 'boolean') {
@@ -99,17 +124,37 @@ export function AdminSettings({isTopbarOpen = true}: AdminSettingsProps): React.
})
.catch((err) => {
console.error('Failed to fetch registration status:', err);
toast.error(t('admin.failedToFetchRegistrationStatus'));
// Only show error if it's not a "no server configured" error
if (!err.message?.includes('No server configured')) {
toast.error(t('admin.failedToFetchRegistrationStatus'));
}
});
}, []);
const fetchUsers = async () => {
const jwt = getCookie("jwt");
if (!jwt) return;
// Check if we're in Electron and have a server configured
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
if (isElectron) {
const serverUrl = (window as any).configuredServerUrl;
if (!serverUrl) {
console.log('No server configured in Electron, skipping user fetch');
return;
}
}
setUsersLoading(true);
try {
const response = await getUserList();
setUsers(response.users);
} catch (err) {
console.error('Failed to fetch users:', err);
// Only show error if it's not a "no server configured" error
if (!err.message?.includes('No server configured')) {
toast.error(t('admin.failedToFetchUsers'));
}
} finally {
setUsersLoading(false);
}

View File

@@ -212,7 +212,7 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
const baseUrl = (window as any).configuredServerUrl || 'http://127.0.0.1:8081';
// Convert HTTP/HTTPS to WS/WSS and use nginx reverse proxy path
const wsProtocol = baseUrl.startsWith('https://') ? 'wss://' : 'ws://';
const wsHost = baseUrl.replace(/^https?:\/\//, '').replace(/:\d+$/, ''); // Remove port if present
const wsHost = baseUrl.replace(/^https?:\/\//, ''); // Keep the port
return `${wsProtocol}${wsHost}/ssh/websocket/`;
})()
: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}/ssh/websocket/`;

View File

@@ -185,12 +185,12 @@ export function HomepageAuth({
setLoggedIn(true);
setIsAdmin(!!meRes.is_admin);
setUsername(meRes.username || null);
setUserId(meRes.userId || null);
setUserId(meRes.id || null);
setDbError(null);
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null
userId: meRes.id || null
});
setInternalLoggedIn(true);
if (tab === "signup") {
@@ -320,12 +320,12 @@ export function HomepageAuth({
setLoggedIn(true);
setIsAdmin(!!meRes.is_admin);
setUsername(meRes.username || null);
setUserId(meRes.userId || null);
setUserId(meRes.id || null);
setDbError(null);
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null
userId: meRes.id || null
});
setInternalLoggedIn(true);
setTotpRequired(false);
@@ -635,14 +635,29 @@ export function HomepageAuth({
<div className="text-center text-muted-foreground mb-4">
<p>{t('auth.loginWithExternalDesc')}</p>
</div>
<Button
type="button"
className="w-full h-11 mt-2 text-base font-semibold"
disabled={oidcLoading}
onClick={handleOIDCLogin}
>
{oidcLoading ? Spinner : t('auth.loginWithExternal')}
</Button>
{(() => {
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
if (isElectron) {
return (
<div className="text-center p-4 bg-muted/50 rounded-lg border">
<p className="text-muted-foreground text-sm">
{t('auth.externalNotSupportedInElectron')}
</p>
</div>
);
} else {
return (
<Button
type="button"
className="w-full h-11 mt-2 text-base font-semibold"
disabled={oidcLoading}
onClick={handleOIDCLogin}
>
{oidcLoading ? Spinner : t('auth.loginWithExternal')}
</Button>
);
}
})()}
</>
)}
{tab === "reset" && (

View File

@@ -6,6 +6,7 @@ import {
Hammer, ChevronUp, User2, HardDrive, Trash2, Users, Shield, Settings, Menu, ChevronRight
} from "lucide-react";
import { useTranslation } from 'react-i18next';
import {getCookie, setCookie} from "@/ui/main-axios.ts";
import {
Sidebar,
@@ -86,17 +87,18 @@ interface SidebarProps {
}
function handleLogout() {
document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
// Clear the JWT token using the proper cookie functions
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
if (isElectron) {
localStorage.removeItem('jwt');
} else {
document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
window.location.reload();
}
function getCookie(name: string) {
return document.cookie.split('; ').reduce((r, v) => {
const parts = v.split('=');
return parts[0] === name ? decodeURIComponent(parts[1]) : r;
}, "");
}
export function LeftSidebar({