Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions app/routes/home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { useRef, useEffect, useCallback, useState } from "react";
import type { PlayerRef, CallbackListener } from "@remotion/player";
import { redirect, useLoaderData, type LoaderFunctionArgs } from "react-router";
import axios from "axios";
import { requireUser } from "~/utils/auth.server";
import {
Play,
Pause,
Expand Down Expand Up @@ -31,7 +34,6 @@ import { Switch } from "~/components/ui/switch";
import { Label } from "~/components/ui/label";
import { Input } from "~/components/ui/input";
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from "~/components/ui/resizable";
import axios from "axios";
import { toast } from "sonner";

// Hooks
Expand Down Expand Up @@ -59,7 +61,15 @@ interface Message {
timestamp: Date;
}


export async function loader({ request }: LoaderFunctionArgs) {
const res = await requireUser(request);
if (res.status !== 200) throw redirect("/login");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Redirecting to /login for any non-200 status code might be misleading if the server returns a 500 error. It is better to only redirect on 401 (Unauthorized) and handle other errors (like 500) by allowing the loader to throw an error that can be caught by an ErrorBoundary.

Suggested change
if (res.status !== 200) throw redirect("/login");
if (res.status === 401) throw redirect("/login");
if (res.status !== 200) throw new Response("Failed to load user data", { status: res.status });

return { user: res.data };
}

export default function TimelineEditor() {
const { user } = useLoaderData<typeof loader>();
const containerRef = useRef<HTMLDivElement>(null);
const playerRef = useRef<PlayerRef>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -338,7 +348,7 @@ export default function TimelineEditor() {
}

const timelineState = getTimelineState();
await axios.put(`/ai/api/api/projects/${encodeURIComponent(id)}`, timelineState, {
await axios.put(`/ai/api/projects/${encodeURIComponent(id)}`, timelineState, {
withCredentials: true,
});

Expand Down Expand Up @@ -756,7 +766,7 @@ export default function TimelineEditor() {
</Button>

<ProfileMenu
user={{ name: "test", email: "test@test.com", image: "https://github.com/shadcn.png" }}
user={{ name: user.name ?? "", email: user.email ?? "", image: user.avatar_url ?? "" }}
starCount={starCount}
onSignOut={() => {}}
/>
Expand Down
2 changes: 1 addition & 1 deletion app/routes/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
if (res.status !== 200) throw redirect("/login");
const { origin } = new URL(request.url);

const projectsRes = await axios.get<{ projects: Project[] }>(`${origin}/ai/api/api/projects`, {
const projectsRes = await axios.get<{ projects: Project[] }>(`${origin}/ai/api/projects`, {
headers: { Cookie: request.headers.get("Cookie") },
});

Expand Down