Firebase×xcode【sign in with apple】が動作しない。
解決したいこと
Firabase及び、プロジェクトに【sign in with apple】を実装を考えていますが、
エラーも吐かれず、Firebase上に登録もされない。
テスト段階で幾度となくsign in with appleの生体認証を利用し、
ユーザーの登録を試みましたが、いつからか何も登録されなくなってしまいました。
何度かユーザー登録をしていくうちに何も登録されてしまうのは、どんな原因が考えられますでしょうか?
何か考えられる事があればなんでもご教授頂ければ幸いです。
念のためソースコードも記載しておきます。
signinwithAppleObject
//
// SignInWithAppleObject.swift
// philosophy
//
// Created by 市川マサル on 2022/08/24.
//
import AuthenticationServices
import CryptoKit
import FirebaseAuth
public class SignInWithAppleObject: NSObject {
private var currentNonce: String?
/**サインアップ=初めて会員登録した際の処理 */
public func signInWithApple() {
let request = ASAuthorizationAppleIDProvider().createRequest()
request.requestedScopes = [.email, .fullName]
let nonce = randomNonceString()
currentNonce = nonce
request.nonce = sha256(nonce)
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.performRequests()
}
// 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
}
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
}
}
/**以下がサインインの処理 */
extension SignInWithAppleObject: ASAuthorizationControllerDelegate {
public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
// Sign in With Firebase app
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
guard let nonce = currentNonce else {
print("Invalid state: A login callback was received, but no login request was sent.")
return
}
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")
return
}
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
Auth.auth().signIn(with: credential) { result, error in
guard error == nil else {
print(error!.localizedDescription)
return
}
}
}
}
}
DebagとReleseともに登録あり
0 likes


