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
3 changes: 0 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;

if (isPublicPath(pathname) || isPublicApi(pathname)) {
if (AUTH_PUBLIC_PATHS.has(pathname) && await hasValidSessionMarker(request)) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}

Expand Down
34 changes: 31 additions & 3 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
"use client";

import { FormEvent, useState } from "react";
import { FormEvent, useEffect, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { Bot, LogIn } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useAuthUser } from "@/lib/hooks/use-auth";

export default function LoginPage() {
const router = useRouter();
const { user, isLoading } = useAuthUser();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);

useEffect(() => {
if (isLoading || !user) {
return;
}

const next = new URLSearchParams(window.location.search).get("next");
const fallback = user.role === "admin" ? "/admin/users" : "/";
router.replace(next && next !== "/" ? next : fallback);
router.refresh();
}, [isLoading, router, user]);

function resolvePostLoginPath(role: "admin" | "user"): string {
const next = new URLSearchParams(window.location.search).get("next");
if (next && next !== "/") {
return next;
}
return role === "admin" ? "/admin/users" : "/";
Comment on lines +21 to +37
Comment on lines +26 to +37
}

async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setLoading(true);
Expand All @@ -35,11 +56,18 @@ export default function LoginPage() {
return;
}

const next = new URLSearchParams(window.location.search).get("next") || "/";
router.replace(next);
router.replace(resolvePostLoginPath(data.user?.role === "admin" ? "admin" : "user"));
router.refresh();
}

if (user) {
return (
<main className="flex min-h-screen items-center justify-center bg-background px-4">
<p className="text-sm text-muted-foreground">Redirecting...</p>
</main>
);
}

return (
<main className="flex min-h-screen items-center justify-center bg-background px-4">
<Card className="w-full max-w-md border-border/70 shadow-lg">
Expand Down