railyard/apps/ritgram/web/components/AccountSwitcher.tsx
caelum0x aa848c66d0 fix(ritgram): Apple-design unslop pass + Rithub OAuth2 sign-in + critical login-redirect-loop fix
Apple design (plans/gtm/APPLE-DESIGN-RULES-2026-11-17.md): rewrote theme
tokens in globals.css/tailwind.config.js to neutral black/white + one
accent (#0071E3), replacing the GitHub-dark blue+green dual-accent theme;
mechanically swept every raw inline hex/rgba literal across app/components
that bypassed the CSS vars (506 hex + 326 rgb(a) replacements) since most
of the app hardcoded colors rather than using tokens; removed all emoji,
decorative gradients (Tailwind bg-gradient-to-* and raw linear/radial-
gradient() in inline styles), confetti, and glow/blur decorative orbs;
converted Navbar/Navigation to icon-only with aria-label+title. Finished
two stopped agents' in-progress work: deleted the fake AnimatedStats/
LiveActivity/ProjectShowcase/Testimonials auth-panel components and
cleaned up the emoji-laden feature list in (auth)/layout.tsx. Removed
remaining fabricated stats (topics/topics/[slug] posts-per-week/
contributors/followers) per the no-fake-numbers rule.

Rithub sign-in: added "Continue with Rithub" as an icon-only OAuth2
button (RithubMark component) on login/register, hidden "Continue with
GitHub" when GITHUB_CLIENT_ID is unset (confirmed unset in prod). Backend:
authenticateWithGitea in oauthService.ts exchanges the Gitea OAuth2 code,
resolves identity via the existing ForgeConnection table (no schema
migration needed), issues the same JWT/session as GitHub/Google login.
OAuth2 app created live on rithub.dev via its REST API as admin user
caelum0x; client id/secret stored in railyard-box's compose file (env
files don't ship to the runtime image, so the actual wiring lives in
docker-compose.yml, not backend/.env) and in ~/products/.env.

Critical bug found and fixed while verifying: services/api.ts's global
401 interceptor redirected every anonymous visitor to /login a couple
seconds after ANY page load -- including /login and /register themselves
-- because the AuthBootstrap's background GET /auth/me (expected to 401
for signed-out visitors) was treated the same as a real session-expiry
401, triggering a refresh attempt, another 401, and a hard
window.location.href redirect, which re-ran AuthBootstrap and repeated.
Fixed by excluding /auth/me and /auth/refresh from that redirect path.
Verified live with a fresh, cookie-less browser: previously looped
/register -> /login forever; now stable after 6s.

Also fixed: production CORS/API origin mismatch -- the frontend was
built with NEXT_PUBLIC_API_URL pointing at ritgram.ereboslab.com while
served from ritgram.rithub.dev, so every API call (login, register, etc.)
was blocked by CORS; rebuilt with rithub.dev as canonical and CORS_ORIGIN
allowing both domains.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-17 17:33:41 +03:00

108 lines
3.9 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import api from '@/services/api';
interface Account {
id: string;
type: 'USER' | 'REPOSITORY' | 'ORGANIZATION';
username: string;
displayName?: string;
avatar?: string;
}
export default function AccountSwitcher() {
const [accounts, setAccounts] = useState<Account[]>([]);
const [currentAccount, setCurrentAccount] = useState<Account | null>(null);
const [isOpen, setIsOpen] = useState(false);
const router = useRouter();
useEffect(() => {
fetchAccounts();
}, []);
const fetchAccounts = async () => {
try {
const res = await api.get('/v1/switch/accounts');
const data = res.data;
setAccounts(data.accounts || []);
if (data.accounts?.length > 0) {
setCurrentAccount(data.accounts[0]);
}
} catch (error) {
console.error('Failed to fetch accounts:', error);
}
};
const switchAccount = async (account: Account) => {
try {
await api.post('/v1/switch/account', { accountId: account.id });
setCurrentAccount(account);
setIsOpen(false);
router.refresh();
} catch (error) {
console.error('Failed to switch account:', error);
}
};
if (!currentAccount) return null;
return (
<div className="relative">
<button
onClick={() => setIsOpen(!isOpen)}
className="flex items-center gap-2 px-3 py-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800"
>
<div className="w-8 h-8 rounded-full bg-primary flex items-center justify-center text-white font-bold">
{currentAccount.username[0].toUpperCase()}
</div>
<div className="text-left">
<div className="text-sm font-medium">@{currentAccount.username}</div>
<div className="text-xs text-gray-500">{currentAccount.type}</div>
</div>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
</svg>
</button>
{isOpen && (
<div className="absolute top-full mt-2 w-64 bg-white dark:bg-gray-900 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-50">
<div className="p-2">
{accounts.map((account) => (
<button
key={account.id}
onClick={() => switchAccount(account)}
className={`w-full flex items-center gap-3 px-3 py-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 ${
currentAccount.id === account.id ? 'bg-gray-100 dark:bg-gray-800' : ''
}`}
>
<div className="w-10 h-10 rounded-full bg-primary flex items-center justify-center text-white font-bold">
{account.username[0].toUpperCase()}
</div>
<div className="flex-1 text-left">
<div className="text-sm font-medium">@{account.username}</div>
<div className="text-xs text-gray-500">{account.type}</div>
</div>
{currentAccount.id === account.id && (
<svg className="w-5 h-5 text-green-500" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" />
</svg>
)}
</button>
))}
</div>
<div className="border-t border-gray-200 dark:border-gray-700 p-2">
<button
onClick={() => router.push('/claim')}
className="w-full px-3 py-2 text-sm text-left rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 text-blue-600 dark:text-blue-400 font-medium"
>
+ Claim Repository
</button>
</div>
</div>
)}
</div>
);
}