Create authentication context
cgen-6e0c243358784667913d8302fae58080
This commit is contained in:
parent
c9e541741a
commit
981e25bfb8
1 changed files with 201 additions and 0 deletions
201
client/contexts/AuthContext.tsx
Normal file
201
client/contexts/AuthContext.tsx
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
import { User, Session } from '@supabase/supabase-js';
|
||||
import { supabase } from '@/lib/supabase';
|
||||
import { UserProfile } from '@/lib/database.types';
|
||||
import { aethexToast } from '@/lib/aethex-toast';
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
profile: UserProfile | null;
|
||||
session: Session | null;
|
||||
loading: boolean;
|
||||
signIn: (email: string, password: string) => Promise<void>;
|
||||
signUp: (email: string, password: string, userData?: Partial<UserProfile>) => Promise<void>;
|
||||
signOut: () => Promise<void>;
|
||||
updateProfile: (updates: Partial<UserProfile>) => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useAuth must be used within an AuthProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [profile, setProfile] = useState<UserProfile | 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) {
|
||||
fetchUserProfile(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 fetchUserProfile(session.user.id);
|
||||
} else {
|
||||
setProfile(null);
|
||||
}
|
||||
setLoading(false);
|
||||
|
||||
// Show toast notifications for auth events
|
||||
if (event === 'SIGNED_IN') {
|
||||
aethexToast.success({
|
||||
title: 'Welcome back!',
|
||||
description: 'Successfully signed in to AeThex OS'
|
||||
});
|
||||
} else if (event === 'SIGNED_OUT') {
|
||||
aethexToast.info({
|
||||
title: 'Signed out',
|
||||
description: 'Come back soon!'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return () => subscription.unsubscribe();
|
||||
}, []);
|
||||
|
||||
const fetchUserProfile = async (userId: string) => {
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('user_profiles')
|
||||
.select('*')
|
||||
.eq('id', userId)
|
||||
.single();
|
||||
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
throw error;
|
||||
}
|
||||
|
||||
setProfile(data);
|
||||
} catch (error) {
|
||||
console.error('Error fetching user profile:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const signIn = async (email: string, password: string) => {
|
||||
try {
|
||||
const { error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
} catch (error: any) {
|
||||
aethexToast.error({
|
||||
title: 'Sign in failed',
|
||||
description: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const signUp = async (email: string, password: string, userData?: Partial<UserProfile>) => {
|
||||
try {
|
||||
const { data, error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
if (data.user && userData) {
|
||||
// Create user profile after successful signup
|
||||
const { error: profileError } = await supabase
|
||||
.from('user_profiles')
|
||||
.insert({
|
||||
id: data.user.id,
|
||||
...userData,
|
||||
});
|
||||
|
||||
if (profileError) throw profileError;
|
||||
|
||||
aethexToast.success({
|
||||
title: 'Account created!',
|
||||
description: 'Please check your email to verify your account'
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
aethexToast.error({
|
||||
title: 'Sign up failed',
|
||||
description: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const signOut = async () => {
|
||||
try {
|
||||
const { error } = await supabase.auth.signOut();
|
||||
if (error) throw error;
|
||||
} catch (error: any) {
|
||||
aethexToast.error({
|
||||
title: 'Sign out failed',
|
||||
description: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const updateProfile = async (updates: Partial<UserProfile>) => {
|
||||
if (!user) throw new Error('No user logged in');
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('user_profiles')
|
||||
.update(updates)
|
||||
.eq('id', user.id)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
setProfile(data);
|
||||
aethexToast.success({
|
||||
title: 'Profile updated',
|
||||
description: 'Your profile has been updated successfully'
|
||||
});
|
||||
} catch (error: any) {
|
||||
aethexToast.error({
|
||||
title: 'Update failed',
|
||||
description: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const value = {
|
||||
user,
|
||||
profile,
|
||||
session,
|
||||
loading,
|
||||
signIn,
|
||||
signUp,
|
||||
signOut,
|
||||
updateProfile,
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
Loading…
Reference in a new issue