136 lines
3.3 KiB
TypeScript
136 lines
3.3 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { User, Session, AuthChangeEvent } from '@supabase/supabase-js';
|
|
import { getSupabase } from '@/lib/supabase/client';
|
|
import type { Database } from '@/lib/supabase/types';
|
|
|
|
type Profile = Database['public']['Tables']['profiles']['Row'];
|
|
|
|
export function useSupabaseAuth() {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [session, setSession] = useState<Session | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const supabase = getSupabase();
|
|
|
|
// Get initial session
|
|
const getInitialSession = async () => {
|
|
const { data } = await supabase.auth.getSession();
|
|
setSession(data.session);
|
|
setUser(data.session?.user ?? null);
|
|
setLoading(false);
|
|
};
|
|
|
|
getInitialSession();
|
|
|
|
// Listen for auth changes
|
|
const {
|
|
data: { subscription },
|
|
} = supabase.auth.onAuthStateChange((_event: AuthChangeEvent, session: Session | null) => {
|
|
setSession(session);
|
|
setUser(session?.user ?? null);
|
|
setLoading(false);
|
|
});
|
|
|
|
return () => subscription.unsubscribe();
|
|
}, []);
|
|
|
|
const signIn = async (email: string, password: string) => {
|
|
const supabase = getSupabase();
|
|
const { data, error } = await supabase.auth.signInWithPassword({
|
|
email,
|
|
password,
|
|
});
|
|
return { data, error };
|
|
};
|
|
|
|
const signUp = async (email: string, password: string) => {
|
|
const supabase = getSupabase();
|
|
const { data, error } = await supabase.auth.signUp({
|
|
email,
|
|
password,
|
|
});
|
|
return { data, error };
|
|
};
|
|
|
|
const signInWithOAuth = async (provider: 'github' | 'google' | 'discord') => {
|
|
const supabase = getSupabase();
|
|
const { data, error } = await supabase.auth.signInWithOAuth({
|
|
provider,
|
|
options: {
|
|
redirectTo: `${window.location.origin}/auth/callback`,
|
|
},
|
|
});
|
|
return { data, error };
|
|
};
|
|
|
|
const signOut = async () => {
|
|
const supabase = getSupabase();
|
|
const { error } = await supabase.auth.signOut();
|
|
return { error };
|
|
};
|
|
|
|
return {
|
|
user,
|
|
session,
|
|
loading,
|
|
signIn,
|
|
signUp,
|
|
signInWithOAuth,
|
|
signOut,
|
|
};
|
|
}
|
|
|
|
export function useProfile() {
|
|
const { user } = useSupabaseAuth();
|
|
const [profile, setProfile] = useState<Profile | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
if (!user) {
|
|
setProfile(null);
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
const fetchProfile = async () => {
|
|
const supabase = getSupabase();
|
|
const { data, error } = await supabase
|
|
.from('profiles')
|
|
.select('*')
|
|
.eq('id', user.id)
|
|
.single();
|
|
|
|
if (error) {
|
|
console.error('Error fetching profile:', error);
|
|
} else {
|
|
setProfile(data);
|
|
}
|
|
setLoading(false);
|
|
};
|
|
|
|
fetchProfile();
|
|
}, [user]);
|
|
|
|
const updateProfile = async (updates: Partial<Profile>) => {
|
|
if (!user) return { error: new Error('Not authenticated') };
|
|
|
|
const supabase = getSupabase();
|
|
const { data, error } = await supabase
|
|
.from('profiles')
|
|
.update({ ...updates, updated_at: new Date().toISOString() })
|
|
.eq('id', user.id)
|
|
.select()
|
|
.single();
|
|
|
|
if (!error && data) {
|
|
setProfile(data);
|
|
}
|
|
|
|
return { data, error };
|
|
};
|
|
|
|
return { profile, loading, updateProfile };
|
|
}
|