wulf-pulse/components/auth/magic-link-form.tsx
root 9f912aed24 feat: add authentication, user management, and admin features
Added comprehensive authentication and authorization system:

Authentication System:
- Better Auth integration with session management
- Login/logout pages and API routes
- Middleware for route protection
- Auth utilities and client libraries

User Management:
- User list, detail, and invite pages
- User API endpoints (CRUD operations)
- Session management for users
- Profile settings page

Role-Based Access Control:
- Role management pages (list, create, edit)
- Permission system with granular controls
- Role assignment to users
- Role API endpoints

Admin Features:
- Audit log page for tracking system events
- Admin settings page
- Audit service for logging user actions

Additional Features:
- Quotes management pages and components
- SalesBldr API integration
- Email service for notifications

Configuration & Documentation:
- Updated docker-compose.yml
- MCP server configuration (mcp.json)
- CVE-2025-55182 security review documentation
- Standards guide and PRD documents
- Re-enabling authentication documentation

Database Migrations:
- 012: Auth tables (users, sessions, accounts, verifications)
- 013: Role tables (roles, permissions, role_permissions, user_roles)
- 014: Admin settings table

UI Updates:
- Updated dashboard layout
- Enhanced app layout with auth integration
2026-01-31 12:43:14 -05:00

134 lines
3.8 KiB
TypeScript

"use client";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Mail, Loader2, CheckCircle } from "lucide-react";
import { toast } from "sonner";
import { authClient } from "@/lib/auth-client";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
const formSchema = z.object({
email: z.string().email("Please enter a valid email address"),
});
type FormData = z.infer<typeof formSchema>;
export function MagicLinkForm() {
const [isLoading, setIsLoading] = useState(false);
const [emailSent, setEmailSent] = useState(false);
const [sentEmail, setSentEmail] = useState("");
const form = useForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
email: "",
},
});
async function onSubmit(data: FormData) {
setIsLoading(true);
try {
const result = await authClient.signIn.magicLink({
email: data.email,
callbackURL: "/",
});
if (result.error) {
toast.error(result.error.message || "Failed to send magic link");
return;
}
setEmailSent(true);
setSentEmail(data.email);
toast.success("Magic link sent! Check your email.");
} catch (error) {
toast.error("An unexpected error occurred");
console.error("Magic link error:", error);
} finally {
setIsLoading(false);
}
}
if (emailSent) {
return (
<div className="text-center space-y-4 py-4">
<div className="flex justify-center">
<div className="h-16 w-16 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center">
<CheckCircle className="h-8 w-8 text-green-600 dark:text-green-400" />
</div>
</div>
<div className="space-y-2">
<h3 className="text-lg font-semibold">Check your email</h3>
<p className="text-sm text-muted-foreground">
We sent a magic link to <strong>{sentEmail}</strong>
</p>
<p className="text-xs text-muted-foreground">
Click the link in the email to sign in. The link expires in 5 minutes.
</p>
</div>
<Button
variant="ghost"
className="text-sm"
onClick={() => {
setEmailSent(false);
form.reset();
}}
>
Use a different email
</Button>
</div>
);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<div className="relative">
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="you@example.com"
className="pl-10"
{...field}
disabled={isLoading}
/>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Sending magic link...
</>
) : (
<>
<Mail className="mr-2 h-4 w-4" />
Send magic link
</>
)}
</Button>
</form>
</Form>
);
}