Adds public /v1/store/branding and /v1/store/logo endpoints so the login page can display the customer's name and logo without auth, with "Powered by LunarFront" underneath — matching the sidebar style. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
115 lines
4.2 KiB
TypeScript
115 lines
4.2 KiB
TypeScript
import { useState, useEffect } from 'react'
|
|
import { createFileRoute, useRouter, redirect } from '@tanstack/react-router'
|
|
import { useAuthStore } from '@/stores/auth.store'
|
|
import { login } from '@/api/auth'
|
|
|
|
interface Branding {
|
|
name: string | null
|
|
hasLogo: boolean
|
|
}
|
|
|
|
export const Route = createFileRoute('/login')({
|
|
beforeLoad: () => {
|
|
const { token } = useAuthStore.getState()
|
|
if (token) {
|
|
throw redirect({ to: '/accounts', search: { page: 1, limit: 25, q: undefined, sort: undefined, order: 'asc' as const } })
|
|
}
|
|
},
|
|
component: LoginPage,
|
|
})
|
|
|
|
function LoginPage() {
|
|
const router = useRouter()
|
|
const setAuth = useAuthStore((s) => s.setAuth)
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [error, setError] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [branding, setBranding] = useState<Branding | null>(null)
|
|
|
|
useEffect(() => {
|
|
fetch('/v1/store/branding')
|
|
.then((r) => r.ok ? r.json() : null)
|
|
.then((data) => { if (data) setBranding(data) })
|
|
.catch(() => {})
|
|
}, [])
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault()
|
|
setError('')
|
|
setLoading(true)
|
|
|
|
try {
|
|
const res = await login(email, password)
|
|
setAuth(res.token, res.user)
|
|
await router.invalidate()
|
|
await router.navigate({ to: '/accounts', search: { page: 1, limit: 25, q: undefined, sort: undefined, order: 'asc' as const }, replace: true })
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Login failed')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className="flex min-h-screen items-center justify-center"
|
|
style={{ background: 'linear-gradient(135deg, #0f1724 0%, #142038 100%)' }}
|
|
>
|
|
<div
|
|
className="w-full max-w-sm rounded-xl border p-8 shadow-2xl"
|
|
style={{ backgroundColor: '#131c2e', borderColor: '#1e2d45' }}
|
|
>
|
|
<div className="text-center mb-8">
|
|
{branding?.hasLogo ? (
|
|
<img src="/v1/store/logo" alt={branding.name ?? 'Store'} className="max-h-14 max-w-[220px] object-contain mx-auto" />
|
|
) : (
|
|
<h1 className="text-3xl font-bold" style={{ color: '#d8dfe9' }}>{branding?.name ?? 'LunarFront'}</h1>
|
|
)}
|
|
{branding?.name ? (
|
|
<p className="text-[10px] mt-2" style={{ color: '#4a5568' }}>Powered by <span style={{ color: '#6b7a8d' }}>LunarFront</span></p>
|
|
) : (
|
|
<p className="text-sm mt-1" style={{ color: '#6b7a8d' }}>Small Business Management</p>
|
|
)}
|
|
</div>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div className="space-y-2">
|
|
<label className="text-sm font-medium" style={{ color: '#b0bec5' }}>Email</label>
|
|
<input
|
|
type="email"
|
|
placeholder="you@example.com"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
className="h-9 w-full rounded-md border px-3 py-1 text-sm outline-none login-input"
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<label className="text-sm font-medium" style={{ color: '#b0bec5' }}>Password</label>
|
|
<input
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
className="h-9 w-full rounded-md border px-3 py-1 text-sm outline-none login-input"
|
|
/>
|
|
</div>
|
|
{error && (
|
|
<p className="text-sm" style={{ color: '#e57373' }}>{error}</p>
|
|
)}
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="h-9 w-full rounded-md border text-sm font-medium transition-colors disabled:opacity-50"
|
|
style={{ backgroundColor: 'transparent', color: '#d0d8e0', borderColor: '#3a4a62' }}
|
|
onMouseEnter={(e) => { (e.target as HTMLElement).style.backgroundColor = '#1e2d45' }}
|
|
onMouseLeave={(e) => { (e.target as HTMLElement).style.backgroundColor = 'transparent' }}
|
|
>
|
|
{loading ? 'Signing in...' : 'Sign in'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|