今回はFirebaseのAppleログイン機能をswiftUIで実装していきます。
FireBaseでログインしてアプリをプロジェクトを作成
まずはfirebaseにアクセスし、コンソールから新しくプロジェクトを追加します。
「プロジェクトを追加」ボタンを押して説明どおりに進めば問題ないと思うので、ここの説明は割愛します。
Firebase
Firebase は、高品質のアプリを迅速に開発できる Google のモバイル プラットフォームで、ビジネスの成長に役立ちます。
GoogleService-info.plistをダウンロードしてファイルを追加
プロジェクトを作成したら、 右上の歯車マークをタップ。「マイアプリ」に記載されているGoogeService-info.plistをダウンロード。

ダウンロードしたら、自身のフォルダにいれます。

サインインメソッドのAppleを有効にする
次にAuthenticationを選択し、サインインメソッドの「Apple」を有効にします。

CocoaPodを追加
次にpodfileに以下を追加していきます。
sudo gem install cocoapods
pod init
// podfile
pod 'FirebaseCore'
pod 'Firebase/Auth'
pod 'FirebaseFirestoreSwift'
pod install
FirebaseCoreをインポート
この時点でビルドできたら、windowファイルにFirebaseCoreを追加していきます。
import SwiftUI
import FirebaseCore //追加
@main
struct MyAppNameApp: App {
//以下を追加
init(){
FirebaseApp.configure()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
SignInWithApp.swiftファイルを作成
新たにSignInWithAppというファイル(名前は何でもいいんですが)を作成し、以下のように書いていきます。
import Foundation
import SwiftUI
import CryptoKit
import AuthenticationServices
import FirebaseAuth
class SignInWithApple: NSObject, ASAuthorizationControllerDelegate{
static let instance = SignInWithApple()
// Unhashed nonce.
fileprivate var currentNonce: String?
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()
}
@available(iOS 13, *)
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
}
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 email = appleIDCredential.email ?? ""
var name = "Your name here"
if let fullName = appleIDCredential.fullName {
let formatter = PersonNameComponentsFormatter()
name = formatter.string(from: fullName)
}
// Initialize a Firebase credential.
let credential = OAuthProvider.credential(withProviderID: "apple.com",
idToken: idTokenString,
rawNonce: nonce)
print("SIGN IN TO FIREBASE NOW: withemail:\(email) / name: \(name) ")
}
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// Handle error.
print("Sign in with Apple errored: \(error)")
}
}
authorizationControllerメソッドのなかでサインイン完了後にする処理を記述できるので、そのなかで今後の処理を作成していきましょう。
コメント