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

182 lines
6.8 KiB
TypeScript

'use client';
import { Suspense, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { useToast } from '@/components/ui/toast';
import { githubService } from '@/services/githubService';
function AuthForm() {
const searchParams = useSearchParams();
const defaultMode = searchParams.get('mode') === 'register' ? false : true;
const [isLogin, setIsLogin] = useState(defaultMode);
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const router = useRouter();
const { showToast } = useToast();
const handleGitHubSignIn = () => {
if (!process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID) {
showToast('GitHub OAuth not configured', 'error');
return;
}
// Redirects to GitHub's authorize page; /auth/github/callback completes it.
githubService.connectGitHub();
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
try {
const endpoint = isLogin ? 'login' : 'register';
const body = isLogin
? { email, password }
: { username, email, password };
const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api';
const res = await fetch(`${apiUrl}/v1/auth/${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(body),
});
const data = await res.json();
if (res.ok) {
// Token is set as httpOnly cookie by the backend
if (data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
router.push('/');
} else {
showToast(data.message || 'Authentication failed', 'error');
}
} catch (error) {
showToast('Failed to authenticate', 'error');
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-primary/10">
<div className="bg-white dark:bg-gray-800 rounded-2xl shadow-2xl p-8 w-full max-w-md">
<div className="text-center mb-8">
<h1 className="text-3xl font-bold mb-2">Ritgram</h1>
<p className="text-gray-600 dark:text-gray-400">
Instagram for GitHub Repositories
</p>
</div>
<button
type="button"
onClick={handleGitHubSignIn}
aria-label="Sign in with GitHub"
className="w-full flex items-center justify-center gap-3 py-3 mb-6 rounded-lg bg-gray-900 text-white font-medium hover:bg-black transition"
>
<svg className="w-5 h-5" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 .5C5.37.5 0 5.87 0 12.5c0 5.3 3.44 9.8 8.21 11.39.6.11.82-.26.82-.58 0-.29-.01-1.04-.02-2.05-3.34.72-4.04-1.61-4.04-1.61-.55-1.39-1.34-1.76-1.34-1.76-1.09-.75.08-.73.08-.73 1.2.09 1.84 1.24 1.84 1.24 1.07 1.83 2.81 1.3 3.5.99.11-.78.42-1.3.76-1.6-2.67-.3-5.47-1.33-5.47-5.93 0-1.31.47-2.38 1.24-3.22-.13-.3-.54-1.52.11-3.18 0 0 1.01-.32 3.3 1.23a11.5 11.5 0 016 0c2.29-1.55 3.3-1.23 3.3-1.23.65 1.66.24 2.88.12 3.18.77.84 1.23 1.91 1.23 3.22 0 4.61-2.8 5.63-5.48 5.92.43.37.81 1.1.81 2.22 0 1.6-.01 2.9-.01 3.29 0 .32.22.7.83.58A12.01 12.01 0 0024 12.5C24 5.87 18.63.5 12 .5z" />
</svg>
Sign in with GitHub
</button>
<div className="relative mb-6">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-gray-200 dark:border-gray-700" />
</div>
<div className="relative flex justify-center text-sm">
<span className="px-3 bg-white dark:bg-gray-800 text-gray-500">or</span>
</div>
</div>
<div className="flex gap-2 mb-6">
<button
onClick={() => setIsLogin(true)}
className={`flex-1 py-2 rounded-lg font-medium transition ${
isLogin
? 'bg-blue-600 text-white'
: 'bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400'
}`}
>
Login
</button>
<button
onClick={() => setIsLogin(false)}
className={`flex-1 py-2 rounded-lg font-medium transition ${
!isLogin
? 'bg-blue-600 text-white'
: 'bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400'
}`}
>
Register
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
{!isLogin && (
<div>
<label htmlFor="auth-username" className="block text-sm font-medium mb-2">Username</label>
<input
id="auth-username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:bg-gray-700"
required
/>
</div>
)}
<div>
<label htmlFor="auth-email" className="block text-sm font-medium mb-2">Email</label>
<input
id="auth-email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:bg-gray-700"
required
/>
</div>
<div>
<label htmlFor="auth-password" className="block text-sm font-medium mb-2">Password</label>
<input
id="auth-password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:bg-gray-700"
required
/>
</div>
<button
type="submit"
disabled={loading}
className="w-full py-3 bg-primary text-white rounded-lg font-medium hover:bg-primary-hover disabled:opacity-50 transition"
>
{loading ? 'Please wait...' : isLogin ? 'Login' : 'Create Account'}
</button>
</form>
<div className="mt-6 text-center text-sm text-gray-600 dark:text-gray-400">
<p>New: Repositories are social accounts!</p>
<p className="mt-2">Claim your repos and post as them</p>
</div>
</div>
</div>
);
}
export default function AuthPage() {
return (
<Suspense fallback={<div className="min-h-screen bg-background" />}>
<AuthForm />
</Suspense>
);
}