Spaces:
Running
Running
File size: 6,582 Bytes
b42dfef |
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 |
'use client';
import { useState, useEffect } from 'react';
import {
initializeOAuth,
loginWithHuggingFace,
loginDevMode,
logout,
getStoredUserInfo,
isAuthenticated,
isDevelopmentMode
} from '@/lib/auth';
import { apiClient } from '@/lib/api';
import type { OAuthUserInfo } from '@/lib/auth';
export default function Header() {
const [userInfo, setUserInfo] = useState<OAuthUserInfo | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [showDevLogin, setShowDevLogin] = useState(false);
const [devUsername, setDevUsername] = useState('');
const isDevMode = isDevelopmentMode();
useEffect(() => {
handleOAuthInit();
}, []);
const handleOAuthInit = async () => {
setIsLoading(true);
try {
const oauthResult = await initializeOAuth();
if (oauthResult) {
setUserInfo(oauthResult.userInfo);
// Set token in API client
apiClient.setToken(oauthResult.accessToken);
} else {
// Check if we have stored user info
const storedUserInfo = getStoredUserInfo();
if (storedUserInfo) {
setUserInfo(storedUserInfo);
}
}
} catch (error) {
console.error('OAuth initialization error:', error);
} finally {
setIsLoading(false);
}
};
const handleLogin = async () => {
try {
await loginWithHuggingFace();
} catch (error) {
console.error('Login failed:', error);
alert('Failed to start login process. Please try again.');
}
};
const handleLogout = () => {
logout();
apiClient.logout();
setUserInfo(null);
// Reload page to clear state
window.location.reload();
};
const handleDevLogin = () => {
if (!devUsername.trim()) {
alert('Please enter a username');
return;
}
try {
const result = loginDevMode(devUsername);
setUserInfo(result.userInfo);
apiClient.setToken(result.accessToken);
setShowDevLogin(false);
setDevUsername('');
} catch (error) {
console.error('Dev login failed:', error);
alert('Failed to login in dev mode');
}
};
return (
<header className="bg-[#28282a] text-white border-b border-[#48484a]">
<div className="flex items-center justify-between px-5 h-14">
<div className="flex items-center space-x-3">
<div className="text-2xl">π</div>
<h1 className="text-base font-semibold text-[#e5e5e7] tracking-tight">AnyCoder</h1>
</div>
<div className="flex items-center space-x-4">
{isLoading ? (
<div className="px-4 py-2">
<span className="text-xs text-[#86868b] font-medium">Loading...</span>
</div>
) : userInfo ? (
<div className="flex items-center space-x-3">
{userInfo.avatarUrl && (
<img
src={userInfo.avatarUrl}
alt={userInfo.name}
className="w-7 h-7 rounded-full ring-2 ring-[#48484a]"
/>
)}
<span className="text-sm text-[#e5e5e7] font-medium">
{userInfo.preferredUsername || userInfo.name}
</span>
<button
onClick={handleLogout}
className="px-4 py-2 bg-[#3a3a3c] text-[#e5e5e7] text-xs rounded-lg hover:bg-[#48484a] transition-all border border-[#48484a] font-semibold shadow-sm active:scale-95"
>
Logout
</button>
</div>
) : (
<div className="flex items-center space-x-3">
{/* Dev Mode Login (only on localhost) */}
{isDevMode && (
<>
{showDevLogin ? (
<div className="flex items-center space-x-2 bg-[#3a3a3c] px-3 py-2 rounded-lg border border-[#ff9f0a] shadow-lg">
<span className="text-xs text-[#ff9f0a] font-semibold">DEV</span>
<input
type="text"
value={devUsername}
onChange={(e) => setDevUsername(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleDevLogin()}
placeholder="username"
className="px-3 py-1.5 rounded-lg text-xs bg-[#2c2c2e] text-[#e5e5e7] border border-[#48484a] focus:outline-none focus:ring-2 focus:ring-[#ff9f0a] focus:border-transparent w-28 font-medium"
autoFocus
/>
<button
onClick={handleDevLogin}
className="px-3 py-1.5 bg-[#ff9f0a] text-white rounded-lg hover:bg-[#ff8800] text-xs font-semibold shadow-sm active:scale-95"
>
OK
</button>
<button
onClick={() => {
setShowDevLogin(false);
setDevUsername('');
}}
className="text-[#86868b] hover:text-[#e5e5e7] text-sm transition-colors"
>
β
</button>
</div>
) : (
<button
onClick={() => setShowDevLogin(true)}
className="px-4 py-2 bg-[#ff9f0a] text-white rounded-lg hover:bg-[#ff8800] transition-all text-xs flex items-center space-x-2 font-semibold shadow-sm active:scale-95"
title="Dev Mode (localhost)"
>
<span>π§</span>
<span>Dev Login</span>
</button>
)}
<span className="text-[#86868b] text-xs font-medium">or</span>
</>
)}
{/* OAuth Login */}
<button
onClick={handleLogin}
className="px-4 py-2 bg-[#007aff] text-white rounded-lg hover:bg-[#0051d5] transition-all text-xs flex items-center space-x-2 font-semibold shadow-md active:scale-95"
>
<span>π€</span>
<span>Sign in</span>
</button>
</div>
)}
</div>
</div>
</header>
);
}
|