mirror of https://github.com/kortix-ai/suna.git
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { supabase } from '@/constants/SupabaseConfig';
|
|
import { AuthChangeEvent, Session, User } from '@supabase/supabase-js';
|
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
|
|
interface AuthContextType {
|
|
session: Session | null;
|
|
user: User | null;
|
|
loading: boolean;
|
|
signOut: () => Promise<void>;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const [session, setSession] = useState<Session | null>(null);
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
// Get initial session
|
|
supabase.auth.getSession().then(({ data: { session } }: { data: { session: Session | null } }) => {
|
|
setSession(session);
|
|
setUser(session?.user ?? null);
|
|
setLoading(false);
|
|
});
|
|
|
|
// Listen for auth changes
|
|
const { data: { subscription } } = supabase.auth.onAuthStateChange(
|
|
async (event: AuthChangeEvent, session: Session | null) => {
|
|
setSession(session);
|
|
setUser(session?.user ?? null);
|
|
setLoading(false);
|
|
}
|
|
);
|
|
|
|
return () => subscription?.unsubscribe();
|
|
}, []);
|
|
|
|
const signOut = async () => {
|
|
await supabase.auth.signOut();
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ session, user, loading, signOut }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useAuth = (): AuthContextType => {
|
|
const context = useContext(AuthContext);
|
|
if (context === undefined) {
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
}
|
|
return context;
|
|
};
|