feat: portal redesign — shadcn/ui, React Query, Zod, isolated portals
Foundation: - CLAUDE.md with full coding rules (tech stack, patterns, redirect rules) - Install @tanstack/react-query, zod, react-hook-form, shadcn/ui deps - CSS variables design system (Tailwind v4 + tw-animate-css) - Shared components: Button, Input, Label, Card, Badge, Separator, Form - PortalLoginShell: two-column login layout (branding left, form right) - PortalNav: shared sidebar nav used by all portals - ReactQueryProvider in root layout - api-client.ts: typed fetch wrapper (no raw fetch in components) Portal isolation: - Sidebar now returns null for all non-chapter routes; portals own their layout - Admin layout: auth guard + PortalNav (slate accent) - Org layout: auth guard + PortalNav (violet accent) - Member layout: server-side cookie check → MemberNav client component - Chapter admin sidebar: PortalNav (blue accent) Login pages (all use react-hook-form + Zod + PortalLoginShell): - /login → defaults next to /search (was /) — fixes redirect bug - /admin/login → replaces /admin - /org/login → replaces /org - /member-login → two-step phone+OTP with proper form validation Portal selector (/) redesigned with accent border-left cards Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+100
-122
@@ -2,144 +2,122 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { z } from 'zod';
|
||||
import { PortalLoginShell } from '../_components/portal-login-shell';
|
||||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '../_components/ui/form';
|
||||
import { Input } from '../_components/ui/input';
|
||||
import { Button } from '../_components/ui/button';
|
||||
import Link from 'next/link';
|
||||
|
||||
const API_BASE = process.env['NEXT_PUBLIC_API_URL'] ?? 'http://localhost:3001';
|
||||
|
||||
type Step = 'phone' | 'code';
|
||||
const phoneSchema = z.object({ phone: z.string().min(8, 'Enter your WhatsApp number') });
|
||||
const otpSchema = z.object({ code: z.string().length(6, 'Enter the 6-digit code') });
|
||||
type PhoneValues = z.infer<typeof phoneSchema>;
|
||||
type OtpValues = z.infer<typeof otpSchema>;
|
||||
|
||||
export default function MemberLoginPage() {
|
||||
const router = useRouter();
|
||||
const [step, setStep] = useState<Step>('phone');
|
||||
const [step, setStep] = useState<'phone' | 'otp'>('phone');
|
||||
const [phone, setPhone] = useState('');
|
||||
const [challengeId, setChallengeId] = useState('');
|
||||
const [code, setCode] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function requestOtp() {
|
||||
setError(null);
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/public/auth/member-login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ phone }),
|
||||
});
|
||||
const data = (await res.json().catch(() => ({}))) as { challengeId?: string; message?: string };
|
||||
if (!res.ok) {
|
||||
setError(data.message ?? 'Failed to send code');
|
||||
return;
|
||||
}
|
||||
setChallengeId(data.challengeId ?? '');
|
||||
setStep('code');
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
const phoneForm = useForm<PhoneValues>({ resolver: zodResolver(phoneSchema), defaultValues: { phone: '' } });
|
||||
const otpForm = useForm<OtpValues>({ resolver: zodResolver(otpSchema), defaultValues: { code: '' } });
|
||||
|
||||
async function onPhone(values: PhoneValues) {
|
||||
const res = await fetch(`${API_BASE}/public/auth/member-login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ phone: values.phone }),
|
||||
});
|
||||
const data = await res.json().catch(() => ({})) as { challengeId?: string; message?: string };
|
||||
if (!res.ok) { phoneForm.setError('root', { message: data.message ?? 'Failed to send code' }); return; }
|
||||
setPhone(values.phone);
|
||||
setChallengeId(data.challengeId ?? '');
|
||||
setStep('otp');
|
||||
}
|
||||
|
||||
async function verifyOtp() {
|
||||
setError(null);
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await fetch('/api/my/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ challengeId, phone, code }),
|
||||
});
|
||||
const data = (await res.json().catch(() => ({}))) as { message?: string };
|
||||
if (!res.ok) {
|
||||
setError(data.message ?? 'Verification failed');
|
||||
return;
|
||||
}
|
||||
router.push('/my');
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
async function onOtp(values: OtpValues) {
|
||||
const res = await fetch('/api/my/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ challengeId, phone, code: values.code }),
|
||||
});
|
||||
const data = await res.json().catch(() => ({})) as { message?: string };
|
||||
if (!res.ok) { otpForm.setError('root', { message: data.message ?? 'Verification failed' }); return; }
|
||||
router.push('/my');
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50 flex items-center justify-center p-4">
|
||||
<div className="w-full max-w-sm bg-white rounded-2xl border border-gray-200 shadow-sm p-7 space-y-6">
|
||||
<div>
|
||||
<div className="w-10 h-10 rounded-xl bg-indigo-100 flex items-center justify-center text-xl mb-4">🏠</div>
|
||||
<h1 className="text-lg font-semibold text-gray-900">Sign in to your portal</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">
|
||||
{step === 'phone'
|
||||
? 'Enter your WhatsApp number and we\'ll send you a code.'
|
||||
: `We sent a 6-digit code to ${phone} on WhatsApp.`}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{step === 'phone' && (
|
||||
<div className="space-y-4">
|
||||
<input
|
||||
type="tel"
|
||||
value={phone}
|
||||
onChange={(e) => setPhone(e.target.value)}
|
||||
placeholder="+91 98765 43210"
|
||||
autoFocus
|
||||
className="w-full rounded-lg border border-gray-300 px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
|
||||
/>
|
||||
{error && <p className="text-sm text-red-600">{error}</p>}
|
||||
<button
|
||||
type="button"
|
||||
onClick={requestOtp}
|
||||
disabled={busy || phone.replace(/\D/g, '').length < 8}
|
||||
className="w-full rounded-lg bg-indigo-600 text-white text-sm font-medium py-2.5 hover:bg-indigo-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{busy ? 'Sending…' : 'Send code'}
|
||||
</button>
|
||||
<p className="text-xs text-center text-gray-400">
|
||||
First time?{' '}
|
||||
<a href="/onboard" className="text-indigo-600 hover:underline">
|
||||
Use your invite link
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{step === 'code' && (
|
||||
<div className="space-y-4">
|
||||
<input
|
||||
type="text"
|
||||
inputMode="numeric"
|
||||
pattern="[0-9]{6}"
|
||||
maxLength={6}
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value.replace(/\D/g, ''))}
|
||||
placeholder="000000"
|
||||
autoFocus
|
||||
className="w-full rounded-lg border border-gray-300 px-3 py-2.5 text-center text-lg tracking-[0.3em] font-mono focus:outline-none focus:ring-2 focus:ring-indigo-500"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={requestOtp}
|
||||
disabled={busy}
|
||||
className="text-xs text-indigo-600 hover:underline disabled:opacity-50 w-full text-center"
|
||||
>
|
||||
Didn't get it? Resend code
|
||||
</button>
|
||||
{error && <p className="text-sm text-red-600">{error}</p>}
|
||||
<PortalLoginShell
|
||||
title="Member Portal"
|
||||
subtitle={step === 'phone' ? 'Enter your WhatsApp number to receive a sign-in code.' : `We sent a 6-digit code to ${phone} on WhatsApp.`}
|
||||
accentClass="bg-emerald-600"
|
||||
footer={
|
||||
step === 'phone'
|
||||
? <><Link href="/onboard" className="hover:text-foreground transition-colors">First time? Use your invite link</Link> · <Link href="/" className="hover:text-foreground transition-colors">Portal selector</Link></>
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{step === 'phone' ? (
|
||||
<Form {...phoneForm}>
|
||||
<form onSubmit={phoneForm.handleSubmit(onPhone)} className="space-y-4">
|
||||
<FormField control={phoneForm.control} name="phone" render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>WhatsApp number</FormLabel>
|
||||
<FormControl><Input type="tel" placeholder="+91 98765 43210" autoFocus {...field} /></FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
{phoneForm.formState.errors.root && (
|
||||
<p className="text-sm text-destructive">{phoneForm.formState.errors.root.message}</p>
|
||||
)}
|
||||
<Button type="submit" className="w-full" disabled={phoneForm.formState.isSubmitting}>
|
||||
{phoneForm.formState.isSubmitting ? 'Sending…' : 'Send code'}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
) : (
|
||||
<Form {...otpForm}>
|
||||
<form onSubmit={otpForm.handleSubmit(onOtp)} className="space-y-4">
|
||||
<FormField control={otpForm.control} name="code" render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>6-digit code</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
inputMode="numeric"
|
||||
pattern="[0-9]{6}"
|
||||
maxLength={6}
|
||||
placeholder="000000"
|
||||
className="text-center text-xl tracking-[0.5em] font-mono"
|
||||
autoFocus
|
||||
{...field}
|
||||
onChange={(e) => field.onChange(e.target.value.replace(/\D/g, ''))}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)} />
|
||||
{otpForm.formState.errors.root && (
|
||||
<p className="text-sm text-destructive">{otpForm.formState.errors.root.message}</p>
|
||||
)}
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => { setStep('phone'); setCode(''); setError(null); }}
|
||||
className="rounded-lg border border-gray-300 text-gray-600 text-sm font-medium px-4 py-2.5 hover:bg-gray-50"
|
||||
>
|
||||
<Button type="button" variant="outline" onClick={() => { setStep('phone'); otpForm.reset(); }} className="flex-none">
|
||||
Back
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={verifyOtp}
|
||||
disabled={busy || code.length < 6}
|
||||
className="flex-1 rounded-lg bg-indigo-600 text-white text-sm font-medium py-2.5 hover:bg-indigo-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{busy ? 'Verifying…' : 'Sign in'}
|
||||
</button>
|
||||
</Button>
|
||||
<Button type="submit" className="flex-1" disabled={otpForm.formState.isSubmitting}>
|
||||
{otpForm.formState.isSubmitting ? 'Verifying…' : 'Sign in'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Button type="button" variant="ghost" size="sm" className="w-full text-muted-foreground" onClick={() => void onPhone({ phone })}>
|
||||
Resend code
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)}
|
||||
</PortalLoginShell>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user