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 ( +
+
+ + setHost(e.target.value)} + value={host} + /> +
+
+ + setPort(e.target.value)} + value={port} + /> +
+
+ + setUsername(e.target.value)} + value={username} + /> +
+
+ + setPassword(e.target.value)} + value={password} + /> +
+
+ +
+ {error && ( +
+

Wrong credentials

+
+ )} +
+ ); +} 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 ( - {children} - + {children} + - ) + ); } diff --git a/app/login/page.tsx b/app/login/page.tsx index 029b410d..7852cd78 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,81 +1,12 @@ -'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, useRef, useState } from "react" -import { useRouter } from "next/navigation" - -const DEFAULT_HOST = 'localhost'; -const DEFAULT_PORT = '6379'; +import { Suspense } from "react"; +import LoginForm from "../components/LoginForm"; export default function Page() { - - const router = useRouter() - const [error, setError] = useState(false) - - const host = useRef(null); - const port = useRef(null); - const username = useRef(null); - const password = useRef(null); - - const onSubmit = (e: FormEvent) => { - e.preventDefault(); - - const params: SignInOptions = { - redirect: false, - host: host.current?.value ?? DEFAULT_HOST, - port: port.current?.value ?? DEFAULT_PORT, - } - if (username.current) { - params.username = username.current.value; - } - if (password.current) { - params.password = password.current.value; - } - - signIn( - "credentials", - params - ).then((res?: SignInResponse) => { - if (res && res.error) { - setError(true) - } else { - router.push('/graph'); - } - - }) - }; - - return ( -
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- -
- {error && -
-

Wrong credentials

-
- } -
-
- ) -} \ No newline at end of file + return ( +
+ + + +
+ ); +}