This commit is contained in:
payacom
2026-07-11 21:38:41 +03:30
parent 402f199e58
commit d83e91dd2d
11 changed files with 79 additions and 5 deletions

View File

@@ -0,0 +1,34 @@
"use client";
import { useCallback } from "react";
import { useRouter } from "next/navigation";
import toast from "react-hot-toast";
import { isAuthenticated } from "@/lib/auth/session";
/**
* گارد احراز هویت برای عملیات‌ها (لایک، کامنت، فالو، ارسال پیام، درخواست همکاری و ...).
*
* کاربر مهمان می‌تواند آزادانه صفحات را ببیند؛ فقط زمانی که بخواهد عملیاتی انجام دهد،
* پیام کوتاهی نمایش داده و به صفحه ورود با آدرس بازگشت (redirect) هدایت می‌شود.
*
* خروجی `true` یعنی کاربر وارد شده و می‌توان عملیات را ادامه داد.
*/
export function useRequireAuth() {
const router = useRouter();
return useCallback(
(message: string | null = "برای انجام این کار وارد حساب کاربری خود شوید"): boolean => {
if (isAuthenticated()) return true;
if (message) toast.error(message);
const current =
typeof window !== "undefined"
? window.location.pathname + window.location.search
: "/";
router.push(`/login?redirect=${encodeURIComponent(current)}`);
return false;
},
[router]
);
}