Spaces:
Configuration error
Configuration error
File size: 12,236 Bytes
e7427b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | import { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Badge } from "@/components/ui/badge";
import { User, Phone, Lock, Shield, Leaf, Users } from "lucide-react";
import { useToast } from "@/hooks/use-toast";
import { userStorage, type UserProfile } from "@/lib/userStorage";
import logoImage from "@assets/ChatGPT Image Jun 6, 2025, 02_19_36 PM.png";
interface AuthProps {
onLogin: (user: UserProfile) => void;
isOpen: boolean;
}
export default function UserAuth({ onLogin, isOpen }: AuthProps) {
const [isLoginMode, setIsLoginMode] = useState(true);
const [name, setName] = useState("");
const [phone, setPhone] = useState("");
const [organization, setOrganization] = useState("");
const [role, setRole] = useState<UserProfile['role']>('engineer');
const [isLoading, setIsLoading] = useState(false);
const [recentUsers, setRecentUsers] = useState<UserProfile[]>([]);
const { toast } = useToast();
useEffect(() => {
loadRecentUsers();
}, []);
const loadRecentUsers = async () => {
try {
const users = await userStorage.getRecentUsers();
setRecentUsers(users.slice(0, 3)); // Show last 3 users
} catch (error) {
console.warn('Could not load recent users:', error);
}
};
const validatePhone = (phoneNumber: string): boolean => {
return userStorage.validatePhoneNumber(phoneNumber);
};
const handleLogin = async () => {
if (!phone.trim()) {
toast({
title: "फोन नंबर आवश्यक / Phone Required",
description: "कृपया अपना फोन नंबर दर्ज करें / Please enter your phone number",
variant: "destructive"
});
return;
}
if (!validatePhone(phone)) {
toast({
title: "अमान्य फोन नंबर / Invalid Phone",
description: "कृपया 10 अंकों का वैध फोन नंबर दर्ज करें / Please enter a valid 10-digit phone number",
variant: "destructive"
});
return;
}
setIsLoading(true);
try {
// Check if user exists
let user = await userStorage.getUserByPhone(phone);
if (user && isLoginMode) {
// Existing user login
const session = userStorage.createSession(user);
toast({
title: `स्वागत है ${user.name} / Welcome ${user.name}`,
description: "सफलतापूर्वक लॉग इन हो गए / Successfully logged in"
});
onLogin(session.user);
} else if (!user && !isLoginMode) {
// New user registration
if (!name.trim()) {
toast({
title: "नाम आवश्यक / Name Required",
description: "कृपया अपना नाम दर्ज करें / Please enter your name",
variant: "destructive"
});
setIsLoading(false);
return;
}
const newUser = await userStorage.createUser({
name: name.trim(),
phone: phone.trim(),
organization: organization.trim() || undefined,
role,
});
const session = userStorage.createSession(newUser);
toast({
title: `खाता बनाया गया / Account Created`,
description: `स्वागत है ${newUser.name} / Welcome ${newUser.name}`
});
onLogin(session.user);
} else if (user && !isLoginMode) {
// User exists but trying to register
toast({
title: "उपयोगकर्ता मौजूद है / User Exists",
description: "यह फोन नंबर पहले से पंजीकृत है / This phone number is already registered",
variant: "destructive"
});
} else {
// User doesn't exist but trying to login
toast({
title: "उपयोगकर्ता नहीं मिला / User Not Found",
description: "कृपया पहले खाता बनाएं / Please create an account first",
variant: "destructive"
});
}
} catch (error) {
toast({
title: "त्रुटि / Error",
description: error instanceof Error ? error.message : "कृपया पुनः प्रयास करें / Please try again",
variant: "destructive"
});
}
setIsLoading(false);
};
const handleQuickLogin = (user: UserProfile) => {
const session = userStorage.createSession(user);
onLogin(session.user);
};
const getRoleDisplay = (role: UserProfile['role']) => {
const roleMap = {
engineer: "इंजीनियर / Engineer",
student: "छात्र / Student",
researcher: "शोधकर्ता / Researcher",
government: "सरकारी / Government",
farmer: "किसान / Farmer",
consultant: "सलाहकार / Consultant"
};
return roleMap[role];
};
const getRoleIcon = (role: UserProfile['role']) => {
const icons = {
engineer: <Shield className="w-4 h-4" />,
student: <User className="w-4 h-4" />,
researcher: <Leaf className="w-4 h-4" />,
government: <Shield className="w-4 h-4" />,
farmer: <Leaf className="w-4 h-4" />,
consultant: <Users className="w-4 h-4" />
};
return icons[role];
};
return (
<Dialog open={isOpen}>
<DialogContent className="max-w-md">
<DialogHeader>
<div className="text-center">
<img
src={logoImage}
alt="Prithvi Guardian AI Logo"
className="w-20 h-20 mx-auto mb-3 rounded-full shadow-md"
/>
<DialogTitle className="text-xl font-bold text-green-800 mb-2">
Prithvi Guardian Ai
</DialogTitle>
</div>
<DialogDescription className="text-center">
पर्यावरण और सिविल इंजीनियरिंग के लिए ऑफलाइन ऐप
<br />
Offline Environmental & Civil Engineering App
</DialogDescription>
</DialogHeader>
<CardContent className="space-y-4">
{/* Recent Users Quick Login */}
{recentUsers.length > 0 && (
<div className="space-y-2">
<Label className="text-sm font-medium">त्वरित लॉगिन / Quick Login</Label>
<div className="space-y-2">
{recentUsers.map((user) => (
<Card
key={user.id}
className="p-3 cursor-pointer hover:bg-gray-50 transition-colors"
onClick={() => handleQuickLogin(user)}
>
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
{getRoleIcon(user.role)}
<div>
<p className="font-medium text-sm">{user.name}</p>
<p className="text-xs text-gray-500">{user.phone}</p>
</div>
</div>
<Badge variant="secondary" className="text-xs">
{getRoleDisplay(user.role).split(' / ')[0]}
</Badge>
</div>
</Card>
))}
</div>
</div>
)}
{/* Login/Register Toggle */}
<div className="flex space-x-2">
<Button
variant={isLoginMode ? "default" : "outline"}
onClick={() => setIsLoginMode(true)}
className="flex-1"
>
लॉग इन / Login
</Button>
<Button
variant={!isLoginMode ? "default" : "outline"}
onClick={() => setIsLoginMode(false)}
className="flex-1"
>
नया खाता / Register
</Button>
</div>
{/* Phone Number Input */}
<div className="space-y-2">
<Label htmlFor="phone">
<Phone className="w-4 h-4 inline mr-2" />
फोन नंबर / Phone Number *
</Label>
<Input
id="phone"
type="tel"
placeholder="9876543210"
value={phone}
onChange={(e) => setPhone(e.target.value)}
maxLength={10}
/>
</div>
{/* Name Input (Registration only) */}
{!isLoginMode && (
<div className="space-y-2">
<Label htmlFor="name">
<User className="w-4 h-4 inline mr-2" />
नाम / Name *
</Label>
<Input
id="name"
type="text"
placeholder="आपका नाम / Your Name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
)}
{/* Organization Input (Registration only) */}
{!isLoginMode && (
<div className="space-y-2">
<Label htmlFor="organization">
संस्था / Organization (Optional)
</Label>
<Input
id="organization"
type="text"
placeholder="कंपनी/कॉलेज का नाम / Company/College Name"
value={organization}
onChange={(e) => setOrganization(e.target.value)}
/>
</div>
)}
{/* Role Selection (Registration only) */}
{!isLoginMode && (
<div className="space-y-2">
<Label>भूमिका / Role</Label>
<div className="grid grid-cols-2 gap-2">
{[
{ key: 'engineer' as const, label: 'इंजीनियर' },
{ key: 'student' as const, label: 'छात्र' },
{ key: 'researcher' as const, label: 'शोधकर्ता' },
{ key: 'government' as const, label: 'सरकारी' },
{ key: 'farmer' as const, label: 'किसान' },
{ key: 'consultant' as const, label: 'सलाहकार' }
].map(({ key, label }) => (
<Button
key={key}
variant={role === key ? "default" : "outline"}
onClick={() => setRole(key)}
className="text-xs py-1"
>
{label}
</Button>
))}
</div>
</div>
)}
{/* Submit Button */}
<Button
onClick={handleLogin}
disabled={isLoading}
className="w-full bg-green-600 hover:bg-green-700"
>
{isLoading ? (
"प्रसंस्करण... / Processing..."
) : isLoginMode ? (
"लॉग इन करें / Login"
) : (
"खाता बनाएं / Create Account"
)}
</Button>
{/* Offline Notice */}
<div className="text-xs text-center text-gray-500 mt-4">
<Lock className="w-3 h-3 inline mr-1" />
पूर्ण ऑफलाइन - कोई डेटा साझा नहीं
<br />
Completely Offline - No Data Shared
</div>
</CardContent>
</DialogContent>
</Dialog>
);
} |