mirror of
https://github.com/Such-Software/smirk-website.git
synced 2026-12-01 12:14:12 +01:00
The v0.3 backend answers GET /users/me/username with {"username": ...};
the legacy backend sent a bare string. The page put the object into state
and rendered it, so /settings threw for every signed-in user.
parseUsernameResponse accepts both shapes and returns only a string or
null. Legacy-only fields on /auth/me are optional. Adds node:test cases,
run with npm test.
405 lines
17 KiB
TypeScript
405 lines
17 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import Image from 'next/image';
|
|
import Link from 'next/link';
|
|
import { OFFICIAL_RELEASES } from '@/lib/desktop-releases';
|
|
import { getChallenge, verifySignature, getMe, getUserCount, type AuthResponse } from '@/lib/api';
|
|
|
|
type Asset = 'btc' | 'ltc' | 'xmr' | 'wow' | 'grin';
|
|
|
|
const COINS: { id: Asset; name: string; icon: string }[] = [
|
|
{ id: 'btc', name: 'Bitcoin', icon: '/coins/bitcoin.svg' },
|
|
{ id: 'ltc', name: 'Litecoin', icon: '/coins/litecoin.svg' },
|
|
{ id: 'xmr', name: 'Monero', icon: '/coins/monero.svg' },
|
|
{ id: 'wow', name: 'Wownero', icon: '/coins/wownero.svg' },
|
|
{ id: 'grin', name: 'Grin', icon: '/coins/grin.svg' },
|
|
];
|
|
|
|
type Step = 'initial' | 'choose-coin' | 'signing' | 'logged-in';
|
|
|
|
export default function Home() {
|
|
const [step, setStep] = useState<Step>('initial');
|
|
const [selectedCoin, setSelectedCoin] = useState<Asset | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [user, setUser] = useState<AuthResponse['user'] | null>(null);
|
|
const [hasExtension, setHasExtension] = useState<boolean | null>(null);
|
|
const [checkingSession, setCheckingSession] = useState(true);
|
|
const [userCount, setUserCount] = useState<number | null>(null);
|
|
|
|
// Check for extension on mount.
|
|
//
|
|
// The Smirk extension injects via `<script type="module">`, which
|
|
// the browser DEFERS until after DOMContentLoaded — so a single
|
|
// immediate-or-500ms check could race past the injection on a busy
|
|
// Next.js page (the cause of an old "Connect" stays-greyed-out
|
|
// bug). Three layers cover all timings:
|
|
//
|
|
// 1. Immediate sync check (fast path: extension already there).
|
|
// 2. Listen for the `smirk-ready` CustomEvent (extension v0.2.5+).
|
|
// 3. Polling fallback up to ~3s, for older extensions + any
|
|
// future browser quirk. Cheap; stops as soon as found.
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
const found = () => {
|
|
if (!cancelled && window.smirk) {
|
|
setHasExtension(true);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
// Layer 1: immediate.
|
|
if (found()) return;
|
|
|
|
// Layer 2: event from the extension (v0.2.5+).
|
|
const onReady = () => found();
|
|
window.addEventListener('smirk-ready', onReady);
|
|
|
|
// Layer 3: polling fallback (100ms intervals up to 3s).
|
|
const poll = setInterval(() => {
|
|
if (found()) clearInterval(poll);
|
|
}, 100);
|
|
|
|
// Final timeout: definitively mark as missing if still not seen.
|
|
const giveUp = setTimeout(() => {
|
|
clearInterval(poll);
|
|
if (!cancelled && !window.smirk) setHasExtension(false);
|
|
}, 3000);
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
window.removeEventListener('smirk-ready', onReady);
|
|
clearInterval(poll);
|
|
clearTimeout(giveUp);
|
|
};
|
|
}, []);
|
|
|
|
// Fetch user count on mount
|
|
useEffect(() => {
|
|
getUserCount()
|
|
.then((data) => setUserCount(data.count))
|
|
.catch(() => {}); // Silently ignore errors
|
|
}, []);
|
|
|
|
const handleConnect = async () => {
|
|
if (!window.smirk) {
|
|
setError('Smirk extension not found');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setError(null);
|
|
// Connect to extension (get public keys)
|
|
await window.smirk.connect();
|
|
setStep('choose-coin');
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Failed to connect');
|
|
}
|
|
};
|
|
|
|
const handleCoinSelect = async (coin: Asset) => {
|
|
if (!window.smirk) return;
|
|
|
|
setSelectedCoin(coin);
|
|
setStep('signing');
|
|
setError(null);
|
|
|
|
try {
|
|
// Get challenge from backend
|
|
const challenge = await getChallenge(window.location.origin);
|
|
|
|
// Sign with the selected coin
|
|
const result = await window.smirk.signMessage(challenge.challenge);
|
|
const sig = result.signatures.find((s) => s.asset === coin);
|
|
if (!sig) {
|
|
throw new Error(`No signature found for ${coin}`);
|
|
}
|
|
|
|
// Verify with backend
|
|
const auth = await verifySignature(challenge.challenge_id, {
|
|
asset: coin,
|
|
signature: sig.signature,
|
|
public_key: sig.publicKey,
|
|
});
|
|
|
|
// Store token and show logged in
|
|
localStorage.setItem('smirk_token', auth.access_token);
|
|
localStorage.setItem('smirk_refresh', auth.refresh_token);
|
|
setUser(auth.user);
|
|
setStep('logged-in');
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Authentication failed');
|
|
setStep('choose-coin');
|
|
}
|
|
};
|
|
|
|
const handleLogout = () => {
|
|
localStorage.removeItem('smirk_token');
|
|
localStorage.removeItem('smirk_refresh');
|
|
setUser(null);
|
|
setSelectedCoin(null);
|
|
setStep('initial');
|
|
};
|
|
|
|
// Check for existing session on mount
|
|
useEffect(() => {
|
|
const token = localStorage.getItem('smirk_token');
|
|
if (token) {
|
|
// Validate token with /auth/me
|
|
getMe(token)
|
|
.then((userData) => {
|
|
setUser({
|
|
id: userData.id,
|
|
telegram_id: userData.telegram_id ?? null,
|
|
telegram_username: userData.telegram_username ?? null,
|
|
});
|
|
setStep('logged-in');
|
|
})
|
|
.catch(() => {
|
|
// Token invalid or expired - clear it
|
|
localStorage.removeItem('smirk_token');
|
|
localStorage.removeItem('smirk_refresh');
|
|
})
|
|
.finally(() => setCheckingSession(false));
|
|
} else {
|
|
setCheckingSession(false);
|
|
}
|
|
}, []);
|
|
|
|
// Show loading while checking session
|
|
if (checkingSession) {
|
|
return (
|
|
<div className="min-h-screen flex flex-col gap-6 items-center justify-center">
|
|
<div className="animate-spin w-8 h-8 border-2 border-[#fbeb0a] border-t-transparent rounded-full" />
|
|
<Link href="/downloads" className="text-[#fbeb0a] underline underline-offset-4">Download desktop</Link>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex-1 flex flex-col items-center justify-center p-8">
|
|
{/* Logo */}
|
|
<div className="mb-12 flex flex-col items-center">
|
|
<h1 className="creepster-text text-4xl mb-4">SMIRK WALLET</h1>
|
|
<div className="logo-glow">
|
|
<Image
|
|
src="/logo.svg"
|
|
alt="Smirk"
|
|
width={160}
|
|
height={160}
|
|
priority
|
|
/>
|
|
</div>
|
|
<p className="creepster-text text-2xl mt-4">smirk.ca$h</p>
|
|
{/* Only show user count before logging in */}
|
|
{!user && userCount !== null && userCount > 0 && (
|
|
<Link
|
|
href="/stats"
|
|
className="text-zinc-500 text-sm mt-4 block hover:text-zinc-400 transition-colors"
|
|
>
|
|
<span className="text-[#fbeb0a] font-semibold">{userCount.toLocaleString()}</span>{' '}
|
|
{userCount === 1 ? 'smirker' : 'smirkers'} and counting
|
|
</Link>
|
|
)}
|
|
|
|
{/* Social links */}
|
|
{!user && (
|
|
<div className="flex flex-col items-center mt-6">
|
|
<p className="text-zinc-500 text-sm mb-3">Join us</p>
|
|
<div className="flex gap-4">
|
|
<a
|
|
href="https://t.me/smirkwallet"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="text-zinc-500 hover:text-[#fbeb0a] transition-colors"
|
|
title="Telegram"
|
|
>
|
|
<svg className="w-6 h-6" viewBox="0 0 24 24" fill="currentColor">
|
|
<path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z"/>
|
|
</svg>
|
|
</a>
|
|
<a
|
|
href="https://discord.gg/7EnsaWTm6C"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="text-zinc-500 hover:text-[#fbeb0a] transition-colors"
|
|
title="Discord"
|
|
>
|
|
<svg className="w-6 h-6" viewBox="0 0 24 24" fill="currentColor">
|
|
<path d="M20.317 4.3698a19.7913 19.7913 0 00-4.8851-1.5152.0741.0741 0 00-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 00-.0785-.037 19.7363 19.7363 0 00-4.8852 1.515.0699.0699 0 00-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 00.0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 00.0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 00-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 01-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 01.0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 01.0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 01-.0066.1276 12.2986 12.2986 0 01-1.873.8914.0766.0766 0 00-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 00.0842.0286c1.961-.6067 3.9495-1.5219 6.0023-3.0294a.077.077 0 00.0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 00-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z"/>
|
|
</svg>
|
|
</a>
|
|
<a
|
|
href="/feed"
|
|
className="text-zinc-500 hover:text-[#fbeb0a] transition-colors"
|
|
title="Nostr feed (self-sovereign)"
|
|
>
|
|
<svg className="w-6 h-6" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round">
|
|
<circle cx="12" cy="12" r="2" fill="currentColor" stroke="none" />
|
|
<path d="M7.5 16.5a6 6 0 0 1 0-9M4.7 19.3a10 10 0 0 1 0-14.6M16.5 7.5a6 6 0 0 1 0 9M19.3 4.7a10 10 0 0 1 0 14.6" />
|
|
</svg>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<Link href="/downloads" className="mb-8 rounded-xl border border-[#fbeb0a] px-6 py-3 font-semibold text-[#fbeb0a] hover:bg-zinc-900">Download desktop</Link>
|
|
|
|
{/* Initial state - Connect button */}
|
|
{step === 'initial' && (
|
|
<div className="flex flex-col items-center gap-6">
|
|
{hasExtension === false && (
|
|
<div className="flex flex-col items-center gap-4 mb-4">
|
|
<p className="creepster-text text-xl text-[#fbeb0a]">GET SMIRKED</p>
|
|
<div className="flex gap-4">
|
|
<a
|
|
href="https://chromewebstore.google.com/detail/smirk-wallet/mbohhckapcchfmanheggbkekofhajnci"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="flex items-center gap-2 px-4 py-2 bg-zinc-800 border border-zinc-700 rounded-lg
|
|
hover:border-[#fbeb0a] hover:shadow-[0_0_12px_rgba(251,235,10,0.4)] transition-all"
|
|
>
|
|
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="currentColor">
|
|
<path d="M12 0C8.21 0 4.831 1.757 2.632 4.501l3.953 6.848A5.454 5.454 0 0 1 12 6.545h10.691A12 12 0 0 0 12 0zM1.931 5.47A11.943 11.943 0 0 0 0 12c0 6.012 4.42 10.991 10.189 11.864l3.953-6.847a5.45 5.45 0 0 1-6.865-2.29zm13.342 2.166a5.446 5.446 0 0 1 1.45 7.09l.002.001h-.002l-5.344 9.257c.206.01.413.016.621.016 6.627 0 12-5.373 12-12 0-1.54-.29-3.011-.818-4.364zM12 16.364a4.364 4.364 0 1 1 0-8.728 4.364 4.364 0 0 1 0 8.728z"/>
|
|
</svg>
|
|
<span className="text-sm">Chrome Store</span>
|
|
</a>
|
|
<a
|
|
href={OFFICIAL_RELEASES}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="flex items-center gap-2 px-4 py-2 bg-zinc-800 border border-zinc-700 rounded-lg
|
|
hover:border-[#fbeb0a] hover:shadow-[0_0_12px_rgba(251,235,10,0.4)] transition-all"
|
|
>
|
|
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="currentColor">
|
|
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
|
|
</svg>
|
|
<span className="text-sm">GitHub</span>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<button
|
|
onClick={handleConnect}
|
|
disabled={hasExtension === false}
|
|
className="px-8 py-4 bg-[#fbeb0a] text-black font-bold rounded-xl
|
|
hover:bg-[#d4c708] transition-colors disabled:opacity-50
|
|
disabled:cursor-not-allowed text-lg"
|
|
>
|
|
Connect with Smirk
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Choose coin step */}
|
|
{step === 'choose-coin' && (
|
|
<div className="flex flex-col items-center gap-6">
|
|
<h2 className="text-xl font-semibold mb-2">Choose your coin to sign in</h2>
|
|
<p className="text-zinc-400 text-sm mb-4">
|
|
Pick your favorite cryptocurrency to authenticate
|
|
</p>
|
|
|
|
<div className="grid grid-cols-5 gap-4">
|
|
{COINS.map((coin) => (
|
|
<button
|
|
key={coin.id}
|
|
onClick={() => handleCoinSelect(coin.id)}
|
|
className={`coin-btn ${selectedCoin === coin.id ? 'selected' : ''}`}
|
|
>
|
|
<Image src={coin.icon} alt={coin.name} width={48} height={48} />
|
|
<span>{coin.id}</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Signing step */}
|
|
{step === 'signing' && (
|
|
<div className="flex flex-col items-center gap-4">
|
|
<div className="animate-spin w-8 h-8 border-2 border-[#fbeb0a] border-t-transparent rounded-full" />
|
|
<p className="text-zinc-400">
|
|
Signing with {selectedCoin?.toUpperCase()}...
|
|
</p>
|
|
<p className="text-zinc-500 text-sm">Check your extension</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Logged in state */}
|
|
{step === 'logged-in' && user && (
|
|
<div className="flex flex-col items-center gap-6 text-center">
|
|
<div className="w-16 h-16 bg-[#fbeb0a] rounded-full flex items-center justify-center">
|
|
<svg
|
|
className="w-8 h-8 text-black"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
viewBox="0 0 24 24"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={3}
|
|
d="M5 13l4 4L19 7"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
|
|
<div>
|
|
<h2 className="text-xl font-semibold mb-2">Logged in!</h2>
|
|
<p className="text-zinc-400 text-sm">
|
|
Signed in with {selectedCoin?.toUpperCase()}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="bg-zinc-900 rounded-xl p-6 w-full max-w-md">
|
|
<p className="text-zinc-500 text-xs mb-1">User ID</p>
|
|
<p className="font-mono text-sm break-all">{user.id}</p>
|
|
|
|
{user.telegram_username && (
|
|
<>
|
|
<p className="text-zinc-500 text-xs mt-4 mb-1">Telegram</p>
|
|
<p className="text-sm">@{user.telegram_username}</p>
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex gap-4">
|
|
<Link
|
|
href="/tips"
|
|
className="px-6 py-2 border border-zinc-700 rounded-lg text-sm
|
|
hover:border-[#fbeb0a] hover:shadow-[0_0_12px_rgba(251,235,10,0.4)] transition-all"
|
|
>
|
|
Tips
|
|
</Link>
|
|
<Link
|
|
href="/settings"
|
|
className="px-6 py-2 border border-zinc-700 rounded-lg text-sm
|
|
hover:border-[#fbeb0a] hover:shadow-[0_0_12px_rgba(251,235,10,0.4)] transition-all"
|
|
>
|
|
Settings
|
|
</Link>
|
|
<button
|
|
onClick={handleLogout}
|
|
className="px-6 py-2 border border-zinc-700 rounded-lg text-sm
|
|
hover:border-[#fbeb0a] hover:shadow-[0_0_12px_rgba(251,235,10,0.4)] transition-all"
|
|
>
|
|
Disconnect
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Error display */}
|
|
{error && (
|
|
<div className="mt-6 p-4 bg-red-900/20 border border-red-500/50 rounded-lg">
|
|
<p className="text-red-400 text-sm">{error}</p>
|
|
</div>
|
|
)}
|
|
|
|
</div>
|
|
);
|
|
}
|