3
votes

My problem sounds quite simple: I have an App and also a Share Extension. What I would like to do is to check inside the Share Extension wether or not a user is logged in. For that I found this documentation.

Right now, if the user signs in I migrate the signed-in user to a shared keychain (inside the main app):

   Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
        
        if let userId = result?.user.uid { // successfully signed in
            
            let user = Auth.auth().currentUser
            do {
              try Auth.auth().useUserAccessGroup("teamID.com.example-app.group")
            } catch let error as NSError {
              print("Error changing user access group: %@", error)
            }
            Auth.auth().updateCurrentUser(user!) { error in
                print(error?.localizedDescription as Any)
            }
        }
    }

This is not giving me any errors so I guess it is working. But I am facing two major issues at the moment:

1. Problem inside the main app:

Like I said the code above works perfeclty fine, I am also loading some data which works fine the first the the user signs up. However if I restart the app, the user doesn't have to sign in again and should be directed to the main view where I retrieve data again for the currentUser. But that is failing because currentUser is nil...

static func getWishlists(completion: @escaping (_ success: Bool, _ dataArray: Any?, _ dropDownArray: Any?) -> Void) {
    
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser!.uid // <- throws error
    ...

That function is also called right after the user is signing in (1st code snippet at the top) and is working fine but only the first time.

This is the error I get from Firebase:

6.21.0 - [Firebase/Auth][I-AUT000001] Error loading saved user when starting up: Error Domain=FIRAuthErrorDomain Code=17995 "An error occurred when accessing the keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary will contain more information about the error encountered" UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_KEYCHAIN_ERROR, NSLocalizedFailureReason=SecItemCopyMatching (0), NSLocalizedDescription=An error occurred when accessing the keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary will contain more information about the error encountered}

2nd problem inside my Share Extension:

Like I said, I would like to check if a user is logged in or not. For that I tried this inside the Share Extension:

    do {
      try Auth.auth().useUserAccessGroup("teamID.com.example-app.group")
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    
    if Auth.auth().currentUser != nil {
      // User is signed in.
      print("signed in")
    } else {
      // No user is signed in.
      print("not signed in")
    }

But that is printing not signed in even though a user just signed in inside the main app.

I have no idea what exactly I am doing wrong here and couldnt find anything in the documentation. Maybe its how I retrieve the data for the user. Maybe I need to call something with accessGroup there as well, but I am stuck... Grateful for any help!

1
Just confirming, you have the Keychain Sharing capability, right?Pastre
@Pastre yes, on both app and extension with the Same identifierChris
What does the NSError.userInfo dictionary's NSLocalizedFailureReasonErrorKey say?Tarun Tyagi
@TarunTyagi I dont know how to read that error...Chris
Sorry, it's visible in the error log - this part NSLocalizedFailureReason=SecItemCopyMatching (0). This seems to be an environment/config issue. See this thread - github.com/firebase/firebase-ios-sdk/issues/3215 EXACTLY the same error.Tarun Tyagi

1 Answers

0
votes

I am not exactly sure of your issue but weren't you supposed to change an set your own userAccessToken

do {
      try Auth.auth().useUserAccessGroup("XXXXXXXXXXXXX")
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    
    if Auth.auth().currentUser != nil {
      // User is signed in.
      print("signed in")
    } else {
      // No user is signed in.
      print("not signed in")
    }

Replace your apps userAccessGroup in the place I have put the XXXXXXXXXXXXX placeholder.