2023-06-15 12:52:46 +03:00
|
|
|
"use client";
|
2023-05-24 17:17:08 +03:00
|
|
|
|
2023-06-15 12:52:46 +03:00
|
|
|
import {
|
|
|
|
createBrowserSupabaseClient,
|
|
|
|
Session,
|
|
|
|
} from "@supabase/auth-helpers-nextjs";
|
|
|
|
import { useRouter } from "next/navigation";
|
2023-07-05 10:30:22 +03:00
|
|
|
import { createContext, useEffect, useState } from "react";
|
2023-05-24 17:17:08 +03:00
|
|
|
|
2023-07-05 10:30:22 +03:00
|
|
|
import { SupabaseContextType } from "./types";
|
2023-05-24 17:17:08 +03:00
|
|
|
|
2023-07-05 10:30:22 +03:00
|
|
|
export const SupabaseContext = createContext<SupabaseContextType | undefined>(
|
|
|
|
undefined
|
|
|
|
);
|
2023-05-24 17:17:08 +03:00
|
|
|
|
2023-06-20 17:17:13 +03:00
|
|
|
export const SupabaseProvider = ({
|
2023-05-24 17:17:08 +03:00
|
|
|
children,
|
|
|
|
session,
|
|
|
|
}: {
|
2023-06-15 12:52:46 +03:00
|
|
|
children: React.ReactNode;
|
2023-07-05 10:30:22 +03:00
|
|
|
session: Session | null;
|
2023-06-15 12:52:46 +03:00
|
|
|
}): JSX.Element => {
|
|
|
|
const [supabase] = useState(() => createBrowserSupabaseClient());
|
|
|
|
const router = useRouter();
|
2023-05-24 17:17:08 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const {
|
|
|
|
data: { subscription },
|
|
|
|
} = supabase.auth.onAuthStateChange(() => {
|
2023-06-15 12:52:46 +03:00
|
|
|
router.refresh();
|
|
|
|
});
|
2023-05-24 17:17:08 +03:00
|
|
|
|
|
|
|
return () => {
|
2023-06-15 12:52:46 +03:00
|
|
|
subscription.unsubscribe();
|
|
|
|
};
|
|
|
|
}, [router, supabase]);
|
2023-05-24 17:17:08 +03:00
|
|
|
|
|
|
|
return (
|
2023-07-05 10:30:22 +03:00
|
|
|
<SupabaseContext.Provider value={{ supabase, session }}>
|
2023-05-24 17:17:08 +03:00
|
|
|
<>{children}</>
|
2023-07-05 10:30:22 +03:00
|
|
|
</SupabaseContext.Provider>
|
2023-06-15 12:52:46 +03:00
|
|
|
);
|
|
|
|
};
|