Files
tfm_ainventory/frontend/components/IdentityCheckOverlay.tsx
Daniel Bedeleanu 1893c4f38b modificari mari
2026-04-15 15:20:45 +03:00

210 lines
10 KiB
TypeScript

'use client';
import { useState, memo, useRef } from 'react';
import { User, Shield, ChevronRight, X, Lock } from 'lucide-react';
import { inventoryApi } from '@/lib/api';
import { toast } from 'react-hot-toast';
interface IdentityCheckOverlayProps {
show: boolean;
users: any[];
onAuthenticated: (user: any) => void;
}
const IdentityCheckOverlay = memo(({ show, users, onAuthenticated }: IdentityCheckOverlayProps) => {
const [selectedUserForLogin, setSelectedUserForLogin] = useState<any | null>(null);
const [isEnterprise, setIsEnterprise] = useState(false);
const enterpriseUserRef = useRef<HTMLInputElement>(null);
const enterprisePassRef = useRef<HTMLInputElement>(null);
const localPassRef = useRef<HTMLInputElement>(null);
if (!show) return null;
const handleLogin = async () => {
let username = "";
let password = "";
if (isEnterprise) {
username = enterpriseUserRef.current?.value || "";
password = enterprisePassRef.current?.value || "";
} else if (selectedUserForLogin) {
username = selectedUserForLogin.username;
password = localPassRef.current?.value || "";
}
if (!username) {
toast.error("Username is required");
return;
}
try {
const user = await inventoryApi.login({
username,
password
});
onAuthenticated(user);
setSelectedUserForLogin(null);
setIsEnterprise(false);
} catch (error) {
toast.error(isEnterprise ? "Login failed. Check credentials or group membership." : "Invalid password");
}
};
const handleSelectUser = async (user: any) => {
if (user.username === 'Admin' || user.id > 1) {
setSelectedUserForLogin(user);
return;
}
onAuthenticated(user);
};
return (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-slate-950/90 backdrop-blur-2xl animate-in fade-in duration-500">
<div className="bg-slate-900/80 border border-slate-800 rounded-[2.5rem] p-8 sm:p-10 max-w-sm w-full shadow-2xl space-y-8 animate-in zoom-in-95 duration-300 relative overflow-hidden">
{/* Subtle accent light */}
<div className="absolute top-0 left-1/2 -translate-x-1/2 w-40 h-1 bg-gradient-to-r from-transparent via-primary/50 to-transparent blur-sm" />
<div className="text-center space-y-3">
<div className="w-20 h-20 bg-primary/10 text-primary rounded-[2rem] flex items-center justify-center mx-auto mb-6 border border-primary/20 shadow-xl shadow-primary/5">
<Shield size={40} className="italic" />
</div>
<h2 className="text-3xl font-black text-white tracking-tight">Protocol Access</h2>
<p className="text-slate-500 text-[10px] font-black uppercase tracking-widest">Select operator profile to initialize</p>
</div>
<div className="grid gap-3.5">
{!selectedUserForLogin && !isEnterprise ? (
<>
{users.map(user => (
<button
key={user.id}
onClick={() => handleSelectUser(user)}
className="bg-slate-800/40 hover:bg-slate-800 border border-slate-800/50 hover:border-primary/40 p-5 rounded-[1.5rem] text-left transition-all group flex items-center justify-between shadow-sm active:scale-[0.98]"
>
<div className="flex items-center gap-4">
<div className="w-10 h-10 rounded-2xl bg-slate-900 border border-slate-800 flex items-center justify-center text-slate-500 group-hover:text-primary transition-all group-hover:scale-110">
{user.role === 'admin' ? <Shield size={18} /> : <User size={18} />}
</div>
<div>
<p className="text-slate-100 font-black text-sm tracking-tight">{user.username}</p>
<p className="text-[9px] text-slate-600 font-black uppercase tracking-widest mt-0.5">{user.role}</p>
</div>
</div>
<div className="p-1.5 rounded-full bg-slate-900/50 text-slate-700 group-hover:text-primary group-hover:bg-primary/10 transition-all">
<ChevronRight size={14} />
</div>
</button>
))}
<div className="pt-4">
<button
onClick={() => setIsEnterprise(true)}
className="w-full flex items-center justify-center gap-3 py-5 rounded-[1.5rem] border border-dashed border-slate-800 text-slate-600 hover:text-primary hover:border-primary/40 hover:bg-primary/5 transition-all font-black text-[10px] uppercase tracking-widest"
>
<Lock size={14} />
Enterprise Directory
</button>
</div>
</>
) : isEnterprise ? (
<div className="space-y-5 animate-in slide-in-from-bottom-5 duration-300">
<div className="flex justify-between items-center px-1">
<p className="text-[10px] font-black text-slate-600 uppercase tracking-widest italic">LDAP Authentication</p>
<button
onClick={() => setIsEnterprise(false)}
className="text-[10px] font-black text-primary hover:underline uppercase tracking-tighter"
>
Profile Swap
</button>
</div>
<div className="space-y-2">
<div className="relative group">
<User className="absolute left-5 top-1/2 -translate-y-1/2 text-slate-600 group-focus-within:text-primary transition-colors" size={18} />
<input
ref={enterpriseUserRef}
type="text"
autoFocus
className="w-full bg-slate-950/50 border border-slate-800/80 focus:border-primary/50 focus:bg-slate-950 rounded-[1.25rem] py-4.5 pl-14 pr-5 text-sm text-slate-100 focus:outline-none transition-all placeholder:text-slate-700 font-mono"
placeholder="DIRECTORY ID"
/>
</div>
</div>
<div className="space-y-2">
<div className="relative group">
<Lock className="absolute left-5 top-1/2 -translate-y-1/2 text-slate-600 group-focus-within:text-primary transition-colors" size={18} />
<input
ref={enterprisePassRef}
type="password"
onKeyDown={(e) => e.key === 'Enter' && handleLogin()}
className="w-full bg-slate-950/50 border border-slate-800/80 focus:border-primary/50 focus:bg-slate-950 rounded-[1.25rem] py-4.5 pl-14 pr-5 text-sm text-slate-100 focus:outline-none transition-all placeholder:text-slate-700 font-mono"
placeholder="••••••••"
/>
</div>
</div>
<button
onClick={handleLogin}
className="w-full bg-primary text-white font-black py-5 rounded-[1.5rem] shadow-2xl shadow-primary/20 hover:shadow-primary/30 active:scale-95 transition-all uppercase text-[10px] tracking-widest border border-primary/20"
>
Initialize Session
</button>
</div>
) : (
<div className="space-y-5 animate-in slide-in-from-bottom-5 duration-300">
<div className="bg-slate-950/50 p-5 rounded-[1.5rem] border border-slate-800 flex items-center justify-between group">
<div className="flex items-center gap-4">
<div className="w-10 h-10 rounded-2xl bg-primary/10 border border-primary/20 flex items-center justify-center text-primary shadow-lg">
<Shield size={20} />
</div>
<div>
<p className="text-[9px] font-black text-slate-600 uppercase tracking-widest">Active Profile</p>
<p className="text-white font-black tracking-tight">{selectedUserForLogin.username}</p>
</div>
</div>
<button
onClick={() => setSelectedUserForLogin(null)}
className="p-2 hover:bg-slate-800 rounded-xl transition-all text-slate-600 hover:text-rose-500"
title="Change User"
>
<X size={18} />
</button>
</div>
<div className="space-y-2">
<div className="relative group">
<Lock className="absolute left-5 top-1/2 -translate-y-1/2 text-slate-600 group-focus-within:text-primary transition-colors" size={18} />
<input
ref={localPassRef}
type="password"
autoFocus
onKeyDown={(e) => e.key === 'Enter' && handleLogin()}
className="w-full bg-slate-950/50 border border-slate-800/80 focus:border-primary/50 focus:bg-slate-950 rounded-[1.25rem] py-4.5 pl-14 pr-5 text-sm text-slate-100 focus:outline-none transition-all placeholder:text-slate-700 font-mono tracking-widest"
placeholder="KEY-PHRASE"
/>
</div>
</div>
<button
onClick={handleLogin}
className="w-full bg-primary text-white font-black py-5 rounded-[1.5rem] shadow-2xl shadow-primary/20 hover:shadow-primary/30 active:scale-95 transition-all uppercase text-[10px] tracking-widest border border-primary/20"
>
Unlock Account
</button>
</div>
)}
</div>
{users.length === 0 && (
<div className="text-center p-8 text-slate-700 animate-pulse font-black text-[10px] uppercase tracking-[0.2em]">
Synchronizing User Tokens...
</div>
)}
</div>
</div>
);
});
export default IdentityCheckOverlay;