Compare commits

...

9 Commits

Author SHA1 Message Date
eb9c2b1da1 feat: use register page 2025-05-21 19:27:17 +02:00
af8b347173 feat: finished login page 2025-05-21 19:27:08 +02:00
ba89880f8a feat: finished register page 2025-05-21 19:27:03 +02:00
07e1cbc66f feat: spread input props 2025-05-21 19:26:54 +02:00
aee3306c2b feat: proper button with loading 2025-05-21 19:26:41 +02:00
d2cb426170 feat: font 2025-05-21 19:26:31 +02:00
e9e1414c90 feat: dev proxy 2025-05-21 19:26:26 +02:00
4a71f6c5ee feat: react-hook-form pkg 2025-05-21 19:26:21 +02:00
a8e75d75f0 feat: accept host variable 2025-05-21 19:26:14 +02:00
11 changed files with 503 additions and 67 deletions

View File

@ -1,5 +1,7 @@
PORT=3001
HOST="127.0.0.1"
DATABASE_URL="postgres://<user>:<user>@<host>:<port>/<db>?sslmode=disable"
ADMIN_NAME="admin"

View File

@ -32,12 +32,17 @@ func main() {
user.EnsureAdminUser(ctx, repo)
host := os.Getenv("HOST")
if host == "" {
host = "0.0.0.0"
}
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
server := api.NewAPIServer(fmt.Sprintf(":%s", port), repo)
server := api.NewAPIServer(fmt.Sprintf("127.0.0.1:%s", port), repo)
if err := server.Run(); err != nil {
log.Fatalln("ERR: Failed to start server:", err)
}

17
web/package-lock.json generated
View File

@ -14,6 +14,7 @@
"next-themes": "^0.4.6",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-hook-form": "^7.56.4",
"react-icons": "^5.5.0",
"react-router": "^7.6.0",
"tailwindcss": "^4.1.7"
@ -4062,6 +4063,22 @@
"react": "^19.1.0"
}
},
"node_modules/react-hook-form": {
"version": "7.56.4",
"resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.56.4.tgz",
"integrity": "sha512-Rob7Ftz2vyZ/ZGsQZPaRdIefkgOSrQSPXfqBdvOPwJfoGnjwRJUs7EM7Kc1mcoDv3NOtqBzPGbcMB8CGn9CKgw==",
"license": "MIT",
"engines": {
"node": ">=18.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/react-hook-form"
},
"peerDependencies": {
"react": "^16.8.0 || ^17 || ^18 || ^19"
}
},
"node_modules/react-icons": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/react-icons/-/react-icons-5.5.0.tgz",

View File

@ -16,6 +16,7 @@
"next-themes": "^0.4.6",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-hook-form": "^7.56.4",
"react-icons": "^5.5.0",
"react-router": "^7.6.0",
"tailwindcss": "^4.1.7"

View File

@ -3,17 +3,53 @@ import type { ButtonHTMLAttributes, FC, ReactNode } from "react";
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
children: ReactNode;
className?: string;
loading?: boolean;
}
export const Button: FC<ButtonProps> = ({ children, className, ...props }) => {
export const Button: FC<ButtonProps> = ({
children,
className,
loading,
...props
}) => {
return (
<button
className={`bg-blue-600 text-white py-2 px-4 rounded-md hover:bg-blue-700 transition-colors ${
className={`bg-blue-600 text-white cursor-pointer py-2 px-4 rounded-md hover:bg-blue-700 transition-colors ${
className || ""
}${
loading
? " flex items-center justify-center gap-3 cursor-not-allowed"
: ""
}`}
{...props}
disabled={props.disabled || loading}
>
{children}
{loading ? (
<>
<div role="status">
<svg
aria-hidden="true"
className="w-5 h-5 text-gray-400 animate-spin fill-white"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
<span className="text-gray-300">{children}</span>
</>
) : (
children
)}
</button>
);
};

View File

@ -1,19 +1,12 @@
import type { FC } from "react";
import type { FC, InputHTMLAttributes } from "react";
interface InputProps {
id: string;
type: string;
placeholder?: string;
className?: string;
}
type InputProps = InputHTMLAttributes<HTMLInputElement>;
export const Input: FC<InputProps> = ({ id, type, placeholder, className }) => {
export const Input: FC<InputProps> = ({ className, ...props }) => {
return (
<input
id={id}
type={type}
placeholder={placeholder}
className={`w-full border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 ${
{...props}
className={`w-full border border-gray-300 rounded-md px-3 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 ${
className || ""
}`}
/>

View File

@ -1 +1,7 @@
@import url("https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap");
@import "tailwindcss";
html,
body {
font-family: "Inter", Arial, Helvetica, sans-serif;
}

View File

@ -4,6 +4,7 @@ import App from "./App";
import "./index.css";
import LoginPage from "./pages/Login";
import RegisterPage from "./pages/Register";
const root = document.getElementById("root")!;
@ -18,7 +19,7 @@ const router = createBrowserRouter([
},
{
path: "/register",
element: <div>Hello from /register!</div>,
element: <RegisterPage />,
},
]);

View File

@ -1,68 +1,172 @@
import { Card, CardContent } from "@/components/ui/card";
import { Mail, Lock, LogIn } from "lucide-react";
import { Mail, Lock } from "lucide-react";
import { Link } from "react-router-dom";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import overlay from "@/assets/overlay.jpg";
import { useForm, type SubmitHandler } from "react-hook-form";
import { useCallback, useState } from "react";
interface LoginForm {
email: string;
password: string;
}
export default function LoginPage() {
const {
register,
reset,
handleSubmit,
formState: { errors },
} = useForm<LoginForm>();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState("");
const onSubmit: SubmitHandler<LoginForm> = useCallback(
async (data) => {
console.log({ data });
setLoading(true);
setError("");
setSuccess("");
try {
const response = await fetch("/api/v1/login", {
method: "POST",
body: JSON.stringify({
email: data.email,
password: data.password,
}),
headers: {
"Content-Type": "application/json",
},
});
if (response.status != 200) {
const json = await response.json();
const text = json.error || "Unexpected error happened";
setError(
`Failed to create an account. ${
text[0].toUpperCase() + text.slice(1)
}`
);
} else {
setSuccess("You have successfully logged in");
reset();
}
} catch (err) {
console.log(err);
setError("Failed to create account. Unexpected error happened");
} finally {
setLoading(false);
}
},
[reset]
);
return (
<div
className="relative min-h-screen bg-cover bg-center"
style={{ backgroundImage: overlay }}
className="relative min-h-screen bg-cover bg-center bg-white"
style={{ backgroundImage: `url(${overlay})` }}
>
<div className="absolute inset-0 bg-black bg-opacity-60"></div>
<div className="relative z-10 flex items-center justify-center min-h-screen">
<Card className="sm:w-96 sm:min-w-96 sm:max-w-96 sm:min-h-auto p-3 min-h-screen w-full min-w-full shadow-lg bg-white/90 backdrop-blur-md">
<div className="flex flex-col items-center pt-16 sm:pt-0">
<img
src="/icon.png"
alt="icon"
className="w-16 h-16 mb-4 mt-2 sm:mt-6"
/>
<div className="relative z-10 flex items-center justify-center min-h-screen p-4">
<Card className="w-full max-w-md sm:max-w-full sm:h-full sm:rounded-none p-6 shadow-lg bg-white/90 backdrop-blur-md">
<div className="flex flex-col items-center">
<LogIn className="w-8 h-8 text-gray-700 mb-4" />
<h2 className="text-2xl font-bold mb-6 text-gray-800">Login</h2>
<div className="px-4 sm:mt-4 mt-8">
<h2 className="text-2xl font-bold text-gray-800 text-left w-full">
Sign In
</h2>
<h4 className="text-base mb-3 text-gray-400 text-left">
Enter your credentials to access home services and tools.
</h4>
</div>
{/* <LogIn className="w-8 h-8 text-gray-700 mb-4" /> */}
<CardContent className="w-full space-y-4">
<div>
<label
htmlFor="email"
className="block text-sm font-medium text-gray-700 mb-1"
>
Email
</label>
<div className="relative">
<Mail className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="email"
type="email"
placeholder="you@example.com"
className="pl-10"
/>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="mb-4">
<div className="relative">
<Mail className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="email"
type="email"
placeholder="Email Address"
className="pl-10"
{...register("email", {
required: true,
pattern: {
value: /\S+@\S+\.\S+/,
message: "Invalid email",
},
})}
aria-invalid={errors.email ? "true" : "false"}
/>
</div>
{!!errors.email && (
<p className="text-red-500">
{errors.email.message ?? "Email is required"}
</p>
)}
</div>
</div>
<div>
<label
htmlFor="password"
className="block text-sm font-medium text-gray-700 mb-1"
>
Password
</label>
<div className="relative">
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="password"
type="password"
placeholder="••••••••"
className="pl-10"
/>
<div className="mb-4">
<div className="relative">
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="password"
type="password"
placeholder="Password"
className="pl-10"
{...register("password", {
required: true,
})}
aria-invalid={errors.password ? "true" : "false"}
/>
</div>
{!!errors.password && (
<p className="text-red-500">
{errors.password.message ?? "Password is required"}
</p>
)}
</div>
</div>
<Button className="w-full mt-2">Log In</Button>
<div className="text-sm text-center text-gray-600">
Dont have an account?{" "}
<Link to="/register" className="text-blue-600 hover:underline">
Register
</Link>
</div>
{success.length > 0 && (
<div className="border border-green-400 p-2 rounded bg-green-200 text-sm">
{success}
</div>
)}
{error.length > 0 && (
<div className="border border-red-400 p-2 rounded bg-red-200 text-sm">
{error}
</div>
)}
<Button
className="w-full mt-2 mb-4"
type="submit"
loading={isLoading}
>
Log In
</Button>
<div className="text-sm text-center text-gray-600">
Don't have an account?{" "}
<Link
to="/register"
className="text-blue-600 hover:underline"
>
Register
</Link>
</div>
</form>
</CardContent>
</div>
</Card>

View File

@ -0,0 +1,259 @@
import { Card, CardContent } from "@/components/ui/card";
import { Mail, Lock, User, Phone } from "lucide-react";
import { Link } from "react-router-dom";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import overlay from "@/assets/overlay.jpg";
import { useCallback, useState } from "react";
import { useForm, type SubmitHandler } from "react-hook-form";
interface RegisterForm {
fullName: string;
email: string;
phoneNumber: string;
password: string;
repeatPassword: string;
}
export default function RegisterPage() {
const {
register,
handleSubmit,
reset,
formState: { errors },
} = useForm<RegisterForm>();
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState("");
const onSubmit: SubmitHandler<RegisterForm> = useCallback(
async (data) => {
console.log({ data });
setLoading(true);
setError("");
setSuccess("");
try {
const response = await fetch("/api/v1/register", {
method: "POST",
body: JSON.stringify({
full_name: data.fullName,
email: data.email,
...(data.phoneNumber ? { phone_number: data.phoneNumber } : {}),
password: data.password,
}),
headers: {
"Content-Type": "application/json",
},
});
if (response.status != 200) {
const json = await response.json();
const text = json.error || "Unexpected error happened";
setError(
`Failed to create an account. ${
text[0].toUpperCase() + text.slice(1)
}`
);
} else {
setSuccess(
"Account has been created. You can now log into your new account"
);
reset();
}
} catch (err) {
console.log(err);
setError("Failed to create account. Unexpected error happened");
} finally {
setLoading(false);
}
},
[reset]
);
return (
<div
className="relative min-h-screen bg-cover bg-center bg-white"
style={{ backgroundImage: `url(${overlay})` }}
>
<div className="relative z-10 flex items-center justify-center min-h-screen">
<Card className="sm:w-96 sm:min-w-96 sm:max-w-96 sm:min-h-auto p-3 min-h-screen w-full min-w-full shadow-lg bg-white/90 backdrop-blur-md">
<div className="flex flex-col items-center pt-16 sm:pt-0">
<img
src="/icon.png"
alt="icon"
className="w-16 h-16 mb-4 mt-2 sm:mt-6"
/>
<div className="px-4 sm:mt-4 mt-8">
<h2 className="text-2xl font-bold text-gray-800 text-left w-full">
Sign Up
</h2>
<h4 className="text-base mb-3 text-gray-400 text-left">
Fill up this form to start using homelab services and tools.
</h4>
</div>
<CardContent className="w-full space-y-4">
<form onSubmit={handleSubmit(onSubmit)}>
<div className="mb-4">
<div className="relative">
<User className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="full_name"
type="text"
placeholder="Full Name"
className="pl-10"
{...register("fullName", { required: true })}
aria-invalid={errors.fullName ? "true" : "false"}
/>
</div>
{!!errors.fullName && (
<p className="text-red-600 opacity-70 text-sm">
{errors.fullName.message ?? "Full Name is required"}
</p>
)}
</div>
<div className="mb-4">
<div className="relative">
<Mail className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="email"
type="email"
placeholder="Email Address"
className="pl-10"
{...register("email", {
required: true,
pattern: {
value: /\S+@\S+\.\S+/,
message: "Invalid email",
},
})}
aria-invalid={errors.email ? "true" : "false"}
/>
</div>
{!!errors.email && (
<p className="text-red-600 opacity-70 text-sm">
{errors.email.message ?? "Email is required"}
</p>
)}
</div>
<div className="mb-4">
<div className="relative">
<Phone className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="phone_number"
type="tel"
placeholder="Phone Number"
className="pl-10"
{...register("phoneNumber", {
required: false,
})}
aria-invalid={errors.phoneNumber ? "true" : "false"}
/>
</div>
{!!errors.phoneNumber && (
<p className="text-red-600 opacity-70 text-sm">
{errors.phoneNumber.message ?? "Phone Number is required"}
</p>
)}
</div>
<div className="mb-4">
<div className="relative">
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="password"
type="password"
placeholder="Password"
className="pl-10"
{...register("password", {
required: true,
validate: (password) => {
if (password.length < 8) {
return "Password must be at least 8 characters long";
}
if (!password.match(/[a-zA-Z]+/gi)) {
return "Password must contain characters";
}
if (
password
.split("")
.every((c) => c.toLowerCase() == c)
) {
return "Password should contain at least 1 uppercase character";
}
if (!password.match(/\d+/gi)) {
return "Password should contain at least 1 digit";
}
},
})}
aria-invalid={errors.password ? "true" : "false"}
/>
</div>
{!!errors.password && (
<p className="text-red-600 opacity-70 text-sm">
{errors.password.message ?? "Password is required"}
</p>
)}
</div>
<div className="mb-4">
<div className="relative">
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
id="repeat_password"
type="password"
placeholder="Repeat Password"
className="pl-10"
{...register("repeatPassword", {
required: true,
validate: (repeatPassword, { password }) => {
if (repeatPassword != password) {
return "Password does not match";
}
},
})}
aria-invalid={errors.repeatPassword ? "true" : "false"}
/>
</div>
{!!errors.repeatPassword && (
<p className="text-red-600 opacity-70 text-sm">
{errors.repeatPassword.message ?? "Password is required"}
</p>
)}
</div>
{success.length > 0 && (
<div className="border border-green-400 p-2 rounded bg-green-200 text-sm">
{success}
</div>
)}
{error.length > 0 && (
<div className="border border-red-400 p-2 rounded bg-red-200 text-sm">
{error}
</div>
)}
<Button className="w-full mt-2 mb-4" loading={isLoading}>
Register
</Button>
<div className="text-sm text-center text-gray-600">
Already have an account?{" "}
<Link to="/login" className="text-blue-600 hover:underline">
Login
</Link>
</div>
</form>
</CardContent>
</div>
</Card>
</div>
</div>
);
}

View File

@ -4,8 +4,20 @@ import tailwindcss from "@tailwindcss/vite";
import { resolve } from "path";
// https://vite.dev/config/
export default defineConfig({
export default defineConfig(({ mode }) => ({
plugins: [react(), tailwindcss()],
server:
mode === "development"
? {
proxy: {
"/api": {
target: "http://127.0.0.1:3001",
changeOrigin: true,
secure: false,
},
},
}
: undefined,
build: {
outDir: "../dist",
emptyOutDir: true,
@ -16,4 +28,4 @@ export default defineConfig({
},
},
base: "/",
});
}));