# Sign in with Apple 디버깅 가이드 ## 🔧 수정 사항 ### 1. Info.plist에 URL Scheme 추가 ✅ ```xml <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLName</key> <string>auth</string> <key>CFBundleURLSchemes</key> <array> <string>com.lemonaa.moni</string> </array> </dict> </array> ``` ### 2. moniApp.swift에 URL 핸들러 추가 ✅ ```swift .onOpenURL { url in // Deep link 처리 (Sign in with Apple 콜백 포함) Task { do { try await SupabaseConfig.shared.client.auth.session(from: url) await authService.checkSession() } catch { print("❌ URL 처리 실패: \(error.localizedDescription)") } } } ``` ### 3. AuthService.swift에 디버깅 로그 추가 ✅ - 각 단계마다 로그를 출력하여 어디서 실패하는지 확인 가능 ### 4. LoginView.swift에 에러 핸들링 개선 ✅ - 사용자가 취소한 경우는 에러를 표시하지 않음 - 상세한 에러 메시지 출력 --- ## 🧪 테스트 방법 ### 시뮬레이터에서 테스트 1. **시뮬레이터 Apple ID 로그인 확인** - 시뮬레이터 Settings → Apple ID - 로그인되어 있지 않으면 Apple ID로 로그인 필요 2. **Xcode 콘솔 로그 확인** - Xcode 하단의 Console을 열어둔 상태로 테스트 - 다음과 같은 로그가 순서대로 나타나야 함: ``` 🔐 Nonce 생성 및 설정 완료 🍎 Apple 로그인 콜백 받음 ✅ Apple 로그인 성공, authorization 받음 🍎 Sign in with Apple 시작 ✅ Apple credential 받음: [USER_ID] ✅ Identity token 받음 (길이: [TOKEN_LENGTH]) 📧 이메일: [EMAIL 또는 없음] 🔐 Nonce: [NONCE_STRING] 🔄 Supabase로 Apple 로그인 시도 (nonce 포함)... ✅ Supabase 로그인 성공! User ID: [UUID] ``` 3. **앱 실행** ```bash # Xcode에서 실행하거나 터미널에서: cd /Users/daeseong/Desktop/moni xcodebuild -workspace moni.xcodeproj/project.xcworkspace -scheme moni -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 15' ``` 4. **Sign in with Apple 버튼 클릭** - Face ID/Touch ID 프롬프트가 나타남 - 또는 비밀번호 입력 요청 5. **로그 확인** - 위의 로그 순서대로 출력되는지 확인 - 에러가 발생하면 어디서 멈추는지 확인 --- ## 🐛 문제 해결 체크리스트 ### ❌ 문제: "Unacceptable audience in id_token" 에러 ⚠️ 중요! **원인**: Apple ID token의 audience가 앱의 Bundle ID(`com.lemonaa.moni`)인데, Supabase는 nonce를 기대함 **해결**: ✅ **이미 수정됨!** - AuthService에 nonce 생성 로직 추가 - `prepareAppleSignInRequest()` 메서드에서 nonce를 생성하고 SHA256 해시를 Apple 요청에 포함 - `signInWithApple()` 메서드에서 원본 nonce를 Supabase에 전달 **설명**: ```swift // 1. 로그인 요청 시 nonce 생성 및 SHA256 해시 전달 func prepareAppleSignInRequest(_ request: ASAuthorizationAppleIDRequest) { let nonce = randomNonceString() currentNonce = nonce request.nonce = sha256(nonce) // Apple에는 해시된 nonce 전달 } // 2. 로그인 완료 후 원본 nonce를 Supabase에 전달 let session = try await supabase.auth.signInWithIdToken( credentials: .init( provider: .apple, idToken: tokenString, nonce: nonce // Supabase에는 원본 nonce 전달 ) ) ``` ### ❌ 문제: "Invalid Client" 에러 **원인**: Supabase에 설정된 Client ID가 잘못됨 **해결**: 1. Supabase Dashboard → Authentication → Providers → Apple 2. Client IDs 확인: `com.lemonaa.moni.supabase` 3. Apple Developer Console → Identifiers → Services IDs에서 동일한 ID 확인 ### ❌ 문제: "Invalid Token" 에러 **원인**: Supabase에 설정된 Private Key가 잘못됨 **해결**: 1. Supabase Dashboard → Authentication → Providers → Apple 2. Private Key, Team ID, Key ID가 올바르게 입력되었는지 확인 3. Private Key는 다음 형식이어야 함: ``` -----BEGIN PRIVATE KEY----- MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg... -----END PRIVATE KEY----- ``` ### ❌ 문제: 로그인 후 리다이렉트 안됨 **원인**: URL Scheme이 올바르게 설정되지 않음 **해결**: 1. ✅ Info.plist에 CFBundleURLTypes가 추가되었는지 확인 (이미 수정됨) 2. ✅ moniApp.swift에 .onOpenURL이 추가되었는지 확인 (이미 수정됨) 3. Xcode → Target → Info → URL Types 확인: - Identifier: `auth` - URL Schemes: `com.lemonaa.moni` - Role: `Editor` ### ❌ 문제: "The operation couldn't be completed" 에러 **원인**: Capability가 올바르게 설정되지 않음 **해결**: 1. Xcode → Target → Signing & Capabilities 2. "Sign in with Apple" Capability가 추가되어 있는지 확인 3. moni.entitlements 파일에 다음이 있는지 확인: ```xml <key>com.apple.developer.applesignin</key> <array> <string>Default</string> </array> ``` ### ❌ 문제: 시뮬레이터에서 "Sign in with Apple 사용 불가" **원인**: 시뮬레이터에 Apple ID가 로그인되어 있지 않음 **해결**: 1. 시뮬레이터 열기 2. Settings → [화면 상단 Apple ID] 클릭 3. 실제 Apple ID로 로그인 4. Two-Factor Authentication 완료 ### ❌ 문제: "User profile not found" 에러 **원인**: Supabase users 테이블에 사용자가 생성되지 않음 **해결**: 1. Supabase Dashboard → Table Editor → users 테이블 확인 2. AuthService의 로그 확인: - "🔄 새 사용자 프로필 생성 중..." 로그가 나타나는지 - "✅ 사용자 프로필 생성 완료" 로그가 나타나는지 3. 에러가 발생하면 users 테이블 스키마 확인 --- ## 📋 Supabase 설정 확인 ### 1. Apple Provider 설정 Supabase Dashboard → Authentication → Providers → Apple: ``` ✅ Enable Sign in with Apple: ON ✅ Client IDs: com.lemonaa.moni.supabase ✅ Team ID: [YOUR_10_DIGIT_TEAM_ID] ✅ Key ID: [YOUR_10_DIGIT_KEY_ID] ✅ Private Key: -----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY----- ``` ### 2. Redirect URLs 설정 Supabase Dashboard → Authentication → URL Configuration: ``` ✅ Site URL: com.lemonaa.moni:// ✅ Redirect URLs: - com.lemonaa.moni:// - com.lemonaa.moni://* - com.lemonaa.moni://login-callback ``` ### 3. Apple Developer Console 설정 [Apple Developer Console](https://developer.apple.com/) → Identifiers → Services IDs → `com.lemonaa.moni.supabase`: ``` ✅ Sign in with Apple: Enabled ✅ Domains and Subdomains: qptsmsyfzbbqqdcyqgeu.supabase.co ✅ Return URLs: https://qptsmsyfzbbqqdcyqgeu.supabase.co/auth/v1/callback ``` --- ## 🔍 실시간 디버깅 팁 ### Xcode Console에서 로그 필터링 1. Console 하단의 검색창에 다음 키워드 입력: - `🍎` - Apple 로그인 관련 로그만 보기 - `❌` - 에러 로그만 보기 - `✅` - 성공 로그만 보기 - `Supabase` - Supabase 관련 로그만 보기 ### 네트워크 요청 확인 Supabase API 호출이 성공하는지 확인하려면: 1. Xcode → Product → Scheme → Edit Scheme 2. Run → Arguments → Environment Variables 추가: - Name: `DEBUG_SUPABASE` - Value: `1` ### 실제 기기에서 테스트 시뮬레이터에서 문제가 계속되면 실제 기기에서 테스트: 1. iPhone/iPad를 Mac에 연결 2. Xcode에서 Target Device를 실제 기기로 선택 3. Run (Cmd+R) 4. 실제 기기의 Apple ID로 로그인 테스트 --- ## 📱 프로덕션 체크리스트 배포 전 확인 사항: - [ ] Apple Developer Console에서 Production Services ID 설정 완료 - [ ] Supabase에서 Production 프로젝트 설정 완료 - [ ] Info.plist의 URL Scheme이 올바른지 확인 - [ ] moni.entitlements에 Sign in with Apple capability 있음 - [ ] 실제 기기에서 로그인 테스트 완료 - [ ] 새 사용자 생성 및 기존 사용자 로그인 둘 다 테스트 완료 - [ ] Privacy Policy에 Apple Sign In 언급 - [ ] App Store 스크린샷에 Apple Sign In 버튼 표시 --- ## 💬 도움이 필요하면 1. **Xcode Console 로그 전체 복사** - 로그인 시도 시 나타나는 모든 로그를 복사 - 특히 ❌ 표시가 있는 에러 로그 확인 2. **Supabase Dashboard 확인** - Authentication → Users에서 사용자가 생성되었는지 확인 - Logs에서 API 호출 실패 로그 확인 3. **Apple Developer Console 확인** - Services ID 설정이 올바른지 확인 - Return URLs가 Supabase Callback URL과 일치하는지 확인 --- ## 🎯 예상되는 성공 시나리오 모든 것이 올바르게 설정되었다면: 1. **사용자가 "Sign in with Apple" 버튼 클릭** 2. **Nonce 생성 및 SHA256 해시가 Apple 요청에 포함됨** 3. **Face ID/Touch ID 프롬프트 또는 비밀번호 입력** 4. **Apple에서 인증 완료** 5. **Xcode Console에 순차적인 로그 출력**: - 🔐 Nonce 생성 및 설정 완료 - 🍎 Apple 로그인 콜백 받음 - ✅ Apple credential 받음 - ✅ Identity token 받음 - 🔐 Nonce: [랜덤 문자열] - 🔄 Supabase로 Apple 로그인 시도 (nonce 포함)... - ✅ Supabase 로그인 성공! - ✅ 사용자 프로필 생성/로드 완료 6. **앱이 자동으로 메인 화면(Dashboard)으로 이동** 7. **Supabase Dashboard → Users에 새 사용자 추가됨** --- ## 🔗 참고 자료 - [Apple Sign in Documentation](https://developer.apple.com/sign-in-with-apple/) - [Supabase Auth - Apple](https://supabase.com/docs/guides/auth/social-login/auth-apple) - [Supabase Swift Client](https://github.com/supabase-community/supabase-swift)