Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to sign in with Apple and Firebase and Supabase #977

Open
onmyway133 opened this issue Aug 3, 2024 · 0 comments
Open

How to sign in with Apple and Firebase and Supabase #977

onmyway133 opened this issue Aug 3, 2024 · 0 comments
Labels

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Aug 3, 2024

Show ASAuthorizationController with a simple nonce. Once we have the idToken, create an OAuthProvider.appleCredential and pass to Firebase Auth

final class AppleLoginService: NSObject {
    static let shared = AppleLoginService()
    
    private var nonce: String?
    
    func show() {
        let nonce = generateNonce()
        self.nonce = nonce
        
        let provider = ASAuthorizationAppleIDProvider()
        let request = provider.createRequest()
        request.requestedScopes = [.fullName, .email]
        request.nonce = nonce?.sha256
        
        let vc = ASAuthorizationController(authorizationRequests: [request])
        vc.delegate = self
        vc.presentationContextProvider = self
        vc.performRequests()
    }
    
    private func generateNonce() -> String? {
        "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._"
            .shuffled()
            .description
    }
}

extension AppleLoginService: ASAuthorizationControllerDelegate {
    func authorizationController(
        controller: ASAuthorizationController,
        didCompleteWithAuthorization authorization: ASAuthorization
    ) {
        guard
            let appleCredential = authorization.credential as? ASAuthorizationAppleIDCredential,
            let idTokenData = appleCredential.identityToken,
            let idToken = String(data: idTokenData, encoding: .utf8)
        else {
            return
        }
     
        let credential = OAuthProvider.appleCredential(
            withIDToken: idToken,
            rawNonce: nonce,
            fullName: appleCredential.fullName
        )
        
        Task {
            do {
                let result = try await Auth.auth().signIn(with: credential)
            } catch {
                printD(error)
            }
        }
    }
    
    func authorizationController(
        controller: ASAuthorizationController,
        didCompleteWithError error: any Error
    ) {
        printD(error)
    }
}

extension AppleLoginService: ASAuthorizationControllerPresentationContextProviding {
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        UIApplication.shared.activeWindow!
    }
}

If we're using Supabase, we can call signInWithIdToken with .apple provider

supabase.auth.signInWithIdToken(
    credentials: .init(
        provider: .apple,
        idToken: idToken,
        nonce: nonce
    )
)

Read more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant