본문 바로가기

카테고리 없음

[OAuth] Google Login

공식문서 보러가기

 

Firebase 설정하기

 

iOS Project에서 Google Login하기

1. Podfile에 pod 추가하기

Podfile에 아래 코드를 추가한다.

pod 'Firebase/Auth'
pod 'GoogleSignIn'

 

2. URL Scheme 추가하기

 

GoogleService-Info의 REVERSED_CLIENT_ID 값을 복사해서 URL Types에 추가한다.

 

2. AppDelegate.swift에 아래 코드 추가

Firebase configure

Firebase configure를 초기화한다.

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

 

GIDSignIn 인스턴스

앱 대리자의 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)
}

 

3. Controller에 추가하기

Google 인증 token 받기

앱의 프레젠테이션 뷰 컨트롤러 및 클라이언트 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)

  // ...
}

 

Firebase에 인증하기

// Google에서 받은 token으로 firebase에 사용자 등록하기
Auth.auth().signIn(with: credential) { [weak self] _, error in
	guard let self = self else { return }
                
	if let error = error {
		print("Fail to Firebase Sign in with Google credential, \(error)")
	} else {
		self.showMainViewController()
	}
}

 

4. sign out하기

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}

 

[참고]

https://ksk9820.tistory.com/64

 

[iOS/swift] Google Sign-in for iOS _우동 소셜 로그인하기_210816

최근에 구글 로그인이 업데이트 된 것 같아서 별로 기록이 없길래 기록해본다. 참고로 M1 버전이다. 구글 로그인 전에 우동 로고 일단 임시로 새우고 기존에 있던 자체 로그인을 지웠다. 그래서

ksk9820.tistory.com

https://developers.google.com/identity/sign-in/ios/sign-in

 

iOS 앱에 Google 로그인 통합  |  Google Sign-In for iOS  |  Google Developers

iOS 앱에 Google 로그인 통합 이 페이지에서는 UIKit 및 UIApplicationDelegate 수명 주기를 기반으로 Google 로그인을 iOS 앱에 통합하는 방법을 보여줍니다. 앱이 장면 기반이거나 SwiftUI를 사용하는 경우 선

developers.google.com