Search
🍎

[FireBase - iOS] Apple 로그인 버튼

부제
카테고리
네트워킹
세부 카테고리
FireBase
Combine 카테고리
최종편집일
2022/07/16 15:17
작성중
관련된 포스팅
생성 일시
2022/07/16 15:14
태그

UIViewRepresentable

UIKit 의 뷰를 사용해야 하기에, UIViewRepresentable 을 선언해준다
이때 사용하고자 하는 UIView 의 타입이 ASAuthorizationAppleIDButton 이므로
UIViewType 의 typealias 를 ASAuthorizationAppleIDButton 로 선언해준다
makeUIView 메서드 내부에 해당 뷰를 만들어서 리턴해주면 되는데
이때

ASAuthorizationAppleIDButton(type: .signIn, style: .black)

를 사용한다
type
style
//SignInWithAppleButton.Swift import SwiftUI import AuthenticationServices struct SignInWithAppleButton: UIViewRepresentable { typealias UIViewType = ASAuthorizationAppleIDButton func makeUIView(context: Context) -> ASAuthorizationAppleIDButton { return ASAuthorizationAppleIDButton(type: .signIn, style: .black) } func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) { } }
Swift
복사
사용하고자 하는 View 에서 생성자를 호출해주면 된다.
//사용하고자 하는 곳 Button { print("DEBUG: Sign in with Apple") } label: { SignInWithAppleButton() .frame(height: 50) .cornerRadius(16) }
Swift
복사

Signing Capability 추가

이 과정에는 반드시 애플 개발자 계정(구독) 이 필요하다.
개발자 계정을 만들고 다시와 서 작성을 이어갈 예정이다.

로그인 메서드 구현

//SignInWithAppleObject.swift import AuthenticationServices import CryptoKit import FirebaseAuth class SignInWithAppleObject: NSObject { private var currentNonce: String? public func signInWithApple() { let request = ASAuthorizationAppleIDProvider().createRequest() request.requestedScopes = [.email, .fullName] //이메일과 풀네임을 요청함. //nonce란? = 랜덤생성 토큰 -> 재생공격을 방지하는 데 사용. //nonce 값을 구해낸다 = 작업 증명 let noce = randomNonceString() currentNonce = noce request.nonce = sha256(currentNonce!) //Apple ID 로그인 요쳥. let authorizationController = ASAuthorizationController(authorizationRequests: [request]) authorizationController.delegate = self authorizationController.performRequests() } // 이렇게 헐 벗은 채로 요청 보낼 수 없다. -> 암호화해서 보내야함. 아래 메서드를 사용해서 // Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce private func randomNonceString(length: Int = 32) -> String { precondition(length > 0) let charset: [Character] = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._") var result = "" var remainingLength = length while remainingLength > 0 { let randoms: [UInt8] = (0 ..< 16).map { _ in var random: UInt8 = 0 let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random) if errorCode != errSecSuccess { fatalError( "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)" ) } return random } randoms.forEach { random in if remainingLength == 0 { return } if random < charset.count { result.append(charset[Int(random)]) remainingLength -= 1 } } } return result } @available(iOS 13, *) private func sha256(_ input: String) -> String { let inputData = Data(input.utf8) let hashedData = SHA256.hash(data: inputData) let hashString = hashedData.compactMap { String(format: "%02x", $0) }.joined() return hashString } } @available(iOS 13, *) extension SignInWithAppleObject: ASAuthorizationControllerDelegate { func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { //1. 인증 객체가 성공적으로 리턴되었는지 확인하기위해 credential 이 존재하는 지 확인해야함 if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential { //2. 3가지 상태를 체크해야함 //2-1 : 리턴받은 nonce 값이 우리가 가지고 있는 로컬 nonce 값과 일치하는지 체크해야하고. guard let nonce = currentNonce else { print("Invalide state: A login callback was recieved, but no login request was sent") return } //2-2 : credential 이 identityToken 을 갖고 있는지 확인해야하며,. guard let appleIDToken = appleIDCredential.identityToken else { print("Unable to fetch identity token") return } //2-3 : 이 토큰이 String 으로 변환이 되는지를 확인해야한다. //이 토큰은 JWT 토큰이다. guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else { print("Unable to serialize token string from data") return } // Initialize a Firebase credential. let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce) // 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 authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { // Handle error. print("Sign in with Apple errored: \(error)") } }
Swift
복사

사용하고자 하는 View 에 State 변수 선언

@State private var signInWithAppleObject = SignInWithAppleObject()
Swift
복사

signInWithApple 메서드 호출

Button { signInWithAppleObject.signInWithApple() } label: { SignInWithAppleButton() .frame(height: 50) .cornerRadius(16) }
Swift
복사