better-auth/apps/web/modules/shared/components/ConsentProvider.tsx
 Илья Шалонин 8538ab969a first
2025-10-29 13:43:43 +05:00

42 lines
907 B
TypeScript

"use client";
import Cookies from "js-cookie";
import { createContext, useState } from "react";
export const ConsentContext = createContext<{
userHasConsented: boolean;
allowCookies: () => void;
declineCookies: () => void;
}>({
userHasConsented: false,
allowCookies: () => {},
declineCookies: () => {},
});
export function ConsentProvider({
children,
initialConsent,
}: {
children: React.ReactNode;
initialConsent?: boolean;
}) {
const [userHasConsented, setUserHasConsented] = useState(!!initialConsent);
const allowCookies = () => {
Cookies.set("consent", "true", { expires: 30 });
setUserHasConsented(true);
};
const declineCookies = () => {
Cookies.set("consent", "false", { expires: 30 });
setUserHasConsented(false);
};
return (
<ConsentContext.Provider
value={{ userHasConsented, allowCookies, declineCookies }}
>
{children}
</ConsentContext.Provider>
);
}