Apple 플랫폼에서 Firebase 인증 시작하기

Firebase Authentication를 사용하여 사용자가 다음 중 하나를 사용하여 앱에 로그인하도록 허용할 수 있습니다. 이메일 주소와 비밀번호 로그인 등 더 많은 로그인 방법 제휴 ID 공급업체(예: Google 로그인 및 Facebook 로그인) 이 튜토리얼에서 Firebase Authentication를 시작하는 방법을 알아보세요. 이메일 주소와 비밀번호로 앱에 로그인할 수 있습니다.

Firebase에 앱 연결

  1. Firebase SDK를 설치합니다.
  2. Firebase Console에서 Firebase 프로젝트에 앱을 추가합니다.

앱에 Firebase Authentication 추가

Swift Package Manager를 사용해 Firebase 종속 항목을 설치하고 관리하세요.

  1. 앱 프로젝트를 연 상태로 Xcode에서 File(파일) > Add Packages(패키지 추가)로 이동합니다.
  2. 메시지가 표시되면 Firebase Apple 플랫폼 SDK 저장소를 추가합니다.
  3.   https://github.com/firebase/firebase-ios-sdk.git
    드림 <ph type="x-smartling-placeholder">
  4. Firebase Authentication 라이브러리를 선택합니다.
  5. 타겟 빌드 설정의 Other Linker Flags(기타 링커 플래그) 섹션에 -ObjC 플래그를 추가합니다.
  6. 완료되면 Xcode가 백그라운드에서 자동으로 종속 항목을 확인하고 다운로드하기 시작합니다.

(선택사항) Firebase Local Emulator Suite로 프로토타입 제작 및 테스트

앱에서 사용자를 인증하는 방법을 설명하기 전에 Authentication 기능의 프로토타입을 제작하고 테스트하는 데 사용할 수 있는 도구: Firebase Local Emulator Suite입니다. 인증 방법을 결정하는 경우 공개 및 비공개 데이터를 사용하여 다양한 데이터 모델을 시도하고 AuthenticationFirebase Security Rules를 사용하거나 로그인 UI 디자인 프로토타입을 제작하여 작업을 수행하는 것이 매우 좋은 방법입니다.

Authentication 에뮬레이터는 Local Emulator Suite의 일부입니다. 이를 통해 앱은 에뮬레이션된 데이터베이스 콘텐츠 및 구성과 상호작용할 수 있으며, 필요한 경우 에뮬레이션된 프로젝트 리소스 (함수, 기타 데이터베이스, 보안 규칙)

Authentication 에뮬레이터를 사용하려면 몇 단계만 거치면 됩니다.

  1. 에뮬레이터에 연결하려면 앱의 테스트 구성에 코드 줄을 추가합니다.
  2. 로컬 프로젝트 디렉터리의 루트에서 firebase emulators:start를 실행합니다.
  3. 대화형 프로토타입 제작에 Local Emulator Suite UI 사용 또는 비대화형 테스트용 Authentication 에뮬레이터 REST API

자세한 가이드는 Authentication 에뮬레이터에 앱 연결에서 확인할 수 있습니다. 자세한 내용은 Local Emulator Suite 소개를 참고하세요.

이제 사용자 인증 방법을 계속 살펴보겠습니다.

Firebase SDK 초기화

우선 앱 대리자에서 Firebase SDK를 가져옵니다.

Swift

import FirebaseCore

Objective-C

@import FirebaseCore;

그런 다음 application:didFinishLaunchingWithOptions: 메서드에서 FirebaseApp 객체를 초기화합니다.

Swift

// Use Firebase library to configure APIs
FirebaseApp.configure()

Objective-C

// Use Firebase library to configure APIs
[FIRApp configure];

인증 상태 수신 대기

각각의 앱 뷰에서 앱에 로그인한 사용자에 대한 정보를 얻기 위해 FIRAuth 객체와 리스너를 연결합니다. 이 리스너는 사용자의 로그인 상태가 변경될 때마다 호출됩니다.

뷰 컨트롤러의 viewWillAppear 메서드에서 리스너를 연결합니다.

Swift

handle = Auth.auth().addStateDidChangeListener { auth, user in
  // ...
}

Objective-C

self.handle = [[FIRAuth auth]
    addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
      // ...
    }];

뷰 컨트롤러의 viewWillDisappear 메서드에서 리스너를 분리합니다.

Swift

Auth.auth().removeStateDidChangeListener(handle!)

Objective-C

[[FIRAuth auth] removeAuthStateDidChangeListener:_handle];

신규 사용자 가입

신규 사용자가 자신의 이메일 주소와 비밀번호를 사용해 앱에 가입할 수 있는 양식을 만듭니다. 사용자가 양식을 작성하면 사용자가 입력한 이메일 주소와 비밀번호의 유효성을 검사한 후 createUser 메서드에 전달합니다.

Swift

Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
  // ...
}

Objective-C

[[FIRAuth auth] createUserWithEmail:email
                           password:password
                         completion:^(FIRAuthDataResult * _Nullable authResult,
                                      NSError * _Nullable error) {
  // ...
}];

기존 사용자 로그인

기존 사용자가 자신의 이메일 주소와 비밀번호를 사용해 로그인할 수 있는 양식을 만듭니다. 사용자가 양식을 작성하면 signIn 메서드를 호출합니다.

Swift

Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
  guard let strongSelf = self else { return }
  // ...
}

Objective-C

[[FIRAuth auth] signInWithEmail:self->_emailField.text
                       password:self->_passwordField.text
                     completion:^(FIRAuthDataResult * _Nullable authResult,
                                  NSError * _Nullable error) {
  // ...
}];

사용자 정보 가져오기

사용자가 로그인되면 사용자에 대한 정보를 가져올 수 있습니다. 예를 들어 인증 상태 리스너에서 다음을 수행합니다.

Swift

if let user = user {
  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getTokenWithCompletion:completion: instead.
  let uid = user.uid
  let email = user.email
  let photoURL = user.photoURL
  var multiFactorString = "MultiFactor: "
  for info in user.multiFactor.enrolledFactors {
    multiFactorString += info.displayName ?? "[DispayName]"
    multiFactorString += " "
  }
  // ...
}

Objective-C

if (user) {
  // The user's ID, unique to the Firebase project.
  // Do NOT use this value to authenticate with your backend server,
  // if you have one. Use getTokenWithCompletion:completion: instead.
  NSString *email = user.email;
  NSString *uid = user.uid;
  NSMutableString *multiFactorString = [NSMutableString stringWithFormat:@"MultiFactor: "];
  for (FIRMultiFactorInfo *info in user.multiFactor.enrolledFactors) {
    [multiFactorString appendString:info.displayName];
    [multiFactorString appendString:@" "];
  }
  NSURL *photoURL = user.photoURL;
  // ...
}

다음 단계

다른 ID 공급업체 및 익명 게스트 계정에 대한 지원을 추가하는 방법을 알아보세요.