0

I'm signing users into my app with a custom token using their WordPress ID.

Auth.auth().signIn(withCustomToken: customToken ?? "") { (user, error) in
  // ...
}

That works ok but I need to update the rules which will keep users from writing to the database. This is counterproductive because I want to track the products they view etc...

I want to sign the user anonymously when the app opens.

Auth.auth().signInAnonymously() { (authResult, error) in
  // ...
}

I need to link the anonymous user to the WordPress user. Unfortunately, according to the documentation https://firebase.google.com/docs/auth/ios/account-linking the user needs to be linked with a credential but the credentials provided are GoogleAuthProvider, FacebookAuthProvider and EmailAuthProvider and several others but no Custom Provider.

Is it possible to create a credential out of the custom token?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
GIJoeCodes
  • 1,680
  • 1
  • 25
  • 28
  • 1
    As far as I know it is not possible to link accounts from custom auth providers with those from built-in providers. See for some more on this bojeil's answer here: https://stackoverflow.com/questions/40171663/linking-custom-auth-provider-with-firebase and https://github.com/FirebaseExtended/custom-auth-samples/issues/10 – Frank van Puffelen Sep 21 '20 at 17:42
  • 1
    @FrankvanPuffelen So, [OauthProvider.credential()](https://firebase.google.com/docs/reference/swift/firebaseauth/api/reference/Classes/OAuthProvider#/c:objc(cs)FIROAuthProvider(cm)credentialWithProviderID:IDToken:accessToken:) will not yield something that can be linked? – Doug Stevenson Sep 21 '20 at 17:53
  • That first link did it! I got the anonymous user id, which gave me the provider id. Then created the credential: let credential = OAuthProvider.credential(withProviderID: providerId, accessToken: customToken). I don't know why OAuthProvider worked. – GIJoeCodes Sep 21 '20 at 17:53
  • @DougStevenson Yeah, for OAuth providers it *is* possible. See nivco's example/explanation in the `custom-auth-samples` repo. – Frank van Puffelen Sep 21 '20 at 18:12

2 Answers2

0

The examples you see in the documentation are not (in theory) the only options. Look at the API documentation for link():

Associates a user account from a third-party identity provider with this user and returns additional identity provider data.

It's signature in Swift is this:

func link(with credential: FIRAuthCredential, completion: ((AuthDataResult?, Error?) -> Void)? = nil)

It takes any FIRAuthCredential, of which there are many subclasses available. One of those is OAuthCredential. If you have custom token, try creating a credential with OAuthProvider.crediential() and link that to the account.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Following Doug Stevenson and Frank van Puffelen comments, I was able to link the anonymous user account using a credential.

I have my original customToken

guard let customToken = result?.data as? String else { return }

Because the user signs in now anonymously when the app first opens, I now have a user.

guard let authUser = Auth.auth().currentUser else { return }

I get the provider id from the user.

let providerId = authUser.providerID

With the provider id now I can get a credential using OAuthProvider

let credential = OAuthProvider.credential(withProviderID: providerId, accessToken: customToken)

Now I can link the user to the anonymous user.

authUser.link(with: credential) { (result, error) in
GIJoeCodes
  • 1,680
  • 1
  • 25
  • 28