애플로그인 임시
이 작업들은 Apple Developer Program에 유료 등록된 계정 소유자만 수행할 수 있습니다.
Apple Developer 사이트 접속: https://developer.apple.com/account
좌측 메뉴에서 "Identifiers" 선택
"+" 버튼을 눌러 새로운 App ID 생성
App Name은 자유롭게 입력
Bundle ID는 Flutter 프로젝트의 ios/Runner.xcodeproj/project.pbxproj의 PRODUCT_BUNDLE_IDENTIFIER와 일치해야 함
ios/Runner.xcworkspace 열기.xcworkspace, .xcodeproj, .pbxproj, project.pbxproj 등이 포함된 iOS 프로젝트를 개발자에게 전달소유자가 위 작업을 마친 후에는 개발자가 Apple 로그인 기능을 Flutter 앱 내에서 구현하고 테스트할 수 있습니다.
pubspec.yaml에 다음 패키지들을 추가
dependencies:
sign_in_with_apple: ^5.0.0
firebase_auth: ^4.0.0
cloud_firestore: ^4.0.0
flutter pub get
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
Future<AppleIDCredential?> signInWithApple() async {
try {
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
);
return credential;
} catch (e) {
print("Apple 로그인 실패: $e");
return null;
}
}
import 'package:firebase_auth/firebase_auth.dart';
Future<User?> firebaseSignInWithApple(AppleIDCredential credential) async {
final oAuthProvider = OAuthProvider("apple.com");
final authCredential = oAuthProvider.credential(
idToken: credential.identityToken,
accessToken: credential.authorizationCode,
);
final userCredential =
await FirebaseAuth.instance.signInWithCredential(authCredential);
return userCredential.user;
}
import 'package:cloud_firestore/cloud_firestore.dart';
Future<void> saveUserToFirestore(User user, AppleIDCredential appleInfo) async {
final docRef = FirebaseFirestore.instance.collection('users').doc(user.uid);
await docRef.set({
'userId': user.uid,
'email': user.email ?? '',
'name': '${appleInfo.givenName ?? ''} ${appleInfo.familyName ?? ''}'.trim(),
'provider': 'apple',
'photoUrl': user.photoURL ?? '',
'createdAt': FieldValue.serverTimestamp(),
}, SetOptions(merge: true));
}
Future<void> handleAppleSignIn() async {
final appleCredential = await signInWithApple();
if (appleCredential == null) return;
final firebaseUser = await firebaseSignInWithApple(appleCredential);
if (firebaseUser == null) return;
await saveUserToFirestore(firebaseUser, appleCredential);
}
SignInWithAppleButton(
onPressed: () async {
await handleAppleSignIn();
},
style: SignInWithAppleButtonStyle.black,
)
ios/Runner/Info.plist 파일 내 다음 항목이 포함되어 있어야 함
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.companyname.appname</string>
</array>
</dict>
</array>
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}