لینک دعوت هم کلیکپذیر نبود و هم کار نمیکرد؛ چهار نقص همزمان:
- `invite_url` نسبی بود (`/accept-invite?token=…`) و host نداشت → حالا از
`settings.app_url` آدرس مطلق ساخته میشود.
- صفحهٔ `/accept-invite` در اپ Next وجود نداشت → ۴۰۴.
- هیچ اندپوینت پذیرش دعوت در بکاند نبود؛ `InviteRepository.mark_accepted`
نوشته شده بود ولی هرگز صدا زده نمیشد، پس توکن مصرف نمیشد.
- در تنظیمات، لینک بهصورت متن ساده در `<p>` رندر میشد.
بکاند:
- روتر `app/interfaces/http/routers/invites.py`:
`GET /api/invites/{token}` (پیشنمایش) و `POST /api/invites/accept`.
عمداً زیر `/organizations/{org_id}` نیست: دعوتشونده هنوز عضو نیست و
`get_org_context` ردش میکند، پس خود توکن مجوز است. accept ایدمپوتنت است.
- `AuditAction.MEMBER_JOIN = "member.join"`.
- رفع باگ نهفته: `Invite.is_expired()` بین `expires_at` خواندهشده از SQLite
(naive) و `utcnow()` (aware) خطای مقایسه میداد. طبق قرارداد موجود پروژه
(`_aware` در `adapters/agy.py`)، در `repositories/identity.py` تابع `_aware`
اضافه و روی `expires_at`/`accepted_at` اعمال شد.
فرانت:
- صفحهٔ `app/accept-invite/page.tsx` با پیشنمایش سازمان/نقش و پذیرش؛ پس از
پذیرش `setActiveOrganization` میکند تا در همان سازمان لند کند.
- قراردادهای `InvitePreview`/`AcceptInviteResult` و کلاینت
`previewInvite`/`acceptInvite`.
- `login`/`signup` پارامتر `?next=` را میفهمند (فقط same-origin) تا توکن دعوت
در مسیر احراز هویت گم نشود.
- `settings`: لینک به `<a>` واقعی + دکمهٔ «کپی لینک».
تست: `tests/test_invites.py` (۶) و `src/test/accept-invite.test.tsx` (۵).
بکاند ۲۵۲ سبز + ruff/mypy تمیز؛ فرانت ۶۱ سبز + tsc/eslint تمیز.
105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { useRouter } from "next/navigation";
|
|
import * as React from "react";
|
|
import { Button, Card, CardHint, CardTitle, ErrorState, Field } from "@agy/ui";
|
|
import { ApiError } from "@/lib/api";
|
|
import { useSession } from "@/lib/session";
|
|
|
|
/**
|
|
* Where to land after signing up. Only same-origin paths are honoured, so a
|
|
* crafted `?next=https://evil.example` cannot turn the form into an open
|
|
* redirect.
|
|
*/
|
|
function nextDestination(fallback: string): string {
|
|
if (typeof window === "undefined") return fallback;
|
|
const next = new URLSearchParams(window.location.search).get("next");
|
|
return next && next.startsWith("/") && !next.startsWith("//") ? next : fallback;
|
|
}
|
|
|
|
export default function SignupPage() {
|
|
const router = useRouter();
|
|
const { signup } = useSession();
|
|
const [name, setName] = React.useState("");
|
|
const [email, setEmail] = React.useState("");
|
|
const [password, setPassword] = React.useState("");
|
|
const [error, setError] = React.useState<string | null>(null);
|
|
const [submitting, setSubmitting] = React.useState(false);
|
|
// Read once on mount so the "already registered?" link carries the invite token.
|
|
const [nextPath, setNextPath] = React.useState<string | null>(null);
|
|
|
|
React.useEffect(() => {
|
|
const next = new URLSearchParams(window.location.search).get("next");
|
|
setNextPath(next && next.startsWith("/") && !next.startsWith("//") ? next : null);
|
|
}, []);
|
|
|
|
const onSubmit = async (event: React.FormEvent) => {
|
|
event.preventDefault();
|
|
setError(null);
|
|
setSubmitting(true);
|
|
try {
|
|
await signup(email, password, name);
|
|
router.replace(nextDestination("/onboarding"));
|
|
} catch (err) {
|
|
setError(err instanceof ApiError ? err.message : "ثبتنام ناموفق بود.");
|
|
} finally {
|
|
setSubmitting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center p-5">
|
|
<Card className="anim-rise w-full max-w-sm">
|
|
<CardTitle>ساخت حساب</CardTitle>
|
|
<CardHint>پس از ثبتنام، اولین سازمان خود را میسازید.</CardHint>
|
|
|
|
<form onSubmit={onSubmit} className="mt-5 flex flex-col gap-4">
|
|
<Field
|
|
label="نام"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
placeholder="نام شما"
|
|
/>
|
|
<Field
|
|
label="ایمیل"
|
|
type="email"
|
|
dir="ltr"
|
|
autoComplete="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
/>
|
|
<Field
|
|
label="رمز عبور"
|
|
type="password"
|
|
dir="ltr"
|
|
autoComplete="new-password"
|
|
required
|
|
minLength={8}
|
|
hint="حداقل ۸ کاراکتر."
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
/>
|
|
|
|
{error && <ErrorState title="ثبتنام ناموفق" message={error} />}
|
|
|
|
<Button type="submit" loading={submitting}>
|
|
ثبتنام
|
|
</Button>
|
|
</form>
|
|
|
|
<p className="mt-4 text-xs text-slate-400">
|
|
قبلاً ثبتنام کردهاید؟{" "}
|
|
<Link
|
|
href={nextPath ? `/login?next=${encodeURIComponent(nextPath)}` : "/login"}
|
|
className="text-accent hover:underline"
|
|
>
|
|
ورود
|
|
</Link>
|
|
</p>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|