새소식

Study/Firebase

[Firebase] Google 로그인

  • -

- PodFile 추가

 

pod 'Firebase/Auth'
pod 'Firebase/Firestore'
pod 'GoogleSignIn'

 

 

 

- Firebase Console에서 Google 추가

 

 

 

- [GoogleService-Info.plist] - [REVERSED_CLIENT_ID]를 URLScheme에 추가

 

 

 

 

- AppDelegate 코드 추가

 

import UIKit
import Firebase
import GoogleSignIn


func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance.handle(url)
}

 

 

- Login Function

 

 @IBAction func btnGoogleLoginClicked(_ sender: UIButton) {
        
        guard let clientID = FirebaseApp.app()?.options.clientID else { return }


        let config = GIDConfiguration(clientID: clientID)
        GIDSignIn.sharedInstance.configuration = config

        GIDSignIn.sharedInstance.signIn(withPresenting: self) { [unowned self] result, error in
            
            if let error = error {
                print("ERROR", error.localizedDescription)
              return
            }

            guard let user = result?.user,
                  let idToken = user.idToken?.tokenString
            else {
                return
            }

            let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                           accessToken: user.accessToken.tokenString)

            Auth.auth().signIn(with: credential) { _, _ in
                self.showMainViewController()
            }
        }
    }
    
    
   func showMainViewController() {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let lobbyViewController = storyboard.instantiateViewController(identifier: "LobbyViewController")
        lobbyViewController.modalPresentationStyle = .fullScreen
        UIApplication.shared.windows.first?.rootViewController?.show(lobbyViewController, sender: nil)
    }

 

 

 

참고

https://firebase.google.com/docs/auth/ios/google-signin?hl=ko#swift_5

 

728x90

'Study > Firebase' 카테고리의 다른 글

[Firebase] Apple 로그인  (0) 2023.06.04
[iOS] Firebase FireStore/Storage  (0) 2022.07.19
[iOS] Firebase Authentication  (0) 2022.07.18
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.