Skip to content

Error after signing in with apple #82

@f3dm76

Description

@f3dm76

In out app we have log in with apple implemented through CustomAuth. Why not Web3Auth? Because it creates extra web view in the background.

So we log in with SignInWithAppleButton, and then pass the JWT to Web3Auth through CustomAuth. And now that we have CustomAuth implementation we have to stick to it, because Web3Auth(params).login(W3ALoginParams(loginProvider: .APPLE)) does not lead to the same profile. Recently we updated from version 5 to version 11, and CustomAuth with apple started returning an error:

Error Domain=com.apple.AuthenticationServices.WebAuthenticationSession Code=3 "The UIWindowScene for the returned window was not in the foreground active state." UserInfo={NSDebugDescription=The UIWindowScene for the returned window was not in the foreground active state.}

Most likely it happens because apple login flow triggers a modal Safari-based login under the hood (via ASWebAuthenticationSession or similar), and this temporarily moves the app to the background or an inactive state while the Safari window is active. After a successful login Web3Auth wants to call a redirect back to app I guess, leading to an error because the app is still in the background while the apple's login sheet is still displayed.

Please tell us exactly how to reproduce the problem you are running into.

  1. Press SignInWithAppleButton, and enter the appID and password
  2. Pass SignInWithAppleButton's result to CustomAuth
  3. Error

Code

this line causes the error:
let torusLoginResponse = try await customAuth.triggerAggregateLogin(args: aggregateLoginParams)

apple button, used to login with apple:

var appleButton: some View {
            SignInWithAppleButton(.signIn) { request in
                request.requestedScopes = [.email, .fullName]
            } onCompletion: { result in
                switch result {
                case .success(let authorization):
                    viewModel.handleSignInWithApple(authorization: authorization) // call the method to pass this to CutomAuth
                case .failure(let error):
                    print("Authorization failed: \(error.localizedDescription)")
                }
            }
    }

handleSignInWithApple:

func handleSignInWithApple(authorization: ASAuthorization) {
        let subVerifierDetailsArray = SingleLoginParams(typeOfLogin: .jwt, verifier: ApplicationTarget.current.appleLoginSubverifierName, clientId: Constants.appleLoginVerifierClientId, jwtParams: OAuthClientOptions(domain: "web3auth.au.auth0.com", verifierIdField: "email"))

        let aggregateLoginParams = AggregateLoginParams(aggregateVerifierType: .single_id_verifier, verifierIdentifier: Constants.appleLoginVerifierId, subVerifierDetailsArray: [subVerifierDetailsArray])

        let customAuthArgs = CustomAuthArgs(urlScheme: "roveapp://auth", network: .CYAN, enableOneKey: true, web3AuthClientId: ApplicationTarget.current.web3AuthClientId)

        Task {
            do {
                let customAuth = try CustomAuth(config: customAuthArgs)
                let torusLoginResponse = try await customAuth.triggerAggregateLogin(args: aggregateLoginParams)
                let encoded = try JSONEncoder().encode(torusLoginResponse)
            } catch {
                DispatchQueue.main.async {
                    print(error)
                }
            }
        }
    }

Please create a minimal reproducible sample that shows the problem, and attach it below between the lines with the backticks.

import SwiftUI
import AuthenticationServices
import CustomAuth

@main
struct Application: App {

    @UIApplicationDelegateAdaptor var appDelegate: AppDelegate

    var body: some Scene {
        WindowGroup {
            appleButton
        }
    }

    var appleButton: some View {
        SignInWithAppleButton(.signIn) { request in
            request.requestedScopes = [.email, .fullName]
        } onCompletion: { result in
            switch result {
            case .success(let authorization):
                handleSignInWithApple(authorization: authorization) // call the method to pass this to CutomAuth
            case .failure(let error):
                print("Authorization failed: \(error.localizedDescription)")
            }
        }
    }

    func handleSignInWithApple(authorization: ASAuthorization) {
        let subVerifierDetailsArray = SingleLoginParams(typeOfLogin: .jwt, verifier: ApplicationTarget.current.appleLoginSubverifierName, clientId: Constants.appleLoginVerifierClientId, jwtParams: OAuthClientOptions(domain: "web3auth.au.auth0.com", verifierIdField: "email"))

        let aggregateLoginParams = AggregateLoginParams(aggregateVerifierType: .single_id_verifier, verifierIdentifier: Constants.appleLoginVerifierId, subVerifierDetailsArray: [subVerifierDetailsArray])

        let customAuthArgs = CustomAuthArgs(urlScheme: "roveapp://auth", network: .CYAN, enableOneKey: true, web3AuthClientId: ApplicationTarget.current.web3AuthClientId)

        Task {
            do {
                let customAuth = try CustomAuth(config: customAuthArgs)
                let torusLoginResponse = try await customAuth.triggerAggregateLogin(args: aggregateLoginParams)
                let encoded = try JSONEncoder().encode(torusLoginResponse)
            } catch {
                DispatchQueue.main.async {
                    print(error)
                }
            }
        }
    }

}

Logs

nw_connection_copy_protocol_metadata_internal_block_invoke [C6] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
nw_connection_copy_connected_local_endpoint_block_invoke [C4] Connection has no local endpoint
nw_connection_copy_connected_local_endpoint_block_invoke [C4] Connection has no local endpoint
11.1.0 - [FirebaseAnalytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
11.1.0 - [FirebaseAnalytics][I-ACS023012] Analytics collection enabled
nw_connection_copy_connected_local_endpoint_block_invoke [C4] Connection has no local endpoint
 89603          HALC_ProxyIOContext.cpp:1621  HALC_ProxyIOContext::IOWorkLoop: skipping cycle due to overload
Error Domain=com.apple.AuthenticationServices.WebAuthenticationSession Code=3 "The UIWindowScene for the returned window was not in the foreground active state." UserInfo={NSDebugDescription=The UIWindowScene for the returned window was not in the foreground active state.}

Smartphone (please complete the following information):

  • Device: simulator iPhone 16 pro max
  • OS: iOS 18.3

Additional context
CustomAuth version 11.0.2

Could you please consider adding something like customAuth.triggerAggregateLogin(...) which simply returns the torusLoginResponse without trying to call any redirects or whatever relies on being in the foreground? Or if you have that already, could you please point me in the correct direction?
Thank you in advance for your help, have a great day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions