firebase에 앱 번들 추가 후 info.plist를 받고 프로젝트에 추가해준다
firebase 내에서 authentication에서 구글 로그인을 추가해준다.
Podfile에 사용할 것들을 추가한다
pod 'Firebase/Auth', '8.6.0'
pod 'Firebase/Firestore', '8.6.0'
pod 'GoogleSignIn', '5.0.2'
Firebase와 GoogleSignIn을 import해준다.
AppDelegate에 GIDSignInDelegate
프로토콜을 받는다.
AppDelegate의 didFinishLaunchingWithOptions
에 FirebaseApp.configure()
로 firebase를 앱 실행시 초기화해준다
구글 로그인 델리게이트를 추가해준다
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
구글의 인증 프로세스가 끝날 때 앱이 수신하는 url을 처리해주는 openurl 메소드를 추가해주고, GIDSignIn.sharedInstance().handle(url)
을 리턴해준다
GIDSignInButton
으로 바꿔준다.GIDSignIn.sharedInsrtance().presentingViewController = self
GIDSignIn.sharedInstance().signIn()
을 추가해서 로그인이 진행될 수 있도록 해준다.didSignInFor
델리게이트 함수 내에서 idtoken값과 accesstoken 값을 받아와서 firebase 인증 정보에 등록해준다. guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) {[weak self] _, _ in
self?.showMainViewController()
}
로그아웃 버튼이 누르면 실행되는 함수에서 signOut()
을 해주고, 화면을 rootViewController로 전환해준다
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
self.navigationController?.popToRootViewController(animated: true)
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}