Skip to content

Commit d1dde26

Browse files
committed
Fix auth loading redirect and stale FileReader preview
- Only redirect to login when session is known to be absent - Ignore FileReader onload after upload auth/storage failure or success
1 parent 6f9bbb8 commit d1dde26

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

apps/cursor/src/components/company/add-company-button.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ export function AddCompanyButton({ redirect }: { redirect?: boolean }) {
3131
// Adding a company requires an authenticated session: the save action is
3232
// auth-guarded and the logo upload hits an auth-only storage policy. Send
3333
// signed-out visitors to sign in instead of into a form that can't succeed.
34-
if (!isAuthenticated) {
34+
if (isAuthenticated === false) {
3535
router.push(`/login?next=${pathname}`);
3636
return;
3737
}
3838

39+
if (isAuthenticated === null) {
40+
return;
41+
}
42+
3943
setAddCompany({ addCompany: true, redirect });
4044
};
4145

apps/cursor/src/components/upload-logo.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export default function UploadLogo({
4040
// Show an optimistic preview while the upload is in flight. It is reverted
4141
// below if the upload fails so the UI never shows an image that wasn't
4242
// actually saved.
43+
let allowReaderPreview = true;
4344
const reader = new FileReader();
4445
reader.onload = (e) => {
46+
if (!allowReaderPreview) return;
4547
setPreview(e.target?.result as string);
4648
};
4749
reader.readAsDataURL(file);
@@ -59,6 +61,7 @@ export default function UploadLogo({
5961

6062
if (!user) {
6163
toast.error("Please sign in to upload an image.");
64+
allowReaderPreview = false;
6265
setPreview(previousPreview);
6366
return;
6467
}
@@ -82,13 +85,15 @@ export default function UploadLogo({
8285
data: { publicUrl },
8386
} = supabase.storage.from("avatars").getPublicUrl(path);
8487

88+
allowReaderPreview = false;
8589
setPreview(publicUrl);
8690
onUpload?.(publicUrl);
8791
} catch (error) {
8892
console.error("Error uploading file:", error);
8993
toast.error(
9094
error instanceof Error ? error.message : "Failed to upload image.",
9195
);
96+
allowReaderPreview = false;
9297
setPreview(previousPreview);
9398
} finally {
9499
setIsUploading(false);

0 commit comments

Comments
 (0)