diff --git a/app/components/LoginForm.tsx b/app/components/LoginForm.tsx new file mode 100644 index 00000000..8d663b20 --- /dev/null +++ b/app/components/LoginForm.tsx @@ -0,0 +1,115 @@ +"use client"; + +import { Label } from "@/components/ui/label"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; +import { SignInOptions, SignInResponse, signIn } from "next-auth/react"; +import { FormEvent, useEffect, useState } from "react"; +import { useRouter, useSearchParams } from "next/navigation"; + +const DEFAULT_HOST = "localhost"; +const DEFAULT_PORT = "6379"; + +export default function LoginForm() { + const router = useRouter(); + const [error, setError] = useState(false); + + const [host, setHost] = useState(DEFAULT_HOST); + const [port, setPort] = useState(DEFAULT_PORT); + const [username, setUsername] = useState(""); + const [password, setPassword] = useState(""); + + const searchParams = useSearchParams(); + + useEffect(() => { + const hostParam = searchParams.get("host"); + const portParam = searchParams.get("port"); + const usernameParam = searchParams.get("username"); + + setHost(decodeURIComponent(hostParam ?? DEFAULT_HOST)); + setPort(decodeURIComponent(portParam ?? DEFAULT_PORT)); + setUsername(decodeURIComponent(usernameParam ?? "")); + }, [searchParams]); + + const onSubmit = (e: FormEvent) => { + e.preventDefault(); + + const params: SignInOptions = { + redirect: false, + host: host ?? DEFAULT_HOST, + port: port ?? DEFAULT_PORT, + }; + if (username) { + params.username = username; + } + if (password) { + params.password = password; + } + + signIn("credentials", params).then((res?: SignInResponse) => { + if (res && res.error) { + setError(true); + } else { + router.push("/graph"); + } + }); + }; + + return ( +
+ ); +} diff --git a/app/layout.tsx b/app/layout.tsx index 262cc365..42454066 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,30 +1,30 @@ -import './globals.css' -import type { Metadata } from 'next' -import { Inter } from 'next/font/google' -import { Toaster } from '@/components/ui/toaster' -import { cn } from '@/lib/utils' -import NextAuthProvider from './providers' +import "./globals.css"; +import type { Metadata } from "next"; +import { Inter } from "next/font/google"; +import { Toaster } from "@/components/ui/toaster"; +import { cn } from "@/lib/utils"; +import NextAuthProvider from "./providers"; -const inter = Inter({ subsets: ['latin'] }) +const inter = Inter({ subsets: ["latin"] }); export const metadata: Metadata = { - title: 'FalkorDB Browser', - description: 'FalkorDB Browser is a web-based UI for FalkorDB.', -} + title: "FalkorDB Browser", + description: "FalkorDB Browser is a web-based UI for FalkorDB.", +}; export default function RootLayout({ children, }: { - children: React.ReactNode + children: React.ReactNode; }) { - // Setting suppressHydrationWarning on html tag to prevent warning + // Setting suppressHydrationWarning on html tag to prevent warning // caused by mismatched client/server content caused by next-themes return ( -