fix: Fix electron login and mobile redirect

This commit is contained in:
LukeGus
2025-11-03 23:36:41 -06:00
parent 1e08a179bd
commit cfe9e3f959
6 changed files with 200 additions and 368 deletions

View File

@@ -578,19 +578,6 @@ class AuthManager {
sessionId, sessionId,
}); });
} }
} else {
try {
await db.delete(sessions).where(eq(sessions.userId, userId));
} catch (error) {
databaseLogger.error(
"Failed to delete user sessions on logout",
error,
{
operation: "sessions_delete_logout_failed",
userId,
},
);
}
} }
} }

View File

@@ -52,6 +52,9 @@ import {
getUserInfo, getUserInfo,
getCookie, getCookie,
isElectron, isElectron,
getSessions,
revokeSession,
revokeAllUserSessions,
} from "@/ui/main-axios.ts"; } from "@/ui/main-axios.ts";
interface AdminSettingsProps { interface AdminSettingsProps {
@@ -126,6 +129,7 @@ export function AdminSettings({
expiresAt: string; expiresAt: string;
lastActiveAt: string; lastActiveAt: string;
jwtToken: string; jwtToken: string;
isRevoked?: boolean;
}> }>
>([]); >([]);
const [sessionsLoading, setSessionsLoading] = React.useState(false); const [sessionsLoading, setSessionsLoading] = React.useState(false);
@@ -565,35 +569,8 @@ export function AdminSettings({
setSessionsLoading(true); setSessionsLoading(true);
try { try {
const isDev = const data = await getSessions();
!isElectron() &&
process.env.NODE_ENV === "development" &&
(window.location.port === "3000" ||
window.location.port === "5173" ||
window.location.port === "" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1");
const apiUrl = isElectron()
? `${(window as { configuredServerUrl?: string }).configuredServerUrl}/users/sessions`
: isDev
? `http://localhost:30001/users/sessions`
: `/users/sessions`;
const response = await fetch(apiUrl, {
method: "GET",
credentials: "include",
headers: {
Authorization: `Bearer ${getCookie("jwt")}`,
},
});
if (response.ok) {
const data = await response.json();
setSessions(data.sessions || []); setSessions(data.sessions || []);
} else {
toast.error(t("admin.failedToFetchSessions"));
}
} catch (err) { } catch (err) {
if (!err?.message?.includes("No server configured")) { if (!err?.message?.includes("No server configured")) {
toast.error(t("admin.failedToFetchSessions")); toast.error(t("admin.failedToFetchSessions"));
@@ -612,30 +589,7 @@ export function AdminSettings({
t("admin.confirmRevokeSession"), t("admin.confirmRevokeSession"),
async () => { async () => {
try { try {
const isDev = await revokeSession(sessionId);
!isElectron() &&
process.env.NODE_ENV === "development" &&
(window.location.port === "3000" ||
window.location.port === "5173" ||
window.location.port === "" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1");
const apiUrl = isElectron()
? `${(window as { configuredServerUrl?: string }).configuredServerUrl}/users/sessions/${sessionId}`
: isDev
? `http://localhost:30001/users/sessions/${sessionId}`
: `/users/sessions/${sessionId}`;
const response = await fetch(apiUrl, {
method: "DELETE",
credentials: "include",
headers: {
Authorization: `Bearer ${getCookie("jwt")}`,
},
});
if (response.ok) {
toast.success(t("admin.sessionRevokedSuccessfully")); toast.success(t("admin.sessionRevokedSuccessfully"));
if (isCurrentSession) { if (isCurrentSession) {
@@ -645,9 +599,6 @@ export function AdminSettings({
} else { } else {
fetchSessions(); fetchSessions();
} }
} else {
toast.error(t("admin.failedToRevokeSession"));
}
} catch { } catch {
toast.error(t("admin.failedToRevokeSession")); toast.error(t("admin.failedToRevokeSession"));
} }
@@ -663,39 +614,8 @@ export function AdminSettings({
t("admin.confirmRevokeAllSessions"), t("admin.confirmRevokeAllSessions"),
async () => { async () => {
try { try {
const isDev = const data = await revokeAllUserSessions(userId);
!isElectron() && toast.success(data.message || t("admin.sessionsRevokedSuccessfully"));
process.env.NODE_ENV === "development" &&
(window.location.port === "3000" ||
window.location.port === "5173" ||
window.location.port === "" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1");
const apiUrl = isElectron()
? `${(window as { configuredServerUrl?: string }).configuredServerUrl}/users/sessions/revoke-all`
: isDev
? `http://localhost:30001/users/sessions/revoke-all`
: `/users/sessions/revoke-all`;
const response = await fetch(apiUrl, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getCookie("jwt")}`,
},
body: JSON.stringify({
targetUserId: userId,
exceptCurrent: false,
}),
});
if (response.ok) {
const data = await response.json();
toast.success(
data.message || t("admin.sessionsRevokedSuccessfully"),
);
if (isCurrentUser) { if (isCurrentUser) {
setTimeout(() => { setTimeout(() => {
@@ -704,9 +624,6 @@ export function AdminSettings({
} else { } else {
fetchSessions(); fetchSessions();
} }
} else {
toast.error(t("admin.failedToRevokeSessions"));
}
} catch { } catch {
toast.error(t("admin.failedToRevokeSessions")); toast.error(t("admin.failedToRevokeSessions"));
} }

View File

@@ -270,6 +270,7 @@ export function Auth({
"*", "*",
); );
setWebviewAuthSuccess(true); setWebviewAuthSuccess(true);
setTimeout(() => window.location.reload(), 100);
setLoading(false); setLoading(false);
return; return;
} catch (e) {} } catch (e) {}
@@ -446,6 +447,7 @@ export function Auth({
"*", "*",
); );
setWebviewAuthSuccess(true); setWebviewAuthSuccess(true);
setTimeout(() => window.location.reload(), 100);
setTotpLoading(false); setTotpLoading(false);
return; return;
} catch (e) {} } catch (e) {}
@@ -553,6 +555,7 @@ export function Auth({
"*", "*",
); );
setWebviewAuthSuccess(true); setWebviewAuthSuccess(true);
setTimeout(() => window.location.reload(), 100);
setOidcLoading(false); setOidcLoading(false);
return; return;
} catch (e) {} } catch (e) {}

View File

@@ -3,23 +3,7 @@ import { Button } from "@/components/ui/button.tsx";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert.tsx"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert.tsx";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { AlertCircle, Loader2, ArrowLeft, RefreshCw } from "lucide-react"; import { AlertCircle, Loader2, ArrowLeft, RefreshCw } from "lucide-react";
import { getCookie, getUserInfo } from "@/ui/main-axios.ts"; import { getCookie } from "@/ui/main-axios.ts";
declare global {
namespace JSX {
interface IntrinsicElements {
webview: React.DetailedHTMLProps<
React.HTMLAttributes<HTMLElement>,
HTMLElement
> & {
src?: string;
partition?: string;
allowpopups?: string;
ref?: React.Ref<any>;
};
}
}
}
interface ElectronLoginFormProps { interface ElectronLoginFormProps {
serverUrl: string; serverUrl: string;
@@ -36,12 +20,10 @@ export function ElectronLoginForm({
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [isAuthenticating, setIsAuthenticating] = useState(false); const [isAuthenticating, setIsAuthenticating] = useState(false);
const webviewRef = useRef<any>(null); const iframeRef = useRef<HTMLIFrameElement>(null);
const hasAuthenticatedRef = useRef(false); const hasAuthenticatedRef = useRef(false);
const [currentUrl, setCurrentUrl] = useState(serverUrl); const [currentUrl, setCurrentUrl] = useState(serverUrl);
const hasLoadedOnce = useRef(false); const hasLoadedOnce = useRef(false);
const urlCheckInterval = useRef<NodeJS.Timeout | null>(null);
const loadTimeout = useRef<NodeJS.Timeout | null>(null);
useEffect(() => { useEffect(() => {
const handleMessage = async (event: MessageEvent) => { const handleMessage = async (event: MessageEvent) => {
@@ -57,7 +39,8 @@ export function ElectronLoginForm({
if ( if (
data.type === "AUTH_SUCCESS" && data.type === "AUTH_SUCCESS" &&
data.token && data.token &&
!hasAuthenticatedRef.current !hasAuthenticatedRef.current &&
!isAuthenticating
) { ) {
hasAuthenticatedRef.current = true; hasAuthenticatedRef.current = true;
setIsAuthenticating(true); setIsAuthenticating(true);
@@ -70,32 +53,11 @@ export function ElectronLoginForm({
throw new Error("Failed to save JWT to localStorage"); throw new Error("Failed to save JWT to localStorage");
} }
try {
await getUserInfo();
} catch (verifyErr) {
localStorage.removeItem("jwt");
const errorMsg =
verifyErr instanceof Error
? verifyErr.message
: "Failed to verify authentication";
console.error("Authentication verification failed:", verifyErr);
throw new Error(
errorMsg.includes("registration") ||
errorMsg.includes("allowed")
? "Authentication failed. Please check your server connection and try again."
: errorMsg,
);
}
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
onAuthSuccess(); onAuthSuccess();
} catch (err) { } catch (err) {
const errorMessage = setError(t("errors.authTokenSaveFailed"));
err instanceof Error
? err.message
: t("errors.authTokenSaveFailed");
setError(errorMessage);
setIsAuthenticating(false); setIsAuthenticating(false);
hasAuthenticatedRef.current = false; hasAuthenticatedRef.current = false;
} }
@@ -109,69 +71,28 @@ export function ElectronLoginForm({
return () => { return () => {
window.removeEventListener("message", handleMessage); window.removeEventListener("message", handleMessage);
}; };
}, [serverUrl, onAuthSuccess, t]); }, [serverUrl, isAuthenticating, onAuthSuccess, t]);
useEffect(() => { useEffect(() => {
const checkWebviewUrl = () => { const iframe = iframeRef.current;
const webview = webviewRef.current; if (!iframe) return;
if (!webview) return;
try {
const webviewUrl = webview.getURL();
if (webviewUrl && webviewUrl !== currentUrl) {
setCurrentUrl(webviewUrl);
}
} catch (e) {}
};
urlCheckInterval.current = setInterval(checkWebviewUrl, 500);
return () => {
if (urlCheckInterval.current) {
clearInterval(urlCheckInterval.current);
urlCheckInterval.current = null;
}
};
}, [currentUrl]);
useEffect(() => {
const webview = webviewRef.current;
if (!webview) return;
loadTimeout.current = setTimeout(() => {
if (!hasLoadedOnce.current && loading) {
setLoading(false);
setError(
"Unable to connect to server. Please check the server URL and try again.",
);
}
}, 15000);
const handleLoad = () => { const handleLoad = () => {
if (loadTimeout.current) {
clearTimeout(loadTimeout.current);
loadTimeout.current = null;
}
setLoading(false); setLoading(false);
hasLoadedOnce.current = true; hasLoadedOnce.current = true;
setError(null); setError(null);
try { try {
const webviewUrl = webview.getURL(); if (iframe.contentWindow) {
setCurrentUrl(webviewUrl || serverUrl); setCurrentUrl(iframe.contentWindow.location.href);
}
} catch (e) { } catch (e) {
setCurrentUrl(serverUrl); setCurrentUrl(serverUrl);
} }
try {
const injectedScript = ` const injectedScript = `
(function() { (function() {
window.IS_ELECTRON = true;
window.IS_ELECTRON_WEBVIEW = true;
if (typeof window.electronAPI === 'undefined') {
window.electronAPI = { isElectron: true };
}
let hasNotified = false; let hasNotified = false;
function postJWTToParent(token, source) { function postJWTToParent(token, source) {
@@ -192,36 +113,6 @@ export function ElectronLoginForm({
} }
} }
function clearAuthData() {
try {
localStorage.removeItem('jwt');
sessionStorage.removeItem('jwt');
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf('=');
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
if (name === 'jwt') {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=' + window.location.hostname;
}
}
} catch (error) {
}
}
window.addEventListener('message', function(event) {
try {
if (event.data && typeof event.data === 'object') {
if (event.data.type === 'CLEAR_AUTH_DATA') {
clearAuthData();
}
}
} catch (error) {
}
});
function checkAuth() { function checkAuth() {
try { try {
const localToken = localStorage.getItem('jwt'); const localToken = localStorage.getItem('jwt');
@@ -289,10 +180,18 @@ export function ElectronLoginForm({
`; `;
try { try {
webview.executeJavaScript(injectedScript); if (iframe.contentWindow) {
} catch (err) { try {
console.error("Failed to inject authentication script:", err); iframe.contentWindow.eval(injectedScript);
} catch (evalError) {
iframe.contentWindow.postMessage(
{ type: "INJECT_SCRIPT", script: injectedScript },
"*",
);
} }
}
} catch (err) {}
} catch (err) {}
}; };
const handleError = () => { const handleError = () => {
@@ -302,27 +201,18 @@ export function ElectronLoginForm({
} }
}; };
webview.addEventListener("did-finish-load", handleLoad); iframe.addEventListener("load", handleLoad);
webview.addEventListener("did-fail-load", handleError); iframe.addEventListener("error", handleError);
return () => { return () => {
webview.removeEventListener("did-finish-load", handleLoad); iframe.removeEventListener("load", handleLoad);
webview.removeEventListener("did-fail-load", handleError); iframe.removeEventListener("error", handleError);
if (loadTimeout.current) {
clearTimeout(loadTimeout.current);
loadTimeout.current = null;
}
}; };
}, [t, loading, serverUrl]); }, [t]);
const handleRefresh = () => { const handleRefresh = () => {
if (webviewRef.current) { if (iframeRef.current) {
if (loadTimeout.current) { iframeRef.current.src = serverUrl;
clearTimeout(loadTimeout.current);
loadTimeout.current = null;
}
webviewRef.current.src = serverUrl;
setLoading(true); setLoading(true);
setError(null); setError(null);
} }
@@ -385,28 +275,14 @@ export function ElectronLoginForm({
</div> </div>
)} )}
{isAuthenticating && (
<div
className="absolute inset-0 flex items-center justify-center bg-dark-bg/80 z-40"
style={{ marginTop: "60px" }}
>
<div className="flex items-center">
<Loader2 className="h-8 w-8 animate-spin text-primary" />
<span className="ml-3 text-muted-foreground">
{t("auth.authenticating")}
</span>
</div>
</div>
)}
<div className="flex-1 overflow-hidden"> <div className="flex-1 overflow-hidden">
<webview <iframe
ref={webviewRef} ref={iframeRef}
src={serverUrl} src={serverUrl}
className="w-full h-full border-0" className="w-full h-full border-0"
partition="persist:termix" title="Server Authentication"
allowpopups="false" sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox allow-storage-access-by-user-activation allow-top-navigation allow-top-navigation-by-user-activation allow-modals allow-downloads"
style={{ width: "100%", height: "100%" }} allow="clipboard-read; clipboard-write; cross-origin-isolated; camera; microphone; geolocation"
/> />
</div> </div>
</div> </div>

View File

@@ -342,6 +342,7 @@ function createApiInstance(
import("sonner").then(({ toast }) => { import("sonner").then(({ toast }) => {
toast.warning("Session expired. Please log in again."); toast.warning("Session expired. Please log in again.");
window.location.reload();
}); });
const currentPath = window.location.pathname; const currentPath = window.location.pathname;
@@ -1944,6 +1945,53 @@ export async function getUserList(): Promise<{ users: UserInfo[] }> {
} }
} }
export async function getSessions(): Promise<{
sessions: {
id: string;
userId: string;
username?: string;
deviceType: string;
deviceInfo: string;
createdAt: string;
expiresAt: string;
lastActiveAt: string;
jwtToken: string;
isRevoked?: boolean;
}[];
}> {
try {
const response = await authApi.get("/users/sessions");
return response.data;
} catch (error) {
handleApiError(error, "fetch sessions");
}
}
export async function revokeSession(
sessionId: string,
): Promise<{ success: boolean; message: string }> {
try {
const response = await authApi.delete(`/users/sessions/${sessionId}`);
return response.data;
} catch (error) {
handleApiError(error, "revoke session");
}
}
export async function revokeAllUserSessions(
userId: string,
): Promise<{ success: boolean; message: string }> {
try {
const response = await authApi.post("/users/sessions/revoke-all", {
targetUserId: userId,
exceptCurrent: false,
});
return response.data;
} catch (error) {
handleApiError(error, "revoke all user sessions");
}
}
export async function makeUserAdmin( export async function makeUserAdmin(
username: string, username: string,
): Promise<Record<string, unknown>> { ): Promise<Record<string, unknown>> {

View File

@@ -244,12 +244,6 @@ export function Auth({
setUsername(meRes.username || null); setUsername(meRes.username || null);
setUserId(meRes.userId || null); setUserId(meRes.userId || null);
setDbError(null); setDbError(null);
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null,
});
postJWTToWebView(); postJWTToWebView();
if (isReactNativeWebView()) { if (isReactNativeWebView()) {
@@ -258,6 +252,12 @@ export function Auth({
return; return;
} }
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null,
});
setInternalLoggedIn(true); setInternalLoggedIn(true);
if (tab === "signup") { if (tab === "signup") {
setSignupConfirmPassword(""); setSignupConfirmPassword("");
@@ -417,11 +417,6 @@ export function Auth({
setUserId(res.userId || null); setUserId(res.userId || null);
setDbError(null); setDbError(null);
onAuthSuccess({
isAdmin: !!res.is_admin,
username: res.username || null,
userId: res.userId || null,
});
postJWTToWebView(); postJWTToWebView();
if (isReactNativeWebView()) { if (isReactNativeWebView()) {
@@ -430,6 +425,12 @@ export function Auth({
return; return;
} }
onAuthSuccess({
isAdmin: !!res.is_admin,
username: res.username || null,
userId: res.userId || null,
});
setInternalLoggedIn(true); setInternalLoggedIn(true);
setTotpRequired(false); setTotpRequired(false);
setTotpCode(""); setTotpCode("");
@@ -510,12 +511,6 @@ export function Auth({
setUsername(meRes.username || null); setUsername(meRes.username || null);
setUserId(meRes.userId || null); setUserId(meRes.userId || null);
setDbError(null); setDbError(null);
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null,
});
postJWTToWebView(); postJWTToWebView();
if (isReactNativeWebView()) { if (isReactNativeWebView()) {
@@ -529,6 +524,12 @@ export function Auth({
return; return;
} }
onAuthSuccess({
isAdmin: !!meRes.is_admin,
username: meRes.username || null,
userId: meRes.userId || null,
});
setInternalLoggedIn(true); setInternalLoggedIn(true);
window.history.replaceState( window.history.replaceState(
{}, {},