updates
This commit is contained in:
1 parent
3fcfce600b
commit
ed1d0e6e53
1 file changed
+66
-4
@@ -24,8 +24,17 @@ import { useCallback, useEffect, useRef, useState } from "react";
|
|||||||
|
|
||||||
type Status = "idle" | "authenticated" | "uploading" | "done";
|
type Status = "idle" | "authenticated" | "uploading" | "done";
|
||||||
|
|
||||||
|
const AUTH_STORAGE_KEY = "filehost_auth";
|
||||||
|
|
||||||
|
type SessionData = {
|
||||||
|
token: string;
|
||||||
|
timestamp: number;
|
||||||
|
};
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const [status, setStatus] = useState<Status>("idle");
|
const [status, setStatus] = useState<Status>("idle");
|
||||||
|
const [session, setSession] = useState<SessionData | null>(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [passwordError, setPasswordError] = useState(false);
|
const [passwordError, setPasswordError] = useState(false);
|
||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
@@ -44,12 +53,58 @@ export default function Home() {
|
|||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
const preUploadStatus = useRef<"idle" | "authenticated">("idle");
|
const preUploadStatus = useRef<"idle" | "authenticated">("idle");
|
||||||
|
|
||||||
|
// Restore session from localStorage on mount
|
||||||
|
useEffect(() => {
|
||||||
|
const stored = localStorage.getItem(AUTH_STORAGE_KEY);
|
||||||
|
if (stored) {
|
||||||
|
try {
|
||||||
|
const sessionData: SessionData = JSON.parse(stored);
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
if (now - sessionData.timestamp < 24 * 60 * 60) {
|
||||||
|
setSession(sessionData);
|
||||||
|
setStatus("authenticated");
|
||||||
|
preUploadStatus.current = "authenticated";
|
||||||
|
// Verify session with server
|
||||||
|
const body = new URLSearchParams({
|
||||||
|
token: sessionData.token,
|
||||||
|
timestamp: sessionData.timestamp.toString(),
|
||||||
|
});
|
||||||
|
fetch("/api/auth/verify", { method: "POST", body }).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
setStatus("idle");
|
||||||
|
setSession(null);
|
||||||
|
preUploadStatus.current = "idle";
|
||||||
|
}
|
||||||
|
setIsLoading(false);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setIsLoading(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Persist session to localStorage when it changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (status === "authenticated" && session) {
|
||||||
|
localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(session));
|
||||||
|
} else if (status === "idle") {
|
||||||
|
localStorage.removeItem(AUTH_STORAGE_KEY);
|
||||||
|
}
|
||||||
|
}, [status, session]);
|
||||||
|
|
||||||
const handleUnlock = async () => {
|
const handleUnlock = async () => {
|
||||||
if (!password.trim()) return;
|
if (!password.trim()) return;
|
||||||
const body = new URLSearchParams({ password });
|
const body = new URLSearchParams({ password });
|
||||||
const res = await fetch("/api/auth", { method: "POST", body });
|
const res = await fetch("/api/auth", { method: "POST", body });
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
setPasswordError(false);
|
setPasswordError(false);
|
||||||
|
setSession({ token: data.token, timestamp: data.timestamp });
|
||||||
setStatus("authenticated");
|
setStatus("authenticated");
|
||||||
} else {
|
} else {
|
||||||
setPasswordError(true);
|
setPasswordError(true);
|
||||||
@@ -78,7 +133,10 @@ export default function Home() {
|
|||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
if (status === "authenticated") {
|
if (status === "authenticated" && session) {
|
||||||
|
formData.append("token", session.token);
|
||||||
|
formData.append("timestamp", session.timestamp.toString());
|
||||||
|
} else if (password) {
|
||||||
formData.append("password", password);
|
formData.append("password", password);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +185,7 @@ export default function Home() {
|
|||||||
xhr.open("POST", "/api/upload");
|
xhr.open("POST", "/api/upload");
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
},
|
},
|
||||||
[status, password],
|
[status, password, session],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleDragOver = useCallback(
|
const handleDragOver = useCallback(
|
||||||
@@ -179,7 +237,7 @@ export default function Home() {
|
|||||||
((status === "uploading" || status === "done") &&
|
((status === "uploading" || status === "done") &&
|
||||||
preUploadStatus.current === "authenticated");
|
preUploadStatus.current === "authenticated");
|
||||||
|
|
||||||
const showAuthRow = status === "idle" || showAuthBadge;
|
const showAuthRow = !isLoading && (status === "idle" || showAuthBadge);
|
||||||
|
|
||||||
const limitLabel =
|
const limitLabel =
|
||||||
isAuthenticated || preUploadStatus.current === "authenticated"
|
isAuthenticated || preUploadStatus.current === "authenticated"
|
||||||
@@ -325,7 +383,11 @@ export default function Home() {
|
|||||||
setResultUrl("");
|
setResultUrl("");
|
||||||
setErrorMessage("");
|
setErrorMessage("");
|
||||||
setUploadedFile(null);
|
setUploadedFile(null);
|
||||||
setStatus(isAuthenticated ? "authenticated" : "idle");
|
setStatus(
|
||||||
|
preUploadStatus.current === "authenticated"
|
||||||
|
? "authenticated"
|
||||||
|
: "idle",
|
||||||
|
);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Upload another
|
Upload another
|
||||||
|
|||||||
Reference in new issue
Block a user