Search
🗝️

[FireBase - iOS] Google 로그인

부제
카테고리
네트워킹
세부 카테고리
FireBase
Combine 카테고리
최종편집일
2022/07/16 15:17
작성중
관련된 포스팅
생성 일시
2022/07/16 15:14
태그
pod 'FirebaseAuth' pod 'GoogleSignIn'
Swift
복사
1.
Firebase Console에서 Google을 로그인 방법으로 사용 설정합니다.
a.
Firebase Console에서 인증 섹션을 엽니다.
b.
로그인 방법 탭에서 Google 로그인 방법을 사용 설정하고 저장을 클릭합니다.
import FirebaseCore import GoogleSignIn
Swift
복사
1.
다음과 같이 Xcode 프로젝트에 커스텀 URL 스키마를 추가합니다.
a.
왼쪽 트리 보기에서 프로젝트 이름을 두 번 클릭하여 프로젝트 구성을 엽니다. 대상 섹션에서 앱을 선택하고 정보 탭을 선택한 후 URL 유형 섹션을 펼칩니다.
b.
+ 버튼을 클릭하고 반전된 클라이언트 ID의 URL 스키마를 추가합니다. 이 값을 찾으려면 GoogleService-Info.plist 구성 파일을 열고 REVERSED_CLIENT_ID 키를 찾습니다. 이 키의 값을 복사하여 구성 페이지의 URL 스키마 상자에 붙여넣습니다. 다른 필드는 비워 둡니다.
class LoginManager { static let shared = LoginManager() var currentUser: User? { Auth.auth().currentUser } var uid: String? { Auth.auth().currentUser?.uid } //로그인 여부 -> Bool 값 리턴 var isLogin: Bool { currentUser != nil } func signInFirebase(with credential: AuthCredential) { // Sign in with Firebase. Auth.auth().signIn(with: credential) { (authResult, error) in if (error != nil) { // Error. If error.code == .MissingOrInvalidNonce, make sure // you're sending the SHA256-hashed nonce as a hex string with // your request to Apple. print(error?.localizedDescription) return } } } //로그아웃 메서드. func logOut() { let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) } } //회원탈퇴 메서드 func signOut() { currentUser?.delete { error in if let error = error { print("DEBUG: 회원탈퇴 에러 - \(error.localizedDescription)") } else { print("DEBUG: 회원탈퇴 성공") } } } }
Swift
복사
//MARK: - 구글 로그인 class SignInWithGoogleObject: NSObject { public func signWithGoogle() { guard let clientID = FirebaseApp.app()?.options.clientID else { return } guard let presentingViewController = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first?.rootViewController else {return} let config = GIDConfiguration(clientID: clientID) // Start the sign in flow! GIDSignIn.sharedInstance.signIn(with: config, presenting: presentingViewController) { user, error in if let error = error { print("DEBUG: 구글 로그인 에러 - \(error.localizedDescription)") return } guard let authentication = user?.authentication, let idToken = authentication.idToken else { return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken) LoginManager.shared.signInFirebase(with: credential) } } }
Swift
복사