import { useEffect, useState } from "react"; import { useLocation } from "wouter"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation } from "@tanstack/react-query"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Checkbox } from "@/components/ui/checkbox"; import { insertUserSchema } from "@shared/schema"; import { apiRequest } from "@/lib/queryClient"; import { useToast } from "@/hooks/use-toast"; // Login form schema const loginSchema = z.object({ username: z.string().min(1, "Username is required"), password: z.string().min(1, "Password is required"), rememberMe: z.boolean().optional(), }); type LoginFormValues = z.infer; // Registration form schema const registerSchema = insertUserSchema.extend({ confirmPassword: z.string().min(1, "Password confirmation is required"), acceptTerms: z.boolean().refine(val => val === true, { message: "You must accept the terms and conditions", }), // Define roleId explicitly to match form values roleId: z.number().optional().nullable(), }).refine(data => data.password === data.confirmPassword, { message: "Passwords do not match", path: ["confirmPassword"], }); type RegisterFormValues = z.infer; export default function AuthPage() { const [location, navigate] = useLocation(); const { toast } = useToast(); const [isChecking, setIsChecking] = useState(true); // Check if user is already logged in useEffect(() => { const checkAuth = async () => { try { const response = await fetch('/api/user', { credentials: 'include' }); if (response.ok) { // User is already logged in, redirect to home navigate('/'); } } catch (error) { console.error('Error checking auth status:', error); } finally { setIsChecking(false); } }; checkAuth(); }, [navigate]); // Login mutation const loginMutation = useMutation({ mutationFn: async (credentials: LoginFormValues) => { const res = await apiRequest("POST", "/api/login", credentials); return await res.json(); }, onSuccess: (user) => { toast({ title: "Login successful", description: `Welcome back, ${user.username}!`, }); navigate('/'); }, onError: (error: Error) => { toast({ title: "Login failed", description: error.message, variant: "destructive", }); }, }); // Register mutation - using our simplified endpoint const registerMutation = useMutation({ mutationFn: async (credentials: any) => { try { const res = await apiRequest("POST", "/api/simple-register", credentials); if (!res.ok) { const errorData = await res.json(); throw new Error(errorData.message || "Registration failed"); } return await res.json(); } catch (error) { console.error("Registration error:", error); throw error instanceof Error ? error : new Error("Unknown registration error"); } }, onSuccess: (response) => { toast({ title: "Registration successful", description: response.message || `Welcome, ${response.username}!`, }); // Switch to login tab automatically const loginTab = document.querySelector('[data-state="inactive"][data-value="login"]') as HTMLElement; if (loginTab) loginTab.click(); }, onError: (error: Error) => { toast({ title: "Registration failed", description: error.message, variant: "destructive", }); }, }); // Login form const loginForm = useForm({ resolver: zodResolver(loginSchema), defaultValues: { username: "", password: "", rememberMe: false, }, }); // Register form const registerForm = useForm({ resolver: zodResolver(registerSchema), defaultValues: { username: "", password: "", confirmPassword: "", email: "", fullName: "", acceptTerms: false, roleId: 2, // Assign the default "user" role ID }, }); // Handle login form submission const onLoginSubmit = (data: LoginFormValues) => { const { username, password } = data; loginMutation.mutate({ username, password }); }; // Handle register form submission const onRegisterSubmit = (data: RegisterFormValues) => { // Remove confirmPassword and acceptTerms which aren't part of the API request const { confirmPassword, acceptTerms, ...registerData } = data; registerMutation.mutate(registerData); }; return (

Active Directory Management API

A comprehensive solution for managing your Active Directory resources

Login Register Login to your account Enter your credentials to access the dashboard
( Username )} /> ( Password )} />
( Remember me )} />
Create a new account Fill out the form to register a new account
( Username )} /> ( Full Name )} /> ( Email )} /> ( Password )} /> ( Confirm Password )} /> (
I accept the terms and conditions
)} />

Active Directory Management API

  • Complete CRUD operations for AD resources
  • Comprehensive API with Swagger documentation
  • Flexible filtering and property selection
  • Secure API token management
  • Intuitive and responsive admin interface
); }