[Firebase] Apple 로그인
2023. 6. 4. 17:18
- [Firebase Console] - [Authentication]에서 Apple 추가
- [Target] - [Signing & Capablilties]에 [Sign in with Apple 추가]
- https://developer.apple.com/에 접속하여 [Certificates, Identifier & Profiles] - [Service IDs] 추가
- 여기서 Identifier은 BundleID에서 추가로 글자를 입력함
- 생성 완료 된 Identifiers 클릭하고 Configure
- 이 창이 등장할것이다.
- Domains and Subdomains에는 승인된 도메인명(.firebaseapp.com으로 끝나는 항목)
- Return URLs은 아래 있는 콜백 URL입력
- import, 암호화와 로그인 Function
import AuthenticationServices
import FirebaseAuth
//Apple Sign in
extension MainViewController {
func startSignInWithAppleFlow() {
let nonce = randomNonceString()
currentNonce = nonce
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
request.nonce = sha256(nonce)
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
private func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
let hashString = hashedData.compactMap {
return String(format: "%02x", $0)
}.joined()
return hashString
}
// 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: Array<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
}
}
- ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding
extension MainViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}
guard let appleIDToken = appleIDCredential.identityToken else {
print("Unable to fetch identity token")
return
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
return
}
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
Auth.auth().signIn(with: credential) { authResult, error in
if let error = error {
print ("Error Apple sign in: %@", error)
return
}
// User is signed in to Firebase with Apple.
// ...
///Main 화면으로 보내기
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let mainViewController = storyboard.instantiateViewController(identifier: "LobbyViewController")
mainViewController.modalPresentationStyle = .fullScreen
self.navigationController?.show(mainViewController, sender: nil)
}
}
}
}
extension MainViewController: ASAuthorizationControllerPresentationContextProviding{
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
728x90
'Study > Firebase' 카테고리의 다른 글
[Firebase] Google 로그인 (0) | 2023.06.04 |
---|---|
[iOS] Firebase FireStore/Storage (0) | 2022.07.19 |
[iOS] Firebase Authentication (0) | 2022.07.18 |