Refactor AuthContext into smaller files

Refactors the AuthContext.tsx file into smaller, more manageable files to improve code organization and maintainability. The functionality remains the same.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 06:57:27 +00:00
parent ad597b281d
commit fd34c62170
13 changed files with 244 additions and 228 deletions

View File

@@ -0,0 +1,78 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { supabase } from '@/lib/supabase';
import { Session, User } from '@supabase/supabase-js';
import { toast } from '@/hooks/useToast.wrapper';
import { AuthContextType } from './types';
import * as authActions from './authActions';
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(() => {
// 현재 세션 체크
const getSession = async () => {
try {
const { data, error } = await supabase.auth.getSession();
if (error) {
console.error('세션 로딩 중 오류:', error);
toast({
title: '세션 로드 오류',
description: '사용자 세션을 불러오는 중 문제가 발생했습니다.',
variant: 'destructive',
});
} else {
setSession(data.session);
setUser(data.session?.user ?? null);
}
} catch (error) {
console.error('세션 확인 중 예외 발생:', error);
} finally {
setLoading(false);
}
};
getSession();
// auth 상태 변경 리스너
const { data: { subscription } } = supabase.auth.onAuthStateChange(
async (event, session) => {
console.log('Supabase auth 이벤트:', event);
setSession(session);
setUser(session?.user ?? null);
setLoading(false);
}
);
// 리스너 정리
return () => {
subscription.unsubscribe();
};
}, []);
// 인증 작업 메서드들
const value: AuthContextType = {
session,
user,
loading,
signIn: authActions.signIn,
signUp: authActions.signUp,
signOut: authActions.signOut,
resetPassword: authActions.resetPassword,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
};
export const useAuth = () => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth는 AuthProvider 내부에서 사용해야 합니다');
}
return context;
};