The canonical /login and /register already offered GitHub OAuth, but the
legacy /auth screen (reached via router.push('/auth') from insights, drafts,
creator-studio, follow-requests) was email/password only — a GitHub-native
new user landing there had no way in. Add a guarded "Sign in with GitHub"
button that calls githubService.connectGitHub() (redirects to GitHub's
authorize endpoint; /auth/github/callback, already in middleware publicPaths,
completes it). Also drop the emoji from the footer copy per the no-emoji UI
rule while editing this screen.
Test: connectGitHub redirects to the authorize endpoint with the configured
client_id, callback redirect_uri, and user:email,repo,read:user scopes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
182 lines
6.9 KiB
TypeScript
182 lines
6.9 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-gradient-to-br from-purple-500 to-pink-500">
|
|
<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">Gitgram</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-gradient-to-r from-purple-600 to-pink-600 text-white rounded-lg font-medium hover:from-purple-700 hover:to-pink-700 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>
|
|
);
|
|
}
|