Skip to main content
Glama
useAuth.ts•2.94 kB
import { useState, useEffect } from 'react' import { User, Session } from '@supabase/supabase-js' import { supabase } from '../lib/supabase' import type { Tables } from '../lib/supabase' interface Profile extends Tables<'profiles'> {} export function useAuth() { const [user, setUser] = useState<User | null>(null) const [profile, setProfile] = useState<Profile | null>(null) const [session, setSession] = useState<Session | null>(null) const [loading, setLoading] = useState(true) useEffect(() => { // Get initial session supabase.auth.getSession().then(({ data: { session } }) => { setSession(session) setUser(session?.user ?? null) if (session?.user) { fetchProfile(session.user.id) } setLoading(false) }) // Listen for auth changes const { data: { subscription }, } = supabase.auth.onAuthStateChange(async (event, session) => { setSession(session) setUser(session?.user ?? null) if (session?.user) { await fetchProfile(session.user.id) } else { setProfile(null) } setLoading(false) }) return () => subscription.unsubscribe() }, []) const fetchProfile = async (userId: string) => { try { const { data, error } = await supabase .from('profiles') .select('*') .eq('id', userId) .single() if (error) { console.error('Error fetching profile:', error) return } setProfile(data) } catch (error) { console.error('Error fetching profile:', error) } } const signIn = async (email: string, password: string) => { const { data, error } = await supabase.auth.signInWithPassword({ email, password, }) return { data, error } } const signUp = async (email: string, password: string, profile: Partial<Profile>) => { const { data, error } = await supabase.auth.signUp({ email, password, options: { data: profile } }) return { data, error } } const signInWithGoogle = async () => { const { data, error } = await supabase.auth.signInWithOAuth({ provider: 'google', options: { redirectTo: `${window.location.origin}/auth/callback` } }) return { data, error } } const signOut = async () => { const { error } = await supabase.auth.signOut() return { error } } const updateProfile = async (updates: Partial<Profile>) => { if (!user) return { error: new Error('No user logged in') } const { data, error } = await supabase .from('profiles') .update(updates) .eq('id', user.id) .select() .single() if (!error && data) { setProfile(data) } return { data, error } } return { user, profile, session, loading, signIn, signUp, signInWithGoogle, signOut, updateProfile, } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ChiragPatankar/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server