Home / IOS Development / fast – Enable Firebase authentication across apps with shared iOS keychain

fast – Enable Firebase authentication across apps with shared iOS keychain



My problem sounds quite simple: I have an app and also a partial extension. What I want to do is check in Share Extension whether a user is not logged in. For that I found this documentation.

Right now, if the user is logging in, I will migrate the logged 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 gives me no error, so I guess it works. But I am facing two major issues at the moment:

1

. Problem inside the main app:

Like I said, the code above works perfectly, I also load some data that works well the first user signs up. But if I restart the app, the user does not have to log in again, and will be taken to the main view where I retrieve data again for currentUser. But it fails 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 feature is also called right after the user logs in (first code snippet at the top) and works well, but only the first time.

This is error I come from Firebase:

6.21.0 - [Firebase/Auth][I-AUT000001] Error loading saved user at startup: Error domain = FIRAuthErrorDomain Code = 17995 "An error occurred while accessing the keyring. , NSLocalizedFailureReason = SecItemCopyMatching (0), NSLocalizedDescription = An error occurred while accessing the keychain. The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary will contain more information about the error}

2. the problem in my Share Extension:

Like I said, I want to check if a user is logged in or not. Because I tried this inside 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 it's printing not signed in even if a user has just logged in to the main app.

I have no idea what I am doing wrong here, and could not find anything in the documentation. Maybe that's how I get the data for the user. Maybe I need to call something accessGroup there too, but I'm stuck ... Grateful for all the help!


Source link