Spaces:
Running
Running
File size: 1,131 Bytes
26a0c00 3a376b8 26a0c00 3a376b8 26a0c00 3a376b8 26a0c00 12c3446 3a376b8 12c3446 3a376b8 12c3446 3a376b8 26a0c00 3a376b8 26a0c00 3a376b8 26a0c00 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | "use client";
import React, { useEffect } from "react";
import { useAuthStore } from "@/store/auth-store";
export function AuthProvider({ children }: { children: React.ReactNode }) {
const initializeAuth = useAuthStore((state) => state.initializeAuth);
const syncTokensRefreshed = useAuthStore((state) => state.syncTokensRefreshed);
const syncLoggedOut = useAuthStore((state) => state.syncLoggedOut);
useEffect(() => {
void initializeAuth();
}, [initializeAuth]);
useEffect(() => {
const handleTokensRefreshed = (e: Event) => {
syncTokensRefreshed((e as CustomEvent).detail);
};
const handleLoggedOut = () => {
syncLoggedOut();
};
window.addEventListener("auth:tokens-refreshed", handleTokensRefreshed);
window.addEventListener("auth:logged-out", handleLoggedOut);
return () => {
window.removeEventListener("auth:tokens-refreshed", handleTokensRefreshed);
window.removeEventListener("auth:logged-out", handleLoggedOut);
};
}, [syncLoggedOut, syncTokensRefreshed]);
return <>{children}</>;
}
export function useAuth() {
return useAuthStore();
}
|