feat(app): identity screens and /app shell — auth store, login/signup/reset flows, dashboard, account
- state/authStore: session subscription (initialize + onAuthStateChange), profile + roles load, recovery flag, URL failure; services/localeSync: profiles.locale <-> localeStore - routes: /login, /signup, /forgot-password, /reset-password, /auth/callback; guards RequireAuth (next=) and RedirectIfSignedIn; /app (dashboard), /app/account behind the guard - shell: AppLayout (desktop sidebar, light top bar, mobile drawer), SidebarNav, UserBadge, SignOutButton - account: personal / professional / preferences / security sections, one save each, Zod validation with i18n keys - ui: Icon, FormField, Notice, ButtonLink (buttonClassName shared), PasswordField, OAuthButtons (config-driven) - Home header: Mon espace / Se connecter when Supabase is configured; local tool routes untouched - i18n: auth.*, shell.*, dashboard.*, account.*, home.space.* in fr + en
This commit is contained in:
1 parent
0f7292f9b3
commit
c517ea267b
46 files changed
+2798
-11
No files matched your search
@@ -1,12 +1,24 @@
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom'
|
||||
import { ConfigProvider } from './providers/ConfigProvider'
|
||||
import { AuthProvider } from './providers/AuthProvider'
|
||||
import Footer from '@/components/brand/Footer'
|
||||
import AppLayout from '@/components/shell/AppLayout'
|
||||
import RequireAuth from './guards/RequireAuth'
|
||||
import RedirectIfSignedIn from './guards/RedirectIfSignedIn'
|
||||
import HomeRoute from './routes/HomeRoute'
|
||||
import NewReportRoute from './routes/NewReportRoute'
|
||||
import EditorRoute from './routes/EditorRoute'
|
||||
import NotFoundRoute from './routes/NotFoundRoute'
|
||||
import LoginRoute from './routes/auth/LoginRoute'
|
||||
import SignupRoute from './routes/auth/SignupRoute'
|
||||
import ForgotPasswordRoute from './routes/auth/ForgotPasswordRoute'
|
||||
import ResetPasswordRoute from './routes/auth/ResetPasswordRoute'
|
||||
import AuthCallbackRoute from './routes/auth/AuthCallbackRoute'
|
||||
import DashboardRoute from './routes/app/DashboardRoute'
|
||||
import AccountRoute from './routes/app/AccountRoute'
|
||||
|
||||
function AppShell() {
|
||||
/** The local tool: unchanged routes, no account needed, shared footer. */
|
||||
function LocalToolShell() {
|
||||
return (
|
||||
<div className="flex min-h-screen flex-col">
|
||||
<main className="flex-1 flex flex-col">
|
||||
@@ -22,12 +34,37 @@ function AppShell() {
|
||||
)
|
||||
}
|
||||
|
||||
/** Auth screens and the signed-in /app shell sit beside the local tool. */
|
||||
export function AppRoutes() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route element={<RedirectIfSignedIn />}>
|
||||
<Route path="/login" element={<LoginRoute />} />
|
||||
<Route path="/signup" element={<SignupRoute />} />
|
||||
<Route path="/forgot-password" element={<ForgotPasswordRoute />} />
|
||||
</Route>
|
||||
<Route path="/reset-password" element={<ResetPasswordRoute />} />
|
||||
<Route path="/auth/callback" element={<AuthCallbackRoute />} />
|
||||
<Route path="/app" element={<RequireAuth />}>
|
||||
<Route element={<AppLayout />}>
|
||||
<Route index element={<DashboardRoute />} />
|
||||
<Route path="account" element={<AccountRoute />} />
|
||||
<Route path="*" element={<NotFoundRoute />} />
|
||||
</Route>
|
||||
</Route>
|
||||
<Route path="*" element={<LocalToolShell />} />
|
||||
</Routes>
|
||||
)
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<ConfigProvider>
|
||||
<BrowserRouter>
|
||||
<AppShell />
|
||||
</BrowserRouter>
|
||||
<AuthProvider>
|
||||
<BrowserRouter>
|
||||
<AppRoutes />
|
||||
</BrowserRouter>
|
||||
</AuthProvider>
|
||||
</ConfigProvider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Navigate, Outlet, useSearchParams } from 'react-router-dom'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { safeNextPath } from '@/auth/redirects'
|
||||
|
||||
/** Wraps login / signup / forgot: a signed-in user goes straight to the app. */
|
||||
export default function RedirectIfSignedIn() {
|
||||
const status = useAuthStore((s) => s.status)
|
||||
const recovery = useAuthStore((s) => s.recovery)
|
||||
const [params] = useSearchParams()
|
||||
if (status === 'signed_in' && !recovery) {
|
||||
return <Navigate to={safeNextPath(params.get('next'))} replace />
|
||||
}
|
||||
return <Outlet />
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Navigate, Outlet, useLocation } from 'react-router-dom'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import SessionLoading from './SessionLoading'
|
||||
|
||||
/** Wraps /app/*: waits for the session, else sends to /login with a return path. */
|
||||
export default function RequireAuth() {
|
||||
const status = useAuthStore((s) => s.status)
|
||||
const location = useLocation()
|
||||
if (status === 'loading') return <SessionLoading />
|
||||
if (status === 'signed_in') return <Outlet />
|
||||
const next = encodeURIComponent(location.pathname + location.search)
|
||||
return <Navigate to={`/login?next=${next}`} replace />
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import Brand from '@/components/brand/Brand'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Full-screen wait while the client reads the persisted session. */
|
||||
export default function SessionLoading() {
|
||||
const t = useT()
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
className="flex min-h-dvh flex-col items-center justify-center gap-4"
|
||||
>
|
||||
<Brand size="lg" />
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('shell.loading_session')}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useEffect, type ReactNode } from 'react'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { startLocaleSync } from '@/services/localeSync'
|
||||
|
||||
/** Starts the session subscription and the locale sync for the app lifetime. */
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
useEffect(() => {
|
||||
const stopAuth = useAuthStore.getState().start()
|
||||
const stopSync = startLocaleSync()
|
||||
return () => {
|
||||
stopAuth()
|
||||
stopSync()
|
||||
}
|
||||
}, [])
|
||||
return <>{children}</>
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
type RebaseOutcome,
|
||||
} from '@faunosteo/domain'
|
||||
import AppHeader from '@/components/brand/AppHeader'
|
||||
import HomeSpaceLink from '@/components/brand/HomeSpaceLink'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import { importReportFromPdf, ImportError, MAX_PDF_BYTES } from '@/pdf/import'
|
||||
@@ -147,7 +148,9 @@ export default function HomeRoute() {
|
||||
|
||||
return (
|
||||
<main className="flex-1 flex flex-col">
|
||||
<AppHeader />
|
||||
<AppHeader>
|
||||
<HomeSpaceLink />
|
||||
</AppHeader>
|
||||
<section className="flex-1 grid place-items-center px-4 sm:px-6">
|
||||
<div className="grid gap-4 md:grid-cols-2 max-w-3xl w-full">
|
||||
{noSheet && (
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import PersonalInfoForm from '@/components/account/PersonalInfoForm'
|
||||
import PreferencesForm from '@/components/account/PreferencesForm'
|
||||
import ProfessionalInfoForm from '@/components/account/ProfessionalInfoForm'
|
||||
import RoleBadges from '@/components/account/RoleBadges'
|
||||
import SecurityPanel from '@/components/account/SecurityPanel'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** "Mon compte": four sections, each with its own save. */
|
||||
export default function AccountRoute() {
|
||||
const t = useT()
|
||||
const profile = useAuthStore((s) => s.profile)
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<header className="space-y-2">
|
||||
<h1 className="font-serif text-2xl sm:text-3xl text-[var(--color-ink)]">
|
||||
{t('account.title')}
|
||||
</h1>
|
||||
<RoleBadges />
|
||||
</header>
|
||||
{profile ? (
|
||||
<>
|
||||
<PersonalInfoForm key={`p-${profile.id}`} profile={profile} />
|
||||
<ProfessionalInfoForm key={`pro-${profile.id}`} profile={profile} />
|
||||
<PreferencesForm key={`pref-${profile.id}`} profile={profile} />
|
||||
</>
|
||||
) : (
|
||||
<Notice tone="info">{t('auth.loading')}</Notice>
|
||||
)}
|
||||
<SecurityPanel />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
import type { Profile } from '@faunosteo/supabase'
|
||||
import RoleBadges from '@/components/account/RoleBadges'
|
||||
import ButtonLink from '@/components/ui/ButtonLink'
|
||||
import Card from '@/components/ui/Card'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
/** Profile fields the dashboard asks to complete, with their label keys. */
|
||||
const COMPLETENESS: readonly { key: keyof Profile; labelKey: string }[] = [
|
||||
{ key: 'first_name', labelKey: 'account.personal.first_name' },
|
||||
{ key: 'last_name', labelKey: 'account.personal.last_name' },
|
||||
{ key: 'professional_title', labelKey: 'account.professional.professional_title' },
|
||||
{ key: 'practice_name', labelKey: 'account.professional.practice_name' },
|
||||
]
|
||||
|
||||
function missingFields(profile: Profile | null, t: Translate): string[] {
|
||||
if (!profile) return []
|
||||
return COMPLETENESS.filter((f) => !profile[f.key]).map((f) => t(f.labelKey))
|
||||
}
|
||||
|
||||
function greeting(profile: Profile | null, t: Translate): string {
|
||||
const name = profile?.first_name ?? profile?.display_name
|
||||
return name
|
||||
? t('dashboard.greeting').replace('{name}', name)
|
||||
: t('dashboard.greeting_fallback')
|
||||
}
|
||||
|
||||
function ProfileCard({ missing }: { missing: string[] }) {
|
||||
const t = useT()
|
||||
const complete = missing.length === 0
|
||||
return (
|
||||
<Card as="section" className="p-5 sm:p-6 space-y-3">
|
||||
<h2 className="font-serif text-xl">{t('dashboard.profile.title')}</h2>
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{complete ? t('dashboard.profile.complete') : t('dashboard.profile.incomplete')}
|
||||
</p>
|
||||
{!complete && (
|
||||
<p className="text-sm">
|
||||
<span className="text-[var(--color-ink-muted)]">
|
||||
{t('dashboard.profile.missing_label')}
|
||||
</span>{' '}
|
||||
{missing.join(', ')}
|
||||
</p>
|
||||
)}
|
||||
<ButtonLink to="/app/account" variant={complete ? 'secondary' : 'primary'}>
|
||||
{complete ? t('dashboard.profile.cta_view') : t('dashboard.profile.cta_complete')}
|
||||
</ButtonLink>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function ToolCard() {
|
||||
const t = useT()
|
||||
return (
|
||||
<Card as="section" className="p-5 sm:p-6 space-y-3">
|
||||
<h2 className="font-serif text-xl">{t('dashboard.tool.title')}</h2>
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('dashboard.tool.description')}
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<ButtonLink to="/new" variant="secondary">
|
||||
{t('dashboard.tool.cta_new')}
|
||||
</ButtonLink>
|
||||
<ButtonLink to="/" variant="ghost">
|
||||
{t('dashboard.tool.cta_import')}
|
||||
</ButtonLink>
|
||||
</div>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function AccountCard() {
|
||||
const t = useT()
|
||||
const user = useAuthStore((s) => s.user)
|
||||
const roles = useAuthStore((s) => s.roles)
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const verified = Boolean(user?.email_confirmed_at)
|
||||
const created = user?.created_at
|
||||
? new Date(user.created_at).toLocaleDateString(locale)
|
||||
: null
|
||||
return (
|
||||
<Card as="section" className="p-5 sm:p-6 space-y-3">
|
||||
<h2 className="font-serif text-xl">{t('dashboard.account.title')}</h2>
|
||||
<dl className="grid gap-2 text-sm">
|
||||
<div className="flex justify-between gap-4">
|
||||
<dt className="text-[var(--color-ink-muted)]">{t('dashboard.account.email')}</dt>
|
||||
<dd className="truncate">{user?.email}</dd>
|
||||
</div>
|
||||
<div className="flex items-center justify-end gap-1.5 text-[var(--color-ink-muted)]">
|
||||
<Icon name={verified ? 'check' : 'alert'} size={16} />
|
||||
<span>{verified ? t('dashboard.account.verified') : t('dashboard.account.unverified')}</span>
|
||||
</div>
|
||||
<div className="flex justify-between gap-4">
|
||||
<dt className="text-[var(--color-ink-muted)]">{t('dashboard.account.language')}</dt>
|
||||
<dd>{t(`lang.${locale}`)}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-4">
|
||||
<dt className="text-[var(--color-ink-muted)]">{t('dashboard.account.roles')}</dt>
|
||||
<dd>{roles.length ? <RoleBadges /> : t('dashboard.account.no_roles')}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
{created && (
|
||||
<p className="text-xs text-[var(--color-ink-muted)]">
|
||||
{t('dashboard.account.member_since').replace('{date}', created)}
|
||||
</p>
|
||||
)}
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
export default function DashboardRoute() {
|
||||
const t = useT()
|
||||
const profile = useAuthStore((s) => s.profile)
|
||||
const missing = missingFields(profile, t)
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<header className="space-y-1">
|
||||
<h1 className="font-serif text-2xl sm:text-3xl text-[var(--color-ink)]">
|
||||
{greeting(profile, t)}
|
||||
</h1>
|
||||
<p className="max-w-prose text-sm text-[var(--color-ink-muted)]">
|
||||
{t('dashboard.intro')}
|
||||
</p>
|
||||
</header>
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<ProfileCard missing={missing} />
|
||||
<ToolCard />
|
||||
<AccountCard />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Navigate, useSearchParams } from 'react-router-dom'
|
||||
import AuthLayout from '@/components/auth/AuthLayout'
|
||||
import AuthLink from '@/components/auth/AuthLink'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import SessionLoading from '@/app/guards/SessionLoading'
|
||||
import { APP_HOME_PATH, safeNextPath } from '@/auth/redirects'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/**
|
||||
* Landing of OAuth redirects and email confirmation links. The client reads
|
||||
* the URL on start; this route only waits for the outcome.
|
||||
*/
|
||||
export default function AuthCallbackRoute() {
|
||||
const t = useT()
|
||||
const status = useAuthStore((s) => s.status)
|
||||
const urlFailure = useAuthStore((s) => s.urlFailure)
|
||||
const [params] = useSearchParams()
|
||||
|
||||
if (status === 'signed_in') {
|
||||
return <Navigate to={safeNextPath(params.get('next'), APP_HOME_PATH)} replace />
|
||||
}
|
||||
const providerError = params.get('error_description') ?? params.get('error')
|
||||
if (status === 'loading' && !urlFailure && !providerError) {
|
||||
return <SessionLoading />
|
||||
}
|
||||
// Landing here without a session means the exchange failed, whatever the client reported.
|
||||
return (
|
||||
<AuthLayout title={t('auth.callback.failed_title')}>
|
||||
<Notice tone="error">{t('auth.callback.failed_description')}</Notice>
|
||||
<AuthLink to="/login">{t('auth.callback.retry')}</AuthLink>
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { sendPasswordReset } from '@faunosteo/supabase'
|
||||
import AuthLayout from '@/components/auth/AuthLayout'
|
||||
import AuthLink from '@/components/auth/AuthLink'
|
||||
import UnavailableNotice from '@/components/auth/UnavailableNotice'
|
||||
import Button from '@/components/ui/Button'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { absoluteUrl, RESET_PASSWORD_PATH } from '@/auth/redirects'
|
||||
import { emailSchema } from '@/auth/validation'
|
||||
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export default function ForgotPasswordRoute() {
|
||||
const t = useT()
|
||||
const describe = useAuthFailureMessage()
|
||||
const client = getSupabaseClient()
|
||||
const [email, setEmail] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [sentTo, setSentTo] = useState<string | null>(null)
|
||||
|
||||
async function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
if (!client) return
|
||||
const parsed = emailSchema.safeParse(email)
|
||||
if (!parsed.success) {
|
||||
setError(parsed.error.issues[0]?.message ?? 'auth.error.email_invalid')
|
||||
return
|
||||
}
|
||||
setError(null)
|
||||
setFailure(null)
|
||||
setBusy(true)
|
||||
try {
|
||||
await sendPasswordReset(client, parsed.data, absoluteUrl(RESET_PASSWORD_PATH))
|
||||
setSentTo(parsed.data)
|
||||
} catch (err) {
|
||||
setFailure(describe(err))
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const footer = (
|
||||
<p>
|
||||
<AuthLink to="/login">{t('auth.forgot.back_to_login')}</AuthLink>
|
||||
</p>
|
||||
)
|
||||
|
||||
if (sentTo) {
|
||||
return (
|
||||
<AuthLayout title={t('auth.forgot.sent_title')} footer={footer}>
|
||||
<Notice tone="success">
|
||||
{t('auth.forgot.sent_description').replace('{email}', sentTo)}
|
||||
</Notice>
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthLayout
|
||||
title={t('auth.forgot.title')}
|
||||
subtitle={t('auth.forgot.subtitle')}
|
||||
footer={client ? footer : undefined}
|
||||
>
|
||||
{!client ? (
|
||||
<UnavailableNotice />
|
||||
) : (
|
||||
<form onSubmit={submit} noValidate className="space-y-4">
|
||||
<FormField
|
||||
id="forgot-email"
|
||||
label={t('auth.email')}
|
||||
error={error ? t(error) : undefined}
|
||||
>
|
||||
<Input
|
||||
id="forgot-email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
inputMode="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
aria-invalid={Boolean(error)}
|
||||
aria-describedby={fieldDescribedBy('forgot-email', undefined, error ?? undefined)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<Button type="submit" size="lg" className="w-full" disabled={busy}>
|
||||
{busy ? t('auth.forgot.submitting') : t('auth.forgot.submit')}
|
||||
</Button>
|
||||
</form>
|
||||
)}
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom'
|
||||
import { signInWithPassword } from '@faunosteo/supabase'
|
||||
import AuthLayout from '@/components/auth/AuthLayout'
|
||||
import AuthLink from '@/components/auth/AuthLink'
|
||||
import OAuthButtons from '@/components/auth/OAuthButtons'
|
||||
import PasswordField from '@/components/auth/PasswordField'
|
||||
import UnavailableNotice from '@/components/auth/UnavailableNotice'
|
||||
import Button from '@/components/ui/Button'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { safeNextPath } from '@/auth/redirects'
|
||||
import { fieldErrors, loginSchema, type FieldErrors } from '@/auth/validation'
|
||||
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type LoginValues = { email: string; password: string }
|
||||
|
||||
export default function LoginRoute() {
|
||||
const t = useT()
|
||||
const navigate = useNavigate()
|
||||
const [params] = useSearchParams()
|
||||
const describe = useAuthFailureMessage()
|
||||
const client = getSupabaseClient()
|
||||
const [values, setValues] = useState<LoginValues>({ email: '', password: '' })
|
||||
const [errors, setErrors] = useState<FieldErrors<LoginValues>>({})
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
async function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
if (!client) return
|
||||
const parsed = loginSchema.safeParse(values)
|
||||
if (!parsed.success) {
|
||||
setErrors(fieldErrors(parsed.error))
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setFailure(null)
|
||||
setBusy(true)
|
||||
try {
|
||||
await signInWithPassword(client, parsed.data)
|
||||
navigate(safeNextPath(params.get('next')), { replace: true })
|
||||
} catch (err) {
|
||||
setFailure(describe(err))
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const footer = (
|
||||
<p>
|
||||
{t('auth.login.no_account')}{' '}
|
||||
<AuthLink to="/signup">{t('auth.login.signup_link')}</AuthLink>
|
||||
</p>
|
||||
)
|
||||
|
||||
return (
|
||||
<AuthLayout
|
||||
title={t('auth.login.title')}
|
||||
subtitle={t('auth.login.subtitle')}
|
||||
footer={client ? footer : undefined}
|
||||
>
|
||||
{!client ? (
|
||||
<UnavailableNotice />
|
||||
) : (
|
||||
<>
|
||||
<OAuthButtons />
|
||||
<form onSubmit={submit} noValidate className="space-y-4">
|
||||
<FormField
|
||||
id="login-email"
|
||||
label={t('auth.email')}
|
||||
error={errors.email && t(errors.email)}
|
||||
>
|
||||
<Input
|
||||
id="login-email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
inputMode="email"
|
||||
value={values.email}
|
||||
onChange={(e) => setValues({ ...values, email: e.target.value })}
|
||||
aria-invalid={Boolean(errors.email)}
|
||||
aria-describedby={fieldDescribedBy('login-email', undefined, errors.email)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
<FormField
|
||||
id="login-password"
|
||||
label={t('auth.password')}
|
||||
error={errors.password && t(errors.password)}
|
||||
>
|
||||
<PasswordField
|
||||
id="login-password"
|
||||
autoComplete="current-password"
|
||||
value={values.password}
|
||||
onChange={(e) => setValues({ ...values, password: e.target.value })}
|
||||
aria-invalid={Boolean(errors.password)}
|
||||
aria-describedby={fieldDescribedBy('login-password', undefined, errors.password)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<Button type="submit" size="lg" className="w-full" disabled={busy}>
|
||||
{busy ? t('auth.login.submitting') : t('auth.login.submit')}
|
||||
</Button>
|
||||
<p className="text-center text-sm">
|
||||
<AuthLink to="/forgot-password">{t('auth.login.forgot')}</AuthLink>
|
||||
</p>
|
||||
</form>
|
||||
</>
|
||||
)}
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { updatePassword } from '@faunosteo/supabase'
|
||||
import AuthLayout from '@/components/auth/AuthLayout'
|
||||
import AuthLink from '@/components/auth/AuthLink'
|
||||
import PasswordField from '@/components/auth/PasswordField'
|
||||
import UnavailableNotice from '@/components/auth/UnavailableNotice'
|
||||
import Button from '@/components/ui/Button'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import SessionLoading from '@/app/guards/SessionLoading'
|
||||
import { APP_HOME_PATH } from '@/auth/redirects'
|
||||
import {
|
||||
fieldErrors,
|
||||
newPasswordSchema,
|
||||
PASSWORD_MIN_LENGTH,
|
||||
type FieldErrors,
|
||||
} from '@/auth/validation'
|
||||
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type Values = { password: string; passwordConfirm: string }
|
||||
|
||||
function NoSession() {
|
||||
const t = useT()
|
||||
return (
|
||||
<AuthLayout
|
||||
title={t('auth.reset.no_session_title')}
|
||||
footer={
|
||||
<p>
|
||||
<AuthLink to="/login">{t('auth.forgot.back_to_login')}</AuthLink>
|
||||
</p>
|
||||
}
|
||||
>
|
||||
<Notice tone="error">{t('auth.reset.no_session_description')}</Notice>
|
||||
<AuthLink to="/forgot-password">{t('auth.reset.request_again')}</AuthLink>
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Landing of the recovery link. The client exchanges the code on load and
|
||||
* emits PASSWORD_RECOVERY; any signed-in user may also change their password here.
|
||||
*/
|
||||
export default function ResetPasswordRoute() {
|
||||
const t = useT()
|
||||
const navigate = useNavigate()
|
||||
const describe = useAuthFailureMessage()
|
||||
const client = getSupabaseClient()
|
||||
const status = useAuthStore((s) => s.status)
|
||||
const clearRecovery = useAuthStore((s) => s.clearRecovery)
|
||||
const [values, setValues] = useState<Values>({ password: '', passwordConfirm: '' })
|
||||
const [errors, setErrors] = useState<FieldErrors<Values>>({})
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
async function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
if (!client) return
|
||||
const parsed = newPasswordSchema.safeParse(values)
|
||||
if (!parsed.success) {
|
||||
setErrors(fieldErrors(parsed.error))
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setFailure(null)
|
||||
setBusy(true)
|
||||
try {
|
||||
await updatePassword(client, parsed.data.password)
|
||||
clearRecovery()
|
||||
navigate(APP_HOME_PATH, { replace: true })
|
||||
} catch (err) {
|
||||
setFailure(describe(err))
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
return (
|
||||
<AuthLayout title={t('auth.reset.title')}>
|
||||
<UnavailableNotice />
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
if (status === 'loading') return <SessionLoading />
|
||||
if (status !== 'signed_in') return <NoSession />
|
||||
|
||||
return (
|
||||
<AuthLayout title={t('auth.reset.title')} subtitle={t('auth.reset.subtitle')}>
|
||||
<form onSubmit={submit} noValidate className="space-y-4">
|
||||
<FormField
|
||||
id="reset-password"
|
||||
label={t('auth.password')}
|
||||
hint={t('auth.password_hint').replace('{min}', String(PASSWORD_MIN_LENGTH))}
|
||||
error={errors.password && t(errors.password)}
|
||||
>
|
||||
<PasswordField
|
||||
id="reset-password"
|
||||
autoComplete="new-password"
|
||||
value={values.password}
|
||||
onChange={(e) => setValues({ ...values, password: e.target.value })}
|
||||
aria-invalid={Boolean(errors.password)}
|
||||
aria-describedby={fieldDescribedBy('reset-password', 'hint', errors.password)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
<FormField
|
||||
id="reset-password-confirm"
|
||||
label={t('auth.password_confirm')}
|
||||
error={errors.passwordConfirm && t(errors.passwordConfirm)}
|
||||
>
|
||||
<PasswordField
|
||||
id="reset-password-confirm"
|
||||
autoComplete="new-password"
|
||||
value={values.passwordConfirm}
|
||||
onChange={(e) => setValues({ ...values, passwordConfirm: e.target.value })}
|
||||
aria-invalid={Boolean(errors.passwordConfirm)}
|
||||
aria-describedby={fieldDescribedBy('reset-password-confirm', undefined, errors.passwordConfirm)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<Button type="submit" size="lg" className="w-full" disabled={busy}>
|
||||
{busy ? t('auth.reset.submitting') : t('auth.reset.submit')}
|
||||
</Button>
|
||||
</form>
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { signUpWithPassword } from '@faunosteo/supabase'
|
||||
import AuthLayout from '@/components/auth/AuthLayout'
|
||||
import AuthLink from '@/components/auth/AuthLink'
|
||||
import OAuthButtons from '@/components/auth/OAuthButtons'
|
||||
import PasswordField from '@/components/auth/PasswordField'
|
||||
import UnavailableNotice from '@/components/auth/UnavailableNotice'
|
||||
import Button from '@/components/ui/Button'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { absoluteUrl, AUTH_CALLBACK_PATH } from '@/auth/redirects'
|
||||
import {
|
||||
fieldErrors,
|
||||
PASSWORD_MIN_LENGTH,
|
||||
signupSchema,
|
||||
type FieldErrors,
|
||||
} from '@/auth/validation'
|
||||
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type SignupValues = {
|
||||
displayName: string
|
||||
email: string
|
||||
password: string
|
||||
passwordConfirm: string
|
||||
}
|
||||
|
||||
const EMPTY: SignupValues = {
|
||||
displayName: '',
|
||||
email: '',
|
||||
password: '',
|
||||
passwordConfirm: '',
|
||||
}
|
||||
|
||||
function CheckInbox({ email }: { email: string }) {
|
||||
const t = useT()
|
||||
return (
|
||||
<AuthLayout
|
||||
title={t('auth.signup.check_inbox_title')}
|
||||
footer={
|
||||
<p>
|
||||
<AuthLink to="/login">{t('auth.signup.login_link')}</AuthLink>
|
||||
</p>
|
||||
}
|
||||
>
|
||||
<Notice tone="success">
|
||||
{t('auth.signup.check_inbox_description').replace('{email}', email)}
|
||||
</Notice>
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('auth.signup.check_inbox_spam')}
|
||||
</p>
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
|
||||
export default function SignupRoute() {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const describe = useAuthFailureMessage()
|
||||
const client = getSupabaseClient()
|
||||
const [values, setValues] = useState<SignupValues>(EMPTY)
|
||||
const [errors, setErrors] = useState<FieldErrors<SignupValues>>({})
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [sentTo, setSentTo] = useState<string | null>(null)
|
||||
|
||||
function field<K extends keyof SignupValues>(key: K) {
|
||||
return (e: React.ChangeEvent<HTMLInputElement>) =>
|
||||
setValues((v) => ({ ...v, [key]: e.target.value }))
|
||||
}
|
||||
|
||||
async function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
if (!client) return
|
||||
const parsed = signupSchema.safeParse(values)
|
||||
if (!parsed.success) {
|
||||
setErrors(fieldErrors(parsed.error))
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setFailure(null)
|
||||
setBusy(true)
|
||||
try {
|
||||
await signUpWithPassword(client, {
|
||||
email: parsed.data.email,
|
||||
password: parsed.data.password,
|
||||
displayName: parsed.data.displayName,
|
||||
locale,
|
||||
emailRedirectTo: absoluteUrl(AUTH_CALLBACK_PATH),
|
||||
})
|
||||
setSentTo(parsed.data.email)
|
||||
} catch (err) {
|
||||
setFailure(describe(err))
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (sentTo) return <CheckInbox email={sentTo} />
|
||||
|
||||
const footer = (
|
||||
<p>
|
||||
{t('auth.signup.have_account')}{' '}
|
||||
<AuthLink to="/login">{t('auth.signup.login_link')}</AuthLink>
|
||||
</p>
|
||||
)
|
||||
const passwordHint = t('auth.password_hint').replace(
|
||||
'{min}',
|
||||
String(PASSWORD_MIN_LENGTH),
|
||||
)
|
||||
|
||||
return (
|
||||
<AuthLayout
|
||||
title={t('auth.signup.title')}
|
||||
subtitle={t('auth.signup.subtitle')}
|
||||
footer={client ? footer : undefined}
|
||||
>
|
||||
{!client ? (
|
||||
<UnavailableNotice />
|
||||
) : (
|
||||
<>
|
||||
<OAuthButtons />
|
||||
<form onSubmit={submit} noValidate className="space-y-4">
|
||||
<FormField
|
||||
id="signup-name"
|
||||
label={t('auth.signup.display_name')}
|
||||
hint={t('auth.signup.display_name_hint')}
|
||||
error={errors.displayName && t(errors.displayName)}
|
||||
>
|
||||
<Input
|
||||
id="signup-name"
|
||||
autoComplete="name"
|
||||
value={values.displayName}
|
||||
onChange={field('displayName')}
|
||||
aria-invalid={Boolean(errors.displayName)}
|
||||
aria-describedby={fieldDescribedBy('signup-name', 'hint', errors.displayName)}
|
||||
required
|
||||
maxLength={80}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField
|
||||
id="signup-email"
|
||||
label={t('auth.email')}
|
||||
error={errors.email && t(errors.email)}
|
||||
>
|
||||
<Input
|
||||
id="signup-email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
inputMode="email"
|
||||
value={values.email}
|
||||
onChange={field('email')}
|
||||
aria-invalid={Boolean(errors.email)}
|
||||
aria-describedby={fieldDescribedBy('signup-email', undefined, errors.email)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
<FormField
|
||||
id="signup-password"
|
||||
label={t('auth.password')}
|
||||
hint={passwordHint}
|
||||
error={errors.password && t(errors.password)}
|
||||
>
|
||||
<PasswordField
|
||||
id="signup-password"
|
||||
autoComplete="new-password"
|
||||
value={values.password}
|
||||
onChange={field('password')}
|
||||
aria-invalid={Boolean(errors.password)}
|
||||
aria-describedby={fieldDescribedBy('signup-password', 'hint', errors.password)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
<FormField
|
||||
id="signup-password-confirm"
|
||||
label={t('auth.password_confirm')}
|
||||
error={errors.passwordConfirm && t(errors.passwordConfirm)}
|
||||
>
|
||||
<PasswordField
|
||||
id="signup-password-confirm"
|
||||
autoComplete="new-password"
|
||||
value={values.passwordConfirm}
|
||||
onChange={field('passwordConfirm')}
|
||||
aria-invalid={Boolean(errors.passwordConfirm)}
|
||||
aria-describedby={fieldDescribedBy('signup-password-confirm', undefined, errors.passwordConfirm)}
|
||||
required
|
||||
/>
|
||||
</FormField>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<Button type="submit" size="lg" className="w-full" disabled={busy}>
|
||||
{busy ? t('auth.signup.submitting') : t('auth.signup.submit')}
|
||||
</Button>
|
||||
</form>
|
||||
</>
|
||||
)}
|
||||
</AuthLayout>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user