🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
refactor: update authentication components for improved modularity and consistency
Bill Yang8cddf61595a6
diff --git a/client/src/app/invitation/components/login.tsx b/client/src/app/invitation/components/login.tsxindex f13d1c8ed..7f7628d22 100644--- a/client/src/app/invitation/components/login.tsx+++ b/client/src/app/invitation/components/login.tsx@@ -1,19 +1,12 @@"use client";-import { GithubLogo, GoogleLogo } from "@phosphor-icons/react/dist/ssr";-import { AlertCircle } from "lucide-react";import { useState } from "react";-import {- Alert,- AlertDescription,- AlertTitle,-} from "../../../components/ui/alert";-import { Button } from "../../../components/ui/button";-import { Input } from "../../../components/ui/input";-import { Label } from "../../../components/ui/label";import { authClient } from "../../../lib/auth";-import { IS_CLOUD } from "../../../lib/const";import { userStore } from "../../../lib/userStore";+import { AuthInput } from "@/components/auth/AuthInput";+import { AuthButton } from "@/components/auth/AuthButton";+import { AuthError } from "@/components/auth/AuthError";+import { SocialButtons } from "@/components/auth/SocialButtons";interface LoginProps {inviterEmail?: string | null;@@ -55,88 +48,36 @@ export function Login({ inviterEmail, organization }: LoginProps) {}};- const handleSocialSignIn = async (- provider: "google" | "github" | "twitter"- ) => {- try {- await authClient.signIn.social({- provider,- });- } catch (error) {- setError(String(error));- }- };-return (<form onSubmit={handleLogin}><div className="flex flex-col gap-4">- <div className="grid gap-2">- <Label htmlFor="email">Email</Label>- <Input- id="email"- type="email"- placeholder="[email protected]"- required- value={email}- onChange={(e) => setEmail(e.target.value)}- />- </div>- <div className="grid gap-2">- <Label htmlFor="password">Password</Label>- <Input- id="password"- type="password"- required- value={password}- onChange={(e) => setPassword(e.target.value)}- placeholder="••••••••"- />- </div>- <Button- type="submit"- className="w-full"- disabled={isLoading}- variant="success"- >- {isLoading ? "Logging in..." : "Login to Accept Invitation"}- </Button>+ <AuthInput+ id="email"+ label="Email"+ type="email"+ placeholder="[email protected]"+ required+ value={email}+ onChange={(e) => setEmail(e.target.value)}+ />++ <AuthInput+ id="password"+ label="Password"+ type="password"+ placeholder="••••••••"+ required+ value={password}+ onChange={(e) => setPassword(e.target.value)}+ />- {IS_CLOUD && (- <>- <div className="relative flex justify-center text-xs uppercase">- <span className="bg-background px-2 text-muted-foreground">- Or continue with- </span>- </div>+ <AuthButton isLoading={isLoading} loadingText="Logging in...">+ Login to Accept Invitation+ </AuthButton>- <div className="flex flex-col gap-2">- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("google")}- >- <GoogleLogo weight="bold" className="mr-2" />- </Button>- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("github")}- >- <GithubLogo weight="bold" className="mr-2" />- GitHub- </Button>- </div>- </>- )}+ <SocialButtons onError={setError} />- {error && (- <Alert variant="destructive">- <AlertCircle className="h-4 w-4" />- <AlertTitle>Error</AlertTitle>- <AlertDescription>{error}</AlertDescription>- </Alert>- )}+ <AuthError error={error} /></div></form>);diff --git a/client/src/app/invitation/components/signup.tsx b/client/src/app/invitation/components/signup.tsxindex 16422bb0a..8e1b4b4c2 100644--- a/client/src/app/invitation/components/signup.tsx+++ b/client/src/app/invitation/components/signup.tsx@@ -1,19 +1,12 @@"use client";-import { GithubLogo, GoogleLogo } from "@phosphor-icons/react/dist/ssr";-import { AlertCircle } from "lucide-react";import { useState } from "react";-import {- Alert,- AlertDescription,- AlertTitle,-} from "../../../components/ui/alert";-import { Button } from "../../../components/ui/button";-import { Input } from "../../../components/ui/input";-import { Label } from "../../../components/ui/label";import { authClient } from "../../../lib/auth";-import { IS_CLOUD } from "../../../lib/const";import { userStore } from "../../../lib/userStore";+import { AuthInput } from "@/components/auth/AuthInput";+import { AuthButton } from "@/components/auth/AuthButton";+import { AuthError } from "@/components/auth/AuthError";+import { SocialButtons } from "@/components/auth/SocialButtons";interface SignupProps {inviterEmail?: string | null;@@ -57,98 +50,45 @@ export function Signup({ inviterEmail, organization }: SignupProps) {}};- const handleSocialSignIn = async (- provider: "google" | "github" | "twitter"- ) => {- try {- await authClient.signIn.social({- provider,- });- } catch (error) {- setError(String(error));- }- };-return (<form onSubmit={handleSignup}><div className="flex flex-col gap-4">- <div className="grid gap-2">- <Label htmlFor="name">Name</Label>- <Input- id="name"- type="text"- placeholder="Your name"- value={name}- onChange={(e) => setName(e.target.value)}- />- </div>- <div className="grid gap-2">- <Label htmlFor="email">Email</Label>- <Input- id="email"- type="email"- placeholder="[email protected]"- required- value={email}- onChange={(e) => setEmail(e.target.value)}- />- </div>- <div className="grid gap-2">- <Label htmlFor="password">Password</Label>- <Input- id="password"- type="password"- placeholder="••••••••"- required- value={password}- onChange={(e) => setPassword(e.target.value)}- />- </div>- <Button- type="submit"- className="w-full"- disabled={isLoading}- variant="success"- >- {isLoading ? "Creating account..." : "Sign Up to Accept Invitation"}- </Button>+ <AuthInput+ id="name"+ label="Name"+ type="text"+ placeholder="Your name"+ value={name}+ onChange={(e) => setName(e.target.value)}+ />++ <AuthInput+ id="email"+ label="Email"+ type="email"+ placeholder="[email protected]"+ required+ value={email}+ onChange={(e) => setEmail(e.target.value)}+ />++ <AuthInput+ id="password"+ label="Password"+ type="password"+ placeholder="••••••••"+ required+ value={password}+ onChange={(e) => setPassword(e.target.value)}+ />- {IS_CLOUD && (- <>- <div className="relative flex justify-center text-xs uppercase">- <span className="bg-background px-2 text-muted-foreground">- Or continue with- </span>- </div>+ <AuthButton isLoading={isLoading} loadingText="Creating account...">+ Sign Up to Accept Invitation+ </AuthButton>- <div className="flex flex-col gap-2">- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("google")}- >- <GoogleLogo weight="bold" className="mr-2" />- </Button>- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("github")}- >- <GithubLogo weight="bold" className="mr-2" />- GitHub- </Button>- </div>- </>- )}+ <SocialButtons onError={setError} />- {error && (- <Alert variant="destructive">- <AlertCircle className="h-4 w-4" />- <AlertTitle>Error</AlertTitle>- <AlertDescription>{error}</AlertDescription>- </Alert>- )}+ <AuthError error={error} /></div></form>);diff --git a/client/src/app/login/page.tsx b/client/src/app/login/page.tsxindex c08a662a0..7fefe83cd 100644--- a/client/src/app/login/page.tsx+++ b/client/src/app/login/page.tsx@@ -1,21 +1,18 @@"use client";-import { Button } from "@/components/ui/button";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";-import { Input } from "@/components/ui/input";-import { Label } from "@/components/ui/label";-import { GithubLogo, GoogleLogo } from "@phosphor-icons/react/dist/ssr";-import { AlertCircle } from "lucide-react";import Image from "next/image";import Link from "next/link";import { useRouter } from "next/navigation";import { useState } from "react";-import { Alert, AlertDescription, AlertTitle } from "../../components/ui/alert";import { useSetPageTitle } from "../../hooks/useSetPageTitle";import { authClient } from "../../lib/auth";-import { IS_CLOUD } from "../../lib/const";import { userStore } from "../../lib/userStore";import { useConfigs } from "../../lib/configs";+import { AuthInput } from "@/components/auth/AuthInput";+import { AuthButton } from "@/components/auth/AuthButton";+import { AuthError } from "@/components/auth/AuthError";+import { SocialButtons } from "@/components/auth/SocialButtons";export default function Page() {const { configs, isLoading: isLoadingConfigs } = useConfigs();@@ -52,20 +49,8 @@ export default function Page() {setIsLoading(false);};- const handleSocialSignIn = async (- provider: "google" | "github" | "twitter"- ) => {- try {- await authClient.signIn.social({- provider,- });- } catch (error) {- setError(String(error));- }- };-return (- <div className="flex justify-center items-center h-screen w-full">+ <div className="flex justify-center items-center h-screen w-full p-4"><Card className="w-full max-w-sm p-1"><CardHeader><Image src="/rybbit.png" alt="Rybbit" width={32} height={32} />@@ -75,83 +60,34 @@ export default function Page() {</CardHeader><CardContent><form onSubmit={handleSubmit}>- <div className="flex flex-col gap-6">- <div className="grid gap-2">- <Label htmlFor={"email"}>Email</Label>- <Input- id={"email"}- type={"email"}- placeholder={"[email protected]"}- required- value={email}- onChange={(e) => setEmail(e.target.value)}- />- </div>- <div className="grid gap-2">- <div className="flex items-center">- <Label htmlFor="password">Password</Label>- {/* todo: add forgot password */}- {/* <a- href="#"- className="ml-auto inline-block text-sm underline-offset-4 hover:underline"- >- Forgot your password?- </a> */}- </div>- <Input- id="password"- type="password"- required- value={password}- onChange={(e) => setPassword(e.target.value)}- placeholder="••••••••"- />- </div>- <Button- type="submit"- className="w-full"- disabled={isLoading}- variant={"success"}- >- {isLoading ? "Logging in..." : "Login"}- </Button>+ <div className="flex flex-col gap-4">+ <AuthInput+ id="email"+ label="Email"+ type="email"+ placeholder="[email protected]"+ required+ value={email}+ onChange={(e) => setEmail(e.target.value)}+ />- {IS_CLOUD && (- <>- <div className="relative flex justify-center text-xs uppercase">- <span className="bg-background px-2 text-muted-foreground">- Or continue with- </span>- </div>+ <AuthInput+ id="password"+ label="Password"+ type="password"+ placeholder="••••••••"+ required+ value={password}+ onChange={(e) => setPassword(e.target.value)}+ />- <div className="flex flex-col gap-2">- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("google")}- >- <GoogleLogo weight="bold" />- </Button>- <Button- type="button"- variant="outline"- onClick={() => handleSocialSignIn("github")}- >- <GithubLogo weight="bold" />- GitHub- </Button>- </div>- </>- )}+ <AuthButton isLoading={isLoading} loadingText="Logging in...">+ Login+ </AuthButton>- {error && (- <Alert variant="destructive">- <AlertCircle className="h-4 w-4" />- <AlertTitle>Error Logging In</AlertTitle>- <AlertDescription>{error}</AlertDescription>- </Alert>- )}+ <SocialButtons onError={setError} />++ <AuthError error={error} title="Error Logging In" />{(!configs?.disableSignup || !isLoadingConfigs) && (<div className="text-center text-sm">diff --git a/client/src/app/signup/page.tsx b/client/src/app/signup/page.tsxindex f3f35a06a..eae87c5b5 100644--- a/client/src/app/signup/page.tsx+++ b/client/src/app/signup/page.tsx@@ -1,13 +1,15 @@"use client";+import { AuthButton } from "@/components/auth/AuthButton";+import { AuthError } from "@/components/auth/AuthError";+import { AuthInput } from "@/components/auth/AuthInput";+import { SocialButtons } from "@/components/auth/SocialButtons";import { Button } from "@/components/ui/button";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";-import { GithubLogo, GoogleLogo } from "@phosphor-icons/react/dist/ssr";import { motion } from "framer-motion";import {- AlertCircle,AppWindow,ArrowLeft,ArrowRight,@@ -24,7 +26,6 @@ import { Suspense, useEffect, useState } from "react";import { addSite } from "../../api/admin/sites";import { CodeSnippet } from "../../components/CodeSnippet";import { Logo } from "../../components/Logo";-import { Alert, AlertDescription, AlertTitle } from "../../components/ui/alert";import { useSetPageTitle } from "../../hooks/useSetPageTitle";import { authClient } from "../../lib/auth";import { useConfigs } from "../../lib/configs";@@ -217,90 +218,55 @@ export default function SignupPage() {><h2 className="text-2xl font-semibold mb-6">Create your account</h2><div className="space-y-4">- <div className="grid gap-2">- <Label htmlFor="name">Name</Label>- <Input- id="name"- type="text"- placeholder="Your name"- value={name}- onChange={(e) => setName(e.target.value)}- className="h-10 transition-all bg-neutral-800/50 border-neutral-700"- />- </div>- <div className="grid gap-2">- <Label htmlFor="email">Email</Label>- <Input- id="email"- type="email"- placeholder="[email protected]"- required- value={email}- onChange={(e) => setEmail(e.target.value)}- className="h-10 transition-all bg-neutral-800/50 border-neutral-700"- />- </div>- <div className="grid gap-2">- <Label htmlFor="password">Password</Label>- <Input- id="password"- type="password"- placeholder="••••••••"- required- value={password}- onChange={(e) => setPassword(e.target.value)}- className="h-10 transition-all bg-neutral-800/50 border-neutral-700"- />- </div>- <Button+ <AuthInput+ id="name"+ label="Name"+ type="text"+ placeholder="Your name"+ value={name}+ onChange={(e) => setName(e.target.value)}+ />++ <AuthInput+ id="email"+ label="Email"+ type="email"+ placeholder="[email protected]"+ required+ value={email}+ onChange={(e) => setEmail(e.target.value)}+ />++ <AuthInput+ id="password"+ label="Password"+ type="password"+ placeholder="••••••••"+ required+ value={password}+ onChange={(e) => setPassword(e.target.value)}+ />++ <AuthButton+ isLoading={isLoading}+ loadingText="Creating account..."onClick={handleAccountSubmit}- className="w-full mt-6 transition-all duration-300 h-11 bg-emerald-600 hover:bg-emerald-500 text-white"- disabled={isLoading || !email || !password}- variant="success"+ type="button"+ className="mt-6 transition-all duration-300 h-11">- {isLoading ? "Creating account..." : "Continue"}+ Continue<ArrowRight className="ml-2 h-4 w-4" />- </Button>+ </AuthButton>{IS_CLOUD && (- <>- <div className="relative flex justify-center text-xs uppercase">- <span className="bg-background px-2 text-muted-foreground">- Or continue with- </span>- </div>- <div className="grid grid-cols-2 gap-2">- <Button- type="button"- variant="outline"- onClick={() => {- authClient.signIn.social({- provider: "google",- callbackURL: "/signup?step=2",- });- }}- className="transition-all duration-300 hover:bg-muted bg-neutral-800/50 border-neutral-700"- >- <GoogleLogo weight="bold" />- </Button>- <Button- type="button"- variant="outline"- onClick={() => {- authClient.signIn.social({- provider: "github",- callbackURL: "/signup?step=2",- });- }}- className="transition-all duration-300 hover:bg-muted bg-neutral-800/50 border-neutral-700"- >- <GithubLogo weight="bold" />- GitHub- </Button>- </div>- </>+ <SocialButtons+ onError={setError}+ callbackURL="/signup?step=2"+ mode="signup"+ className="grid grid-cols-2 gap-2"+ />)}+<div className="text-center text-sm">Already have an account?{" "}<Link@@ -310,6 +276,8 @@ export default function SignupPage() {Log in</Link></div>++ <AuthError error={error} /></div></motion.div>);@@ -597,13 +565,7 @@ export default function SignupPage() {{/* Right content area */}<div className="p-6 md:p-8 flex-1 h-[600px] flex flex-col backdrop-blur-sm bg-neutral-900/50">- {error && (- <Alert variant="destructive" className="mb-6">- <AlertCircle className="h-4 w-4" />- <AlertTitle>Error</AlertTitle>- <AlertDescription>{error}</AlertDescription>- </Alert>- )}+ {error && <AuthError error={error} />}<div className="flex-1 flex flex-col justify-between"><div className="min-h-[400px]">{renderStepContent()}</div>diff --git a/client/src/components/auth/AuthButton.tsx b/client/src/components/auth/AuthButton.tsxnew file mode 100644index 000000000..83f684dd3--- /dev/null+++ b/client/src/components/auth/AuthButton.tsx@@ -0,0 +1,43 @@+"use client";++import { Button } from "@/components/ui/button";+import { ReactNode } from "react";++interface AuthButtonProps {+ isLoading: boolean;+ loadingText?: string;+ children: ReactNode;+ className?: string;+ onClick?: () => void;+ type?: "button" | "submit" | "reset";+ variant?:+ | "default"+ | "destructive"+ | "outline"+ | "secondary"+ | "ghost"+ | "link"+ | "success";+}++export function AuthButton({+ isLoading,+ loadingText = "Loading...",+ children,+ className = "",+ onClick,+ type = "submit",+ variant = "success",+}: AuthButtonProps) {+ return (+ <Button+ type={type}+ className={`w-full ${className}`}+ disabled={isLoading}+ variant={variant}+ onClick={onClick}+ >+ {isLoading ? loadingText : children}+ </Button>+ );+}diff --git a/client/src/components/auth/AuthError.tsx b/client/src/components/auth/AuthError.tsxnew file mode 100644index 000000000..3a1cb4e77--- /dev/null+++ b/client/src/components/auth/AuthError.tsx@@ -0,0 +1,21 @@+"use client";++import { AlertCircle } from "lucide-react";+import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";++interface AuthErrorProps {+ error?: string;+ title?: string;+}++export function AuthError({ error, title = "Error" }: AuthErrorProps) {+ if (!error) return null;++ return (+ <Alert variant="destructive">+ <AlertCircle className="h-4 w-4" />+ <AlertTitle>{title}</AlertTitle>+ <AlertDescription>{error}</AlertDescription>+ </Alert>+ );+}diff --git a/client/src/components/auth/AuthInput.tsx b/client/src/components/auth/AuthInput.tsxnew file mode 100644index 000000000..cad803623--- /dev/null+++ b/client/src/components/auth/AuthInput.tsx@@ -0,0 +1,41 @@+"use client";++import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";++interface AuthInputProps {+ id: string;+ label: string;+ type: string;+ placeholder: string;+ value: string;+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;+ required?: boolean;+ className?: string;+}++export function AuthInput({+ id,+ label,+ type,+ placeholder,+ value,+ onChange,+ required = false,+ className = "",+}: AuthInputProps) {+ return (+ <div className={`grid gap-2 ${className}`}>+ <Label htmlFor={id}>{label}</Label>+ <Input+ id={id}+ type={type}+ placeholder={placeholder}+ value={value}+ onChange={onChange}+ required={required}+ className="h-10 transition-all bg-neutral-800/50 border-neutral-700"+ />+ </div>+ );+}diff --git a/client/src/components/auth/SocialButtons.tsx b/client/src/components/auth/SocialButtons.tsxnew file mode 100644index 000000000..ad4c090ee--- /dev/null+++ b/client/src/components/auth/SocialButtons.tsx@@ -0,0 +1,64 @@+"use client";++import { Button } from "@/components/ui/button";+import { GithubLogo, GoogleLogo } from "@phosphor-icons/react/dist/ssr";+import { authClient } from "@/lib/auth";+import { IS_CLOUD } from "@/lib/const";++interface SocialButtonsProps {+ onError: (error: string) => void;+ callbackURL?: string;+ mode?: "signin" | "signup";+ className?: string;+}++export function SocialButtons({+ onError,+ callbackURL,+ mode = "signin",+ className = "",+}: SocialButtonsProps) {+ if (!IS_CLOUD) return null;++ const handleSocialAuth = async (+ provider: "google" | "github" | "twitter"+ ) => {+ try {+ await authClient.signIn.social({+ provider,+ ...(callbackURL ? { callbackURL } : {}),+ });+ } catch (error) {+ onError(String(error));+ }+ };++ return (+ <>+ <div className="relative flex justify-center text-xs uppercase">+ <span className="text-muted-foreground">Or continue with</span>+ </div>++ <div className={`flex flex-col gap-2 ${className}`}>+ <Button+ type="button"+ variant="outline"+ onClick={() => handleSocialAuth("google")}+ className="transition-all duration-300 hover:bg-muted bg-neutral-800/50 border-neutral-700"+ >+ <GoogleLogo weight="bold" className="mr-2" />+ </Button>+ <Button+ type="button"+ variant="outline"+ onClick={() => handleSocialAuth("github")}+ className="transition-all duration-300 hover:bg-muted bg-neutral-800/50 border-neutral-700"+ >+ <GithubLogo weight="bold" className="mr-2" />+ GitHub+ </Button>+ </div>+ </>+ );+}