feat(auth): add production-grade Firebase Authentication setup with secure session cookies, CSRF defense, and UI modal
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
'use client';
|
||||
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import Link from 'next/link';
|
||||
import {
|
||||
User as UserIcon,
|
||||
LogOut,
|
||||
Sparkles,
|
||||
ChevronDown,
|
||||
CheckCircle2,
|
||||
AlertCircle,
|
||||
ExternalLink,
|
||||
} from 'lucide-react';
|
||||
import { useAuth } from '@/lib/firebase/AuthContext';
|
||||
|
||||
export function UserMenu() {
|
||||
const { user, loading, openAuthModal, logout } = useAuth();
|
||||
const [dropdownOpen, setDropdownOpen] = useState(false);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Close dropdown on click outside
|
||||
useEffect(() => {
|
||||
if (!dropdownOpen) return;
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
|
||||
setDropdownOpen(false);
|
||||
}
|
||||
};
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, [dropdownOpen]);
|
||||
|
||||
// Close on Escape key
|
||||
useEffect(() => {
|
||||
if (!dropdownOpen) return;
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape') setDropdownOpen(false);
|
||||
};
|
||||
document.addEventListener('keydown', handleKeyDown);
|
||||
return () => document.removeEventListener('keydown', handleKeyDown);
|
||||
}, [dropdownOpen]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="w-8 h-8 rounded-full bg-slate-200/80 animate-pulse shrink-0" />
|
||||
);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openAuthModal('signin')}
|
||||
className="inline-flex items-center gap-1.5 px-3.5 py-1.5 rounded-full text-xs sm:text-sm font-sans font-semibold text-slate-700 hover:text-slate-950 bg-paper-2/90 hover:bg-paper-hi border border-rule transition-colors cursor-pointer active:scale-[0.98]"
|
||||
aria-label="Sign in to your account"
|
||||
>
|
||||
<UserIcon className="w-3.5 h-3.5 text-accent" />
|
||||
<span>Sign In</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
const initial = (user.displayName || user.email || 'U').charAt(0).toUpperCase();
|
||||
|
||||
return (
|
||||
<div className="relative shrink-0" ref={dropdownRef}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDropdownOpen(!dropdownOpen)}
|
||||
className="inline-flex items-center gap-2 p-1 pr-2 rounded-full border border-rule bg-paper-hi hover:bg-slate-50 transition-colors shadow-2xs cursor-pointer focus:outline-none focus:ring-2 focus:ring-accent/20"
|
||||
aria-expanded={dropdownOpen}
|
||||
aria-haspopup="true"
|
||||
aria-label="User account menu"
|
||||
>
|
||||
{user.photoURL ? (
|
||||
<img
|
||||
src={user.photoURL}
|
||||
alt={user.displayName || 'User profile'}
|
||||
className="w-7 h-7 rounded-full object-cover border border-slate-200"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-7 h-7 rounded-full bg-accent/15 text-accent font-sans font-bold text-xs flex items-center justify-center border border-accent/20">
|
||||
{initial}
|
||||
</div>
|
||||
)}
|
||||
<span className="hidden sm:inline text-xs font-sans font-medium text-slate-800 max-w-[100px] truncate">
|
||||
{user.displayName || user.email?.split('@')[0]}
|
||||
</span>
|
||||
<ChevronDown className={`w-3.5 h-3.5 text-slate-400 transition-transform ${dropdownOpen ? 'rotate-180' : ''}`} />
|
||||
</button>
|
||||
|
||||
{dropdownOpen && (
|
||||
<div
|
||||
className="absolute right-0 mt-2 w-64 rounded-2xl bg-paper-hi border border-rule shadow-2xl p-2 z-50 animate-in fade-in-0 zoom-in-95 duration-150"
|
||||
role="menu"
|
||||
>
|
||||
{/* User Profile Summary */}
|
||||
<div className="p-3 border-b border-rule">
|
||||
<div className="font-semibold text-xs text-slate-900 truncate">
|
||||
{user.displayName || 'Studio Member'}
|
||||
</div>
|
||||
<div className="text-[11px] font-mono text-slate-500 truncate mt-0.5">
|
||||
{user.email}
|
||||
</div>
|
||||
{user.emailVerified ? (
|
||||
<span className="inline-flex items-center gap-1 text-[10px] text-emerald-600 mt-1.5 font-medium">
|
||||
<CheckCircle2 className="w-3 h-3" />
|
||||
<span>Verified Account</span>
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-[10px] text-amber-600 mt-1.5 font-medium">
|
||||
<AlertCircle className="w-3 h-3" />
|
||||
<span>Unverified Email</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Quick Links */}
|
||||
<div className="py-1">
|
||||
<Link
|
||||
href="/tools"
|
||||
onClick={() => setDropdownOpen(false)}
|
||||
className="flex items-center gap-2.5 px-3 py-2 rounded-xl text-xs font-sans text-slate-700 hover:bg-slate-100 hover:text-slate-950 transition-colors"
|
||||
role="menuitem"
|
||||
>
|
||||
<Sparkles className="w-3.5 h-3.5 text-accent" />
|
||||
<span>Studio Workspaces</span>
|
||||
</Link>
|
||||
<Link
|
||||
href="/apps"
|
||||
onClick={() => setDropdownOpen(false)}
|
||||
className="flex items-center gap-2.5 px-3 py-2 rounded-xl text-xs font-sans text-slate-700 hover:bg-slate-100 hover:text-slate-950 transition-colors"
|
||||
role="menuitem"
|
||||
>
|
||||
<ExternalLink className="w-3.5 h-3.5 text-slate-400" />
|
||||
<span>Mobile App Suite</span>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Sign Out */}
|
||||
<div className="pt-1 border-t border-rule">
|
||||
<button
|
||||
type="button"
|
||||
onClick={async () => {
|
||||
setDropdownOpen(false);
|
||||
await logout();
|
||||
}}
|
||||
className="w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-xs font-sans text-rose-600 hover:bg-rose-50 transition-colors cursor-pointer font-medium"
|
||||
role="menuitem"
|
||||
>
|
||||
<LogOut className="w-3.5 h-3.5" />
|
||||
<span>Sign Out</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user