managarten/games/figgos/apps/mobile/utils/AuthContext.tsx
Till-JS 05d074c57e 🔧 refactor(figgos): restructure to standard monorepo pattern
Migrate figgos from single Expo app to multi-app monorepo structure:
- Move mobile app to apps/mobile/
- Add apps/web/ (SvelteKit) and apps/backend/ (NestJS) scaffolds
- Add packages/shared/ for shared types and constants
- Update root package.json with new dev commands
- Temporarily skip type-check (run pnpm install first)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 17:27:15 +01:00

89 lines
2.3 KiB
TypeScript

import React, { createContext, useState, useEffect, useContext } from 'react';
import { Session, User } from '@supabase/supabase-js';
import { supabase } from './supabase';
type AuthContextType = {
user: User | null;
session: Session | null;
loading: boolean;
signUp: (email: string, password: string) => Promise<{ error: any }>;
signIn: (email: string, password: string) => Promise<{ error: any }>;
signOut: () => Promise<{ error: any }>;
};
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [user, setUser] = useState<User | null>(null);
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Lade den aktuellen Benutzer beim Start
const loadUser = async () => {
const { data } = await supabase.auth.getSession();
setSession(data.session);
setUser(data.session?.user || null);
setLoading(false);
// Abonniere Authentifizierungsänderungen
const { data: authListener } = supabase.auth.onAuthStateChange(async (event, session) => {
setSession(session);
setUser(session?.user || null);
setLoading(false);
});
return () => {
authListener.subscription.unsubscribe();
};
};
loadUser();
}, []);
const signUp = async (email: string, password: string) => {
try {
const { error } = await supabase.auth.signUp({ email, password });
return { error };
} catch (error) {
return { error };
}
};
const signIn = async (email: string, password: string) => {
try {
const { error } = await supabase.auth.signInWithPassword({ email, password });
return { error };
} catch (error) {
return { error };
}
};
const signOut = async () => {
try {
const { error } = await supabase.auth.signOut();
return { error };
} catch (error) {
return { error };
}
};
const value = {
user,
session,
loading,
signUp,
signIn,
signOut,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
};
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};