ui: improve auth UI

This commit is contained in:
Sosokker 2025-03-07 02:25:14 +07:00
parent 0dddff308e
commit 08f852a397
6 changed files with 447 additions and 130 deletions

View File

@ -18,21 +18,21 @@ export default function ForgotPasswordModal() {
<div>
<Dialog>
<DialogTrigger asChild>
<Button className=" whitespace-nowrap flex bg-transparent border-none hover:bg-transparent shadow-none">
<Button className="whitespace-nowrap flex bg-transparent border-none hover:bg-transparent shadow-none">
<h1 className="text-green-600 underline">Forgot password?</h1>
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogContent className="sm:max-w-md dark:bg-slate-800">
<DialogHeader>
<DialogTitle>What&apos;s your email?</DialogTitle>
<DialogDescription>
Please verify your email for us. Once you do, we&apos;ll send instructions to reset your password
Please verify your email for us. Once you do, we&apos;ll send instructions to reset your password.
</DialogDescription>
</DialogHeader>
<div className="flex items-center space-x-2">
<div className="grid flex-1 gap-2">
<Label htmlFor="link" className="sr-only">
Link
Email
</Label>
<Input id="email" type="email" placeholder="your.email@gmail.com" />
</div>

View File

@ -2,8 +2,9 @@ import Image from "next/image";
export function GoogleSigninButton() {
return (
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
<div className="flex items-center justify-center gap-3 w-full py-2 px-4 rounded-full border border-border bg-gray-100 dark:bg-slate-700 hover:bg-gray-200 dark:hover:bg-slate-600 transition-colors cursor-pointer">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
<span className="font-medium text-gray-800 dark:text-gray-100">Sign in with Google</span>
</div>
);
}

View File

@ -2,35 +2,35 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { signInSchema } from "@/schemas/auth.schema";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
import { Button } from "@/components/ui/button";
import ForgotPasswordModal from "./forgot-password-modal";
import Link from "next/link";
import Image from "next/image";
import { GoogleSigninButton } from "./google-oauth";
import { z } from "zod";
import type { z } from "zod";
import { useContext, useState } from "react";
import { useRouter } from "next/navigation";
import { loginUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
import { Eye, EyeOff, Leaf, ArrowRight, AlertCircle, Loader2 } from "lucide-react";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { ThemeToggle } from "@/components/theme-toggle";
export default function SigninPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const router = useRouter();
const session = useContext(SessionContext);
const {
register,
handleSubmit,
formState: { errors },
formState: { errors, isSubmitting },
} = useForm({
resolver: zodResolver(signInSchema),
defaultValues: {
@ -56,68 +56,188 @@ export default function SigninPage() {
router.push("/setup");
} catch (error: any) {
console.error("Error logging in:", error);
setServerError(error.message);
setServerError(error.message || "Invalid email or password. Please try again.");
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-screen bg-gradient-to-br from-green-50 dark:from-gray-900 to-white dark:to-gray-800">
<div className="grid lg:grid-cols-[1fr_1.2fr] h-screen overflow-hidden">
{/* Left side - Image */}
<div className="hidden lg:block relative">
<div className="absolute inset-0 bg-[url('/plant-background.jpeg')] bg-cover bg-center">
<div className="absolute inset-0 bg-gradient-to-br from-green-900/70 to-green-800/40 flex flex-col justify-between p-10">
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<Link href="/" className="flex items-center gap-2 text-white">
<Leaf className="h-6 w-6" />
<span className="font-bold text-xl">ForFarm</span>
</Link>
</div>
<div className="flex justify-center items-center">
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Welcome back.</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
<span className="text-md">New to Forfarm?</span>
<span className="text-green-600">
<Link href="signup" className="underline">
<div className="max-w-md">
<h2 className="text-3xl font-bold text-white mb-4">Grow smarter with ForFarm</h2>
<p className="text-green-100 mb-6">
Join thousands of farmers using our platform to optimize their agricultural operations and increase
yields.
</p>
<div className="flex items-center gap-4">
<div className="flex -space-x-2">
{[1, 2, 3].map((i) => (
<div key={i} className="w-8 h-8 rounded-full border-2 border-green-600 overflow-hidden">
<Image
src={`/placeholder.svg?height=32&width=32`}
alt="User"
width={32}
height={32}
className="bg-green-200"
/>
</div>
))}
</div>
<div className="text-sm text-green-100">
<span className="font-bold">500+</span> farmers already using ForFarm
</div>
</div>
</div>
</div>
</div>
</div>
{/* Right side - Form */}
<div className="flex justify-center items-center p-6">
<div className="w-full max-w-md">
<div className="lg:hidden flex justify-center mb-8">
<Link href="/" className="flex items-center gap-2">
<Image src="/forfarm-logo.png" alt="Forfarm" width={80} height={80} />
</Link>
</div>
<div className="text-center mb-8">
<h1 className="text-3xl font-bold mb-2">Welcome back</h1>
<p className="text-gray-500 dark:text-gray-400">
New to Forfarm?{" "}
<Link href="/auth/signup" className="text-green-600 hover:text-green-700 font-medium">
Sign up
</Link>
</span>
</div>
</p>
</div>
{serverError && (
<Alert variant="destructive" className="mb-6 animate-fadeIn">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{serverError}</AlertDescription>
</Alert>
)}
{/* Sign in form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5">
<div className="space-y-2">
<Label htmlFor="email" className="text-sm font-medium dark:text-gray-300">
Email
</Label>
<div className="relative">
<Input
type="email"
id="email"
placeholder="name@example.com"
className={`h-12 px-4 ${errors.email ? "border-red-500 focus-visible:ring-red-500" : ""}`}
{...register("email")}
/>
</div>
<div className="mt-5">
<Label htmlFor="password">Password</Label>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
{errors.email && (
<p className="text-red-500 text-sm mt-1 flex items-center gap-1">
<AlertCircle className="h-3 w-3" />
{errors.email.message}
</p>
)}
</div>
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Logging in..." : "Log in"}
<div className="space-y-2">
<div className="flex justify-between">
<Label htmlFor="password" className="text-sm font-medium dark:text-gray-300">
Password
</Label>
<ForgotPasswordModal />
</div>
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
id="password"
placeholder="••••••••"
className={`h-12 px-4 ${errors.password ? "border-red-500 focus-visible:ring-red-500" : ""}`}
{...register("password")}
/>
<button
type="button"
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700"
onClick={() => setShowPassword(!showPassword)}>
{showPassword ? <EyeOff className="h-5 w-5" /> : <Eye className="h-5 w-5" />}
</button>
</div>
{errors.password && (
<p className="text-red-500 text-sm mt-1 flex items-center gap-1">
<AlertCircle className="h-3 w-3" />
{errors.password.message}
</p>
)}
</div>
<div className="flex items-center space-x-2">
<Checkbox id="remember" />
<label
htmlFor="remember"
className="text-sm text-gray-500 dark:text-gray-400 cursor-pointer select-none">
Remember me for 30 days
</label>
</div>
<Button
type="submit"
className="w-full h-12 rounded-full font-medium text-base bg-green-600 hover:bg-green-700 transition-all"
disabled={isLoading}>
{isLoading ? (
<span className="flex items-center gap-2">
<Loader2 className="h-4 w-4 animate-spin" />
Logging in...
</span>
) : (
<span className="flex items-center justify-center gap-2">
Log in
<ArrowRight className="h-4 w-4" />
</span>
)}
</Button>
</form>
<div id="signin-footer" className="flex justify-between mt-5">
<div className="flex items-center space-x-2">
<Checkbox id="terms" />
<label htmlFor="terms" className="text-sm font-medium leading-none">
Remember me
</label>
<div className="mt-8">
<div className="relative">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-gray-200 dark:border-gray-700"></div>
</div>
<div className="relative flex justify-center text-sm">
<span className="px-2 bg-gradient-to-br from-green-50 to-white dark:from-gray-900 dark:to-gray-800 text-gray-500">
Or continue with
</span>
</div>
<ForgotPasswordModal />
</div>
<div className="my-5">
<p className="text-sm">Or log in with</p>
<div className="flex flex-col gap-x-5 mt-3">
<div className="mt-6">
<GoogleSigninButton />
</div>
</div>
<p className="text-center text-sm text-gray-500 dark:text-gray-400 mt-8">
By signing in, you agree to our{" "}
<Link href="/terms" className="text-green-600 hover:text-green-700">
Terms of Service
</Link>{" "}
and{" "}
<Link href="/privacy" className="text-green-600 hover:text-green-700">
Privacy Policy
</Link>
</p>
</div>
</div>
</div>

View File

@ -1,12 +0,0 @@
import React from "react";
const WaterDrop = () => {
return (
<div className="relative w-6 h-[400px] overflow-hidden">
{/* Water Drop animation */}
<div className="absolute w-6 h-6 bg-blue-500 rounded-full animate-drop overflow-hidden"></div>
</div>
);
};
export default WaterDrop;

View File

@ -1,28 +1,30 @@
"use client";
import type React from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
import { signUpSchema } from "@/schemas/auth.schema";
import Link from "next/link";
import Image from "next/image";
import { useContext, useState } from "react";
import { z } from "zod";
import type { z } from "zod";
import { useRouter } from "next/navigation";
import { registerUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
import { Eye, EyeOff, Leaf, ArrowRight, AlertCircle, Loader2, Check } from "lucide-react";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Progress } from "@/components/ui/progress";
export default function SignupPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [successMessage, setSuccessMessage] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [passwordStrength, setPasswordStrength] = useState(0);
const router = useRouter();
const session = useContext(SessionContext);
@ -30,7 +32,8 @@ export default function SignupPage() {
const {
register,
handleSubmit,
formState: { errors },
watch,
formState: { errors, isSubmitting },
} = useForm<z.infer<typeof signUpSchema>>({
resolver: zodResolver(signUpSchema),
defaultValues: {
@ -40,6 +43,29 @@ export default function SignupPage() {
},
});
const password = watch("password");
// Calculate password strength based on several criteria
const calculatePasswordStrength = (password: string) => {
if (!password) return 0;
let strength = 0;
// Length check
if (password.length >= 8) strength += 25;
// Contains lowercase
if (/[a-z]/.test(password)) strength += 25;
// Contains uppercase
if (/[A-Z]/.test(password)) strength += 25;
// Contains number or special char
if (/[0-9!@#$%^&*(),.?":{}|<>]/.test(password)) strength += 25;
return strength;
};
const onPasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newStrength = calculatePasswordStrength(e.target.value);
setPasswordStrength(newStrength);
};
const onSubmit = async (values: z.infer<typeof signUpSchema>) => {
setServerError(null);
setSuccessMessage(null);
@ -52,82 +78,264 @@ export default function SignupPage() {
setServerError("An error occurred while registering. Please try again.");
throw new Error("No data received from the server.");
}
session!.setToken(data.token);
session!.setUser(values.email);
setSuccessMessage("Registration successful! You can now sign in.");
router.push("/setup");
} catch (error: any) {
console.error("Error during registration:", error);
setServerError(error.message);
setServerError(error.message || "Registration failed. Please try again.");
} finally {
setIsLoading(false);
}
};
return (
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
const getPasswordStrengthText = () => {
if (passwordStrength === 0) return "";
if (passwordStrength <= 25) return "Weak";
if (passwordStrength <= 50) return "Fair";
if (passwordStrength <= 75) return "Good";
return "Strong";
};
<div className="flex justify-center items-center">
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Hi! Welcome</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
<span className="text-md">Already have an account?</span>
<span className="text-green-600">
<Link href="/auth/signin" className="underline">
const getPasswordStrengthColor = () => {
if (passwordStrength <= 25) return "bg-red-500";
if (passwordStrength <= 50) return "bg-yellow-500";
if (passwordStrength <= 75) return "bg-blue-500";
return "bg-green-500";
};
return (
<div className="min-h-screen bg-gradient-to-br from-green-50 dark:from-gray-900 to-white dark:to-gray-800">
<div className="grid lg:grid-cols-[1fr_1.2fr] h-screen overflow-hidden">
{/* Left Side - Image */}
<div className="hidden lg:block relative">
<div className="absolute inset-0 bg-[url('/plant-background.jpeg')] bg-cover bg-center">
<div className="absolute inset-0 bg-gradient-to-br from-green-900/70 to-green-800/40 flex flex-col justify-between p-10">
<div>
<Link href="/" className="flex items-center gap-2 text-white">
<Leaf className="h-6 w-6" />
<span className="font-bold text-xl">ForFarm</span>
</Link>
</div>
<div className="max-w-md">
<h2 className="text-3xl font-bold text-white mb-4">Join the farming revolution</h2>
<p className="text-green-100 mb-6">
Create your account today and discover how ForFarm can help you optimize your agricultural operations.
</p>
<div className="space-y-4">
{[
"Real-time monitoring of your crops",
"Smart resource management",
"Data-driven insights for better yields",
"Connect with other farmers in your area",
].map((feature, index) => (
<div key={index} className="flex items-start gap-2">
<div className="rounded-full bg-green-500 p-1 mt-0.5">
<Check className="h-3 w-3 text-white" />
</div>
<span className="text-green-100 text-sm">{feature}</span>
</div>
))}
</div>
</div>
</div>
</div>
</div>
{/* Right Side - Form */}
<div className="flex justify-center items-center p-6">
<div className="w-full max-w-md">
{/* Theme Selector Placeholder */}
<div className="mb-4 text-center text-sm text-gray-500 dark:text-gray-400">Theme Selector Placeholder</div>
<div className="lg:hidden flex justify-center mb-8">
<Link href="/" className="flex items-center gap-2">
<Image src="/forfarm-logo.png" alt="Forfarm" width={80} height={80} />
</Link>
</div>
<div className="text-center mb-8">
<h1 className="text-3xl font-bold mb-2">Create your account</h1>
<p className="text-gray-500 dark:text-gray-400">
Already have an account?{" "}
<Link href="/auth/signin" className="text-green-600 hover:text-green-700 font-medium">
Sign in
</Link>
</p>
</div>
{serverError && (
<Alert variant="destructive" className="mb-6 animate-fadeIn">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{serverError}</AlertDescription>
</Alert>
)}
{successMessage && (
<Alert className="mb-6 bg-green-50 text-green-800 border-green-200 animate-fadeIn">
<Check className="h-4 w-4" />
<AlertDescription>{successMessage}</AlertDescription>
</Alert>
)}
{/* Sign Up Form */}
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5">
{/* Email */}
<div className="space-y-2">
<Label htmlFor="email" className="text-sm font-medium dark:text-gray-300">
Email
</Label>
<div className="relative">
<Input
type="email"
id="email"
placeholder="name@example.com"
className={`h-12 px-4 ${errors.email ? "border-red-500 focus-visible:ring-red-500" : ""}`}
{...register("email")}
/>
</div>
{errors.email && (
<p className="text-red-500 text-sm mt-1 flex items-center gap-1">
<AlertCircle className="h-3 w-3" />
{errors.email.message}
</p>
)}
</div>
{/* Password */}
<div className="space-y-2">
<Label htmlFor="password" className="text-sm font-medium dark:text-gray-300">
Password
</Label>
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
id="password"
placeholder="••••••••"
className={`h-12 px-4 ${errors.password ? "border-red-500 focus-visible:ring-red-500" : ""}`}
{...register("password", { onChange: onPasswordChange })}
/>
<button
type="button"
aria-label={showPassword ? "Hide password" : "Show password"}
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700"
onClick={() => setShowPassword(!showPassword)}>
{showPassword ? <EyeOff className="h-5 w-5" /> : <Eye className="h-5 w-5" />}
</button>
</div>
{/* Password Strength Indicator */}
{password && (
<div className="mt-2 space-y-1">
<div className="flex justify-between items-center">
<span className="text-xs text-gray-500 dark:text-gray-400">Password strength</span>
<span
className={`text-xs font-medium ${
passwordStrength <= 25
? "text-red-500"
: passwordStrength <= 50
? "text-yellow-500"
: passwordStrength <= 75
? "text-blue-500"
: "text-green-500"
}`}>
{getPasswordStrengthText()}
</span>
</div>
<Progress value={passwordStrength} className={`${getPasswordStrengthColor()} h-1`} />
</div>
)}
{errors.password && (
<p className="text-red-500 text-sm mt-1 flex items-center gap-1">
<AlertCircle className="h-3 w-3" />
{errors.password.message}
</p>
)}
</div>
{/* Signup form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
</div>
<div className="mt-5">
<Label htmlFor="password">Password</Label>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
</div>
<div className="mt-5">
<Label htmlFor="confirmPassword">Confirm Password</Label>
{/* Confirm Password */}
<div className="space-y-2">
<Label htmlFor="confirmPassword" className="text-sm font-medium dark:text-gray-300">
Confirm Password
</Label>
<div className="relative">
<Input
type="password"
type={showConfirmPassword ? "text" : "password"}
id="confirmPassword"
placeholder="Confirm Password"
placeholder="••••••••"
className={`h-12 px-4 ${errors.confirmPassword ? "border-red-500 focus-visible:ring-red-500" : ""}`}
{...register("confirmPassword")}
/>
{errors.confirmPassword && <p className="text-red-600 text-sm">{errors.confirmPassword.message}</p>}
<button
type="button"
aria-label={showConfirmPassword ? "Hide password" : "Show password"}
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}>
{showConfirmPassword ? <EyeOff className="h-5 w-5" /> : <Eye className="h-5 w-5" />}
</button>
</div>
{errors.confirmPassword && (
<p className="text-red-500 text-sm mt-1 flex items-center gap-1">
<AlertCircle className="h-3 w-3" />
{errors.confirmPassword.message}
</p>
)}
</div>
{serverError && <p className="text-red-600 mt-2 text-sm">{serverError}</p>}
{successMessage && <p className="text-green-600 mt-2 text-sm">{successMessage}</p>}
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Signing up..." : "Sign up"}
<Button
type="submit"
className="w-full h-12 rounded-full font-medium text-base bg-green-600 hover:bg-green-700 transition-all"
disabled={isLoading}>
{isLoading ? (
<span className="flex items-center gap-2">
<Loader2 className="h-4 w-4 animate-spin" />
Creating account...
</span>
) : (
<span className="flex items-center justify-center gap-2">
Create account
<ArrowRight className="h-4 w-4" />
</span>
)}
</Button>
</form>
<div className="my-5">
<p className="text-sm">Or sign up with</p>
<div className="flex flex-col gap-x-5 mt-3">
{/* Google OAuth button or additional providers */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
<div className="mt-8">
<div className="relative">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-gray-200 dark:border-gray-700"></div>
</div>
<div className="relative flex justify-center text-sm">
<span className="px-2 bg-gradient-to-br from-green-50 to-white dark:from-gray-900 dark:to-gray-800 text-gray-500">
Or sign up with
</span>
</div>
</div>
<div className="mt-6">
<Button
variant="outline"
className="w-full h-12 rounded-full border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
<Image src="/google-logo.png" alt="Google Logo" width={20} height={20} className="mr-2" />
Sign up with Google
</Button>
</div>
</div>
<p className="text-center text-sm text-gray-500 dark:text-gray-400 mt-8">
By signing up, you agree to our{" "}
<Link href="/terms" className="text-green-600 hover:text-green-700">
Terms of Service
</Link>{" "}
and{" "}
<Link href="/privacy" className="text-green-600 hover:text-green-700">
Privacy Policy
</Link>
</p>
</div>
</div>
</div>
</div>

View File

@ -93,7 +93,7 @@ export default function Home() {
<Link href="/demo">
<Button
variant="outline"
className="border-white text-white text-md font-bold px-6 py-6 rounded-full hover:bg-white/10 transition-colors w-full sm:w-auto">
className="border-white dark:border-white text-black dark:text-white text-md font-bold px-6 py-6 rounded-full hover:bg-white/10 transition-colors w-full sm:w-auto">
Watch demo
</Button>
</Link>