Google 로그인을 앱에 통합하여 사용자가 Google 계정을 사용하여 Firebase에 인증하도록 할 수 있습니다.
시작하기 전에
- Apple 프로젝트에 Firebase를 추가합니다 .
Podfile
에 다음 포드를 포함합니다.pod 'FirebaseAuth' pod 'GoogleSignIn'
- 앱을 Firebase 프로젝트에 아직 연결하지 않았다면 Firebase 콘솔 에서 연결하세요.
- Firebase 콘솔에서 로그인 방법으로 Google을 사용하도록 설정합니다.
- Firebase 콘솔 에서 인증 섹션을 엽니다.
- 로그인 방법 탭에서 Google 로그인 방법을 활성화하고 저장 을 클릭합니다.
1. 필요한 헤더 파일 가져오기
먼저 Firebase SDK 및 Google 로그인 SDK 헤더 파일을 앱으로 가져와야 합니다.
빠른
import FirebaseCore import GoogleSignIn
목표-C
@import FirebaseCore; @import GoogleSignIn;
2. 구글 로그인 구현
다음 단계에 따라 Google 로그인을 구현하십시오. iOS에서 Google 로그인을 사용하는 방법에 대한 자세한 내용은 Google 로그인 개발자 문서 를 참조하세요.
- Xcode 프로젝트에 사용자 지정 URL 체계를 추가합니다.
- 프로젝트 구성 열기: 왼쪽 트리 보기에서 프로젝트 이름을 두 번 클릭합니다. 대상 섹션에서 앱 을 선택한 다음 정보 탭을 선택하고 URL 유형 섹션을 확장합니다.
- + 버튼을 클릭하고 반전된 클라이언트 ID에 대한 URL 체계를 추가합니다. 이 값을 찾으려면
구성 파일을 열고GoogleService-Info.plist REVERSED_CLIENT_ID
키를 찾으십시오. 해당 키의 값을 복사하여 구성 페이지의 URL 체계 상자에 붙여넣습니다. 다른 필드는 비워 둡니다.완료되면 구성은 다음과 유사하게 표시되어야 합니다(단, 애플리케이션별 값 포함).
- 앱 대리자의
application:didFinishLaunchingWithOptions:
메서드에서FirebaseApp
개체를 구성합니다.빠른
// Use Firebase library to configure APIs FirebaseApp.configure()
목표-C
// Use Firebase library to configure APIs [FIRApp configure];
- 앱 델리게이트의
application:openURL:options:
메소드를 구현하십시오. 이 메서드는GIDSignIn
인스턴스의handleURL
메서드를 호출해야 합니다. 이 메서드는 인증 프로세스가 끝날 때 애플리케이션이 받는 URL을 적절하게 처리합니다.빠른
@available(iOS 9.0, *) func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool { return GIDSignIn.sharedInstance.handle(url) }
목표-C
- (BOOL)application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *, id> *)options { return [[GIDSignIn sharedInstance] handleURL:url]; }
- 앱의 프레젠테이션 보기 컨트롤러 및 클라이언트 ID를 Google 로그인 로그인 방법에 전달하고 결과 Google 인증 토큰에서 Firebase 인증 자격 증명을 만듭니다.
빠른
guard let clientID = FirebaseApp.app()?.options.clientID else { return } // Create Google Sign In configuration object. let config = GIDConfiguration(clientID: clientID) // Start the sign in flow! GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in if let error = error { // ... return } guard let authentication = user?.authentication, let idToken = authentication.idToken else { return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken) // ... }
목표-C
GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:[FIRApp defaultApp].options.clientID]; [GIDSignIn.sharedInstance setConfiguration:config]; __weak __auto_type weakSelf = self; [GIDSignIn.sharedInstance signInWithPresentingViewController:self completion:^(GIDSignInResult * _Nullable result, NSError * _Nullable error) { __auto_type strongSelf = weakSelf; if (strongSelf == nil) { return; } if (error == nil) { FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:result.user.idToken.tokenString accessToken:result.user.accessToken.tokenString]; // ... } else { // ... } }];
-
GIDSignInButton
을 스토리보드, XIB 파일에 추가하거나 프로그래밍 방식으로 인스턴스화합니다. 스토리보드나 XIB 파일에 버튼을 추가하려면 뷰를 추가하고 커스텀 클래스를GIDSignInButton
으로 설정하세요. - 선택 사항 : 버튼을 사용자 지정하려면 다음을 수행합니다.
빠른
- 보기 컨트롤러에서 로그인 버튼을 속성으로 선언합니다.
@IBOutlet weak var signInButton: GIDSignInButton!
- 방금 선언한
signInButton
속성에 버튼을 연결합니다. - GIDSignInButton 개체의 속성을 설정하여 버튼을 사용자 지정합니다.
목표-C
- 보기 컨트롤러의 헤더 파일에서 로그인 버튼을 속성으로 선언합니다.
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
- 방금 선언한
signInButton
속성에 버튼을 연결합니다. - GIDSignInButton 개체의 속성을 설정하여 버튼을 사용자 지정합니다.
- 보기 컨트롤러에서 로그인 버튼을 속성으로 선언합니다.
3. Firebase로 인증
마지막으로 이전 단계에서 만든 인증 자격 증명으로 Firebase 로그인 프로세스를 완료합니다.
빠른
Auth.auth().signIn(with: credential) { authResult, error in if let error = error { let authError = error as NSError if isMFAEnabled, authError.code == AuthErrorCode.secondFactorRequired.rawValue { // The user is a multi-factor user. Second factor challenge is required. let resolver = authError .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver var displayNameString = "" for tmpFactorInfo in resolver.hints { displayNameString += tmpFactorInfo.displayName ?? "" displayNameString += " " } self.showTextInputPrompt( withMessage: "Select factor to sign in\n\(displayNameString)", completionBlock: { userPressedOK, displayName in var selectedHint: PhoneMultiFactorInfo? for tmpFactorInfo in resolver.hints { if displayName == tmpFactorInfo.displayName { selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo } } PhoneAuthProvider.provider() .verifyPhoneNumber(with: selectedHint!, uiDelegate: nil, multiFactorSession: resolver .session) { verificationID, error in if error != nil { print( "Multi factor start sign in failed. Error: \(error.debugDescription)" ) } else { self.showTextInputPrompt( withMessage: "Verification code for \(selectedHint?.displayName ?? "")", completionBlock: { userPressedOK, verificationCode in let credential: PhoneAuthCredential? = PhoneAuthProvider.provider() .credential(withVerificationID: verificationID!, verificationCode: verificationCode!) let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator .assertion(with: credential!) resolver.resolveSignIn(with: assertion!) { authResult, error in if error != nil { print( "Multi factor finanlize sign in failed. Error: \(error.debugDescription)" ) } else { self.navigationController?.popViewController(animated: true) } } } ) } } } ) } else { self.showMessagePrompt(error.localizedDescription) return } // ... return } // User is signed in // ... }
목표-C
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (isMFAEnabled && error && error.code == FIRAuthErrorCodeSecondFactorRequired) { FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; NSMutableString *displayNameString = [NSMutableString string]; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { [displayNameString appendString:tmpFactorInfo.displayName]; [displayNameString appendString:@" "]; } [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString] completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) { FIRPhoneMultiFactorInfo* selectedHint; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { if ([displayName isEqualToString:tmpFactorInfo.displayName]) { selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo; } } [FIRPhoneAuthProvider.provider verifyPhoneNumberWithMultiFactorInfo:selectedHint UIDelegate:nil multiFactorSession:resolver.session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName] completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) { FIRPhoneAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID verificationCode:verificationCode]; FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { NSLog(@"Multi factor finanlize sign in succeeded."); } }]; }]; } }]; }]; } else if (error) { // ... return; } // User successfully signed in. Get user data from the FIRUser object if (authResult == nil) { return; } FIRUser *user = authResult.user; // ... }];
다음 단계
사용자가 처음으로 로그인하면 새 사용자 계정이 생성되고 사용자가 로그인할 때 사용한 자격 증명(즉, 사용자 이름과 암호, 전화 번호 또는 인증 공급자 정보)에 연결됩니다. 이 새 계정은 Firebase 프로젝트의 일부로 저장되며 사용자 로그인 방법에 관계없이 프로젝트의 모든 앱에서 사용자를 식별하는 데 사용할 수 있습니다.
Firebase 실시간 데이터베이스 및 Cloud Storage 보안 규칙 에서
auth
변수에서 로그인한 사용자의 고유한 사용자 ID를 가져오고 이를 사용하여 사용자가 액세스할 수 있는 데이터를 제어할 수 있습니다.
인증 공급자 자격 증명을 기존 사용자 계정에 연결하여 사용자가 여러 인증 공급자를 사용하여 앱에 로그인하도록 허용할 수 있습니다.
사용자를 로그아웃하려면 signOut:
을 호출합니다.
빠른
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) }
목표-C
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }
인증 오류의 전체 범위에 대한 오류 처리 코드를 추가할 수도 있습니다. 오류 처리 를 참조하십시오.