fix: Root und 404 leiten unangemeldete User direkt zu /login
Backend Image / build-and-push (push) Successful in 2m52s
Frontend Image / build-and-push (push) Successful in 2m38s

Bisher gingen "/" und unbekannte URLs immer erst zu /dashboard, das
dann bei fehlendem Login zu /login weiterleitete - unnoetiger Umweg.
Beide pruefen jetzt selbst den Auth-Status (gleiches Muster wie
(app)/layout.tsx: waehrend isLoading nichts tun, danach je nach User
direkt zu /dashboard oder /login).
This commit is contained in:
Nick Szaunig committed 2026-08-26 23:21:06 +02:00
1 parent b3157484c4
commit 6776bdfe73
4 files changed
+98 -10

No files matched your search

@@ -0,0 +1,37 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { render } from "@testing-library/react";
import NotFound from "./not-found";
const replaceMock = vi.fn();
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: replaceMock }),
}));
let mockAuth: { user: { id: string } | null; isLoading: boolean };
vi.mock("@/lib/auth/auth-context", () => ({
useAuth: () => mockAuth,
}));
describe("NotFound page", () => {
beforeEach(() => {
replaceMock.mockReset();
});
it("does not redirect while the auth check is still loading", () => {
mockAuth = { user: null, isLoading: true };
render(<NotFound />);
expect(replaceMock).not.toHaveBeenCalled();
});
it("redirects to the dashboard when logged in", () => {
mockAuth = { user: { id: "user-1" }, isLoading: false };
render(<NotFound />);
expect(replaceMock).toHaveBeenCalledWith("/dashboard");
});
it("redirects to login when not logged in", () => {
mockAuth = { user: null, isLoading: false };
render(<NotFound />);
expect(replaceMock).toHaveBeenCalledWith("/login");
});
});
+16 -2
View File
@@ -1,5 +1,19 @@
import { redirect } from "next/navigation";
"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/lib/auth/auth-context";
export default function NotFound() {
redirect("/dashboard");
const router = useRouter();
const { user, isLoading } = useAuth();
useEffect(() => {
if (isLoading) {
return;
}
router.replace(user ? "/dashboard" : "/login");
}, [isLoading, user, router]);
return null;
}
+29 -6
View File
@@ -1,14 +1,37 @@
import { describe, expect, it, vi } from "vitest";
import { redirect } from "next/navigation";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { render } from "@testing-library/react";
import Page from "./page";
const replaceMock = vi.fn();
vi.mock("next/navigation", () => ({
redirect: vi.fn(),
useRouter: () => ({ replace: replaceMock }),
}));
let mockAuth: { user: { id: string } | null; isLoading: boolean };
vi.mock("@/lib/auth/auth-context", () => ({
useAuth: () => mockAuth,
}));
describe("Home page", () => {
it("redirects to the dashboard", () => {
Page();
expect(redirect).toHaveBeenCalledWith("/dashboard");
beforeEach(() => {
replaceMock.mockReset();
});
it("does not redirect while the auth check is still loading", () => {
mockAuth = { user: null, isLoading: true };
render(<Page />);
expect(replaceMock).not.toHaveBeenCalled();
});
it("redirects to the dashboard when logged in", () => {
mockAuth = { user: { id: "user-1" }, isLoading: false };
render(<Page />);
expect(replaceMock).toHaveBeenCalledWith("/dashboard");
});
it("redirects to login when not logged in", () => {
mockAuth = { user: null, isLoading: false };
render(<Page />);
expect(replaceMock).toHaveBeenCalledWith("/login");
});
});
+16 -2
View File
@@ -1,5 +1,19 @@
import { redirect } from "next/navigation";
"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/lib/auth/auth-context";
export default function Home() {
redirect("/dashboard");
const router = useRouter();
const { user, isLoading } = useAuth();
useEffect(() => {
if (isLoading) {
return;
}
router.replace(user ? "/dashboard" : "/login");
}, [isLoading, user, router]);
return null;
}