File size: 21,087 Bytes
23b413b | 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | /**
* AuthScreen — Login / Register screen.
*
* Shown when no active user session.
* Design system: matches the main app's neutral dark palette.
*
* Color tokens (aligned with App.tsx / SettingsPanel / AccountMenu):
* Page bg: #050506 (slightly deeper than --bg: hsl(0,0%,4%) = #0a0a0a)
* Card bg: #141414 (matches elevated surfaces: #121212–#181818)
* Input bg: rgba(255,255,255, 0.035) over card = app input style
* Borders: rgba(255,255,255, 0.08) (matches white/10 in app)
* Primary: #2563eb solid (blue-600, matches app buttons)
* Text: white at 95/70/45/30 opacity (matches app hierarchy)
*/
import React, { useEffect, useState } from 'react'
import {
User,
LogIn,
UserPlus,
ArrowRight,
Eye,
EyeOff,
Mail,
AlertCircle,
CheckCircle2,
ChevronRight,
} from 'lucide-react'
import type { AuthUser } from './AuthGate'
import type { RecentUser } from './AuthGate'
import { resolveFileUrl } from '../resolveFileUrl'
interface AuthScreenProps {
backendUrl: string
onAuthenticated: (user: AuthUser, token: string) => void
/** Shown after a smooth logout (e.g. "Signed out as Alice") */
logoutMessage?: string
/** Previously logged-in accounts for quick switch */
recentUsers?: RecentUser[]
}
export default function AuthScreen({
backendUrl,
onAuthenticated,
logoutMessage,
recentUsers,
}: AuthScreenProps) {
const [mode, setMode] = useState<'login' | 'register'>('login')
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [email, setEmail] = useState('')
const [displayName, setDisplayName] = useState('')
const [showPassword, setShowPassword] = useState(false)
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const [showLogoutBanner, setShowLogoutBanner] = useState(!!logoutMessage)
// Auto-dismiss logout banner after 5 seconds
useEffect(() => {
if (showLogoutBanner) {
const timer = setTimeout(() => setShowLogoutBanner(false), 5000)
return () => clearTimeout(timer)
}
}, [showLogoutBanner])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')
setLoading(true)
try {
const endpoint = mode === 'login' ? '/v1/auth/login' : '/v1/auth/register'
const body = mode === 'login'
? { username, password }
: { username, password, email, display_name: displayName || username }
const res = await fetch(`${backendUrl}${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(body),
})
const data = await res.json()
if (!res.ok) {
setError(data.detail || 'Something went wrong')
return
}
if (data.ok && data.token && data.user) {
onAuthenticated(data.user, data.token)
}
} catch (err) {
setError('Could not connect to the backend')
} finally {
setLoading(false)
}
}
/** Quick-login: prefill username from a recent account and focus password */
const handleQuickSelect = (recentUser: RecentUser) => {
setMode('login')
setUsername(recentUser.username)
setPassword('')
setError('')
setShowLogoutBanner(false)
setTimeout(() => {
const pwInput = document.getElementById('auth-password-input')
pwInput?.focus()
}, 50)
}
const filteredRecent = (recentUsers || []).filter(u => u.username !== username)
const showRecentUsers = mode === 'login' && filteredRecent.length > 0
// ── Design tokens (app-consistent) ──────────────────────────────────────────
const T = {
pageBg: '#050506',
cardBg: '#141414',
inputBg: 'rgba(255, 255, 255, 0.035)',
inputBgFocus: 'rgba(255, 255, 255, 0.055)',
border: 'rgba(255, 255, 255, 0.08)',
borderFocus: 'rgba(255, 255, 255, 0.18)',
borderSubtle: 'rgba(255, 255, 255, 0.05)',
// Text
textPrimary: 'rgba(255, 255, 255, 0.95)',
textSecondary: 'rgba(255, 255, 255, 0.70)',
textMuted: 'rgba(255, 255, 255, 0.45)',
textFaint: 'rgba(255, 255, 255, 0.30)',
textPlaceholder: 'rgba(255, 255, 255, 0.22)',
// Accent
blue: '#2563eb',
blueHover: '#3b82f6',
blueSubtle: 'rgba(37, 99, 235, 0.12)',
blueBorder: 'rgba(37, 99, 235, 0.25)',
blueText: 'rgba(147, 197, 253, 0.90)', // light blue for links
// Status
green: 'rgba(34, 197, 94, 0.12)',
greenBorder: 'rgba(34, 197, 94, 0.25)',
greenText: '#86efac',
red: 'rgba(239, 68, 68, 0.10)',
redBorder: 'rgba(239, 68, 68, 0.20)',
redText: '#fca5a5',
// Surfaces
recentBg: '#111111',
tabBg: 'rgba(255, 255, 255, 0.03)',
tabActive: 'rgba(37, 99, 235, 0.15)',
// Misc
icon: 'rgba(255, 255, 255, 0.25)',
font: 'Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif',
}
const inputStyle: React.CSSProperties = {
width: '100%',
padding: '11px 12px 11px 38px',
borderRadius: 10,
border: `1px solid ${T.border}`,
background: T.inputBg,
color: T.textPrimary,
fontSize: 14,
outline: 'none',
boxSizing: 'border-box',
transition: 'border-color 0.2s, background 0.2s',
fontFamily: T.font,
}
const inputStyleNoIcon: React.CSSProperties = {
...inputStyle,
paddingLeft: 12,
}
const focusHandlers = {
onFocus: (e: React.FocusEvent<HTMLInputElement>) => {
e.currentTarget.style.borderColor = T.borderFocus
e.currentTarget.style.background = T.inputBgFocus
},
onBlur: (e: React.FocusEvent<HTMLInputElement>) => {
e.currentTarget.style.borderColor = T.border
e.currentTarget.style.background = T.inputBg
},
}
return (
<div style={{
minHeight: '100vh',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
background: T.pageBg,
fontFamily: T.font,
padding: '20px',
}}>
{/* Subtle decorative glow — behind card, not competing */}
<div style={{
position: 'fixed',
top: '35%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 600,
height: 400,
background: 'radial-gradient(ellipse, rgba(37, 99, 235, 0.04) 0%, transparent 70%)',
pointerEvents: 'none',
zIndex: 0,
}} />
{/* Logo / Title */}
<div style={{ marginBottom: 36, textAlign: 'center', position: 'relative', zIndex: 1 }}>
<h1 style={{
fontSize: 30,
fontWeight: 700,
color: T.textPrimary,
margin: 0,
letterSpacing: '-0.5px',
}}>
HomePilot
</h1>
<p style={{ color: T.textMuted, fontSize: 14, marginTop: 8 }}>
Your AI-Powered Creative Studio
</p>
</div>
{/* Logout success banner */}
{showLogoutBanner && logoutMessage && (
<div style={{
display: 'flex',
alignItems: 'center',
gap: 10,
padding: '12px 20px',
borderRadius: 12,
background: T.green,
border: `1px solid ${T.greenBorder}`,
marginBottom: 20,
fontSize: 14,
color: T.greenText,
maxWidth: 420,
width: '100%',
position: 'relative',
zIndex: 1,
animation: 'fadeIn 300ms ease-out',
}}>
<CheckCircle2 size={18} style={{ flexShrink: 0 }} />
<span>{logoutMessage}</span>
<button
type="button"
onClick={() => setShowLogoutBanner(false)}
style={{
marginLeft: 'auto',
background: 'none',
border: 'none',
color: T.greenText,
cursor: 'pointer',
padding: 2,
fontSize: 18,
lineHeight: 1,
opacity: 0.6,
}}
aria-label="Dismiss"
>
×
</button>
</div>
)}
{/* Recent accounts — quick switch */}
{showRecentUsers && (
<div style={{
width: '100%',
maxWidth: 420,
marginBottom: 12,
background: T.recentBg,
border: `1px solid ${T.borderSubtle}`,
borderRadius: 14,
overflow: 'hidden',
position: 'relative',
zIndex: 1,
}}>
<div style={{
padding: '12px 16px 8px',
fontSize: 11,
fontWeight: 600,
color: T.textFaint,
letterSpacing: '0.5px',
textTransform: 'uppercase',
}}>
Recent Accounts
</div>
{filteredRecent.map((recent) => (
<button
key={recent.username}
type="button"
onClick={() => handleQuickSelect(recent)}
style={{
width: '100%',
display: 'flex',
alignItems: 'center',
gap: 12,
padding: '10px 16px',
border: 'none',
background: 'transparent',
cursor: 'pointer',
transition: 'background 0.15s',
textAlign: 'left',
}}
onMouseEnter={e => (e.currentTarget.style.background = 'rgba(255,255,255,0.04)')}
onMouseLeave={e => (e.currentTarget.style.background = 'transparent')}
>
{/* Avatar */}
{recent.avatar_url ? (
<img
src={resolveFileUrl(recent.avatar_url)}
alt=""
style={{
width: 36,
height: 36,
borderRadius: '50%',
objectFit: 'cover',
border: `2px solid ${T.border}`,
}}
/>
) : (
<div style={{
width: 36,
height: 36,
borderRadius: '50%',
background: T.blue,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: '#fff',
fontSize: 14,
fontWeight: 700,
flexShrink: 0,
}}>
{(recent.display_name || recent.username).charAt(0).toUpperCase()}
</div>
)}
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{
fontSize: 14,
fontWeight: 600,
color: T.textPrimary,
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}}>
{recent.display_name || recent.username}
</div>
<div style={{
fontSize: 12,
color: T.textMuted,
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}}>
@{recent.username}
</div>
</div>
<ChevronRight size={16} style={{ color: T.textFaint, flexShrink: 0 }} />
</button>
))}
</div>
)}
{/* Card — primary interaction surface */}
<div style={{
width: '100%',
maxWidth: 420,
background: T.cardBg,
border: `1px solid ${T.border}`,
borderRadius: 16,
padding: '32px 28px',
position: 'relative',
zIndex: 1,
boxShadow: '0 8px 32px rgba(0, 0, 0, 0.5), 0 1px 4px rgba(0, 0, 0, 0.3)',
}}>
{/* Tab switcher */}
<div style={{
display: 'flex',
gap: 4,
marginBottom: 28,
background: T.tabBg,
borderRadius: 10,
padding: 4,
border: `1px solid ${T.borderSubtle}`,
}}>
{(['login', 'register'] as const).map(m => (
<button
key={m}
onClick={() => { setMode(m); setError('') }}
style={{
flex: 1,
padding: '10px 0',
borderRadius: 8,
border: 'none',
fontSize: 13,
fontWeight: 600,
cursor: 'pointer',
transition: 'all 0.2s',
background: mode === m ? T.tabActive : 'transparent',
color: mode === m ? T.blueText : T.textMuted,
}}
>
{m === 'login' ? (
<span style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6 }}>
<LogIn size={14} /> Sign In
</span>
) : (
<span style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 6 }}>
<UserPlus size={14} /> Create Account
</span>
)}
</button>
))}
</div>
{/* Error */}
{error && (
<div style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '10px 14px',
borderRadius: 10,
background: T.red,
border: `1px solid ${T.redBorder}`,
marginBottom: 16,
fontSize: 13,
color: T.redText,
}}>
<AlertCircle size={14} style={{ flexShrink: 0 }} />
{error}
</div>
)}
<form onSubmit={handleSubmit}>
{/* Username */}
<div style={{ marginBottom: 18 }}>
<label style={{ display: 'block', fontSize: 12, color: T.textMuted, marginBottom: 6, fontWeight: 500 }}>
Username
</label>
<div style={{ position: 'relative' }}>
<User size={16} style={{ position: 'absolute', left: 12, top: 12, color: T.icon }} />
<input
type="text"
value={username}
onChange={e => setUsername(e.target.value)}
placeholder="Enter username"
required
autoFocus={!showRecentUsers}
minLength={2}
maxLength={32}
style={inputStyle}
{...focusHandlers}
/>
</div>
</div>
{/* Display Name (register only) */}
{mode === 'register' && (
<div style={{ marginBottom: 18 }}>
<label style={{ display: 'block', fontSize: 12, color: T.textMuted, marginBottom: 6, fontWeight: 500 }}>
Display Name <span style={{ color: T.textFaint }}>(optional)</span>
</label>
<input
type="text"
value={displayName}
onChange={e => setDisplayName(e.target.value)}
placeholder="How should we call you?"
maxLength={64}
style={inputStyleNoIcon}
{...focusHandlers}
/>
</div>
)}
{/* Password */}
<div style={{ marginBottom: mode === 'register' ? 18 : 24 }}>
<label style={{ display: 'block', fontSize: 12, color: T.textMuted, marginBottom: 6, fontWeight: 500 }}>
Password {mode === 'register' && <span style={{ color: T.textFaint }}>(optional — skip for passwordless)</span>}
</label>
<div style={{ position: 'relative' }}>
<input
id="auth-password-input"
type={showPassword ? 'text' : 'password'}
value={password}
onChange={e => setPassword(e.target.value)}
placeholder={mode === 'register' ? 'Leave empty for no password' : 'Enter password'}
maxLength={128}
style={{
...inputStyleNoIcon,
paddingRight: 40,
}}
{...focusHandlers}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
style={{
position: 'absolute',
right: 8,
top: 8,
background: 'none',
border: 'none',
color: T.icon,
cursor: 'pointer',
padding: 4,
}}
>
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
</div>
</div>
{/* Email (register only) */}
{mode === 'register' && (
<div style={{ marginBottom: 24 }}>
<label style={{ display: 'block', fontSize: 12, color: T.textMuted, marginBottom: 6, fontWeight: 500 }}>
Email <span style={{ color: T.textFaint }}>(optional — for password recovery)</span>
</label>
<div style={{ position: 'relative' }}>
<Mail size={16} style={{ position: 'absolute', left: 12, top: 12, color: T.icon }} />
<input
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
placeholder="your@email.com"
maxLength={256}
style={inputStyle}
{...focusHandlers}
/>
</div>
</div>
)}
{/* Submit — single authority blue */}
<button
type="submit"
disabled={loading || !username.trim()}
style={{
width: '100%',
padding: '12px',
borderRadius: 10,
border: 'none',
background: T.blue,
color: '#fff',
fontSize: 15,
fontWeight: 600,
cursor: loading ? 'wait' : 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: 8,
transition: 'all 0.15s',
opacity: (!username.trim() || loading) ? 0.4 : 1,
fontFamily: T.font,
letterSpacing: '0.01em',
}}
onMouseEnter={e => { if (username.trim() && !loading) e.currentTarget.style.background = T.blueHover }}
onMouseLeave={e => { e.currentTarget.style.background = T.blue }}
>
{loading ? 'Please wait...' : mode === 'login' ? 'Sign In' : 'Create Account'}
{!loading && <ArrowRight size={16} />}
</button>
</form>
{/* Helpful hint when coming from logout */}
{mode === 'login' && logoutMessage && (
<div style={{
marginTop: 16,
padding: '10px 14px',
borderRadius: 10,
background: T.blueSubtle,
border: `1px solid ${T.blueBorder}`,
fontSize: 12,
color: T.textSecondary,
textAlign: 'center',
lineHeight: 1.5,
}}>
Sign in with a different account or{' '}
<button
type="button"
onClick={() => { setMode('register'); setError('') }}
style={{
background: 'none',
border: 'none',
color: T.blueText,
cursor: 'pointer',
fontWeight: 600,
fontSize: 12,
textDecoration: 'underline',
padding: 0,
fontFamily: T.font,
}}
>
create a new account
</button>
</div>
)}
</div>
{/* Footer */}
<p style={{ color: T.textFaint, fontSize: 12, marginTop: 24, position: 'relative', zIndex: 1 }}>
Self-hosted · Private · Open Source
</p>
{/* Keyframe animation for the logout banner */}
<style>{`
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-8px); }
to { opacity: 1; transform: translateY(0); }
}
/* Override browser autofill styling to match dark theme */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
-webkit-box-shadow: 0 0 0 1000px #141414 inset !important;
-webkit-text-fill-color: rgba(255, 255, 255, 0.95) !important;
caret-color: rgba(255, 255, 255, 0.95) !important;
transition: background-color 5000s ease-in-out 0s;
}
/* Placeholder color */
input::placeholder {
color: rgba(255, 255, 255, 0.22) !important;
}
`}</style>
</div>
)
}
|