fix: Root und 404 leiten unangemeldete User direkt zu /login
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:
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");
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user