firebaseAuth via phonenumber - "token mismatch" error on iPhone

Dear Corona community,

I finally managed to auth via SMS on android devices, but I just can’t get it work on iphones and after 12 hours of trying, it’s time to ask here :slight_smile:

I use Scott’s firebaseAuth plugin and SMS registration works on my Android. When I try to signup on my iPhone, I get a “token mismatch” error instead.

I have:

  • created an APN-key in apple developer portal and included it in firebase

  • put the GoogleServices-Info.plist in my root directory

  • verified that in GoogleServices-info.plist the “API_KEY” is the same as my firebase server key

  • recreated provisioning profile and certificates over and over again

Does anyone have had the same error or any idea where to look at?

Appreciate any help :slight_smile:

Thanks,

Chris

This be fixed 

check 

firebaseAuth.setAPNSToken(APNSToken, mode)

https://scotth.tech/plugin-firebaseAuth

Thank you sir!

I have problems in understanding what

APNSToken(string) Apple push notifications

should look like though.

I tried the content of my APN-key file (the file that I also add in firebase console) without success. Would you mind giving any information on what this string consists of?

try

local function onNotification(event) if event.type == "remoteRegistration" then firebaseAuth.setAPNSToken(event.token, "sandbox") end end -- Set up a notification listener. Runtime:addEventListener("notification", onNotification)

Thank you Scott.

Unfortunately that does not help. I can see in device’s console that onNotification fires three times when I initiate phonenumber-verification, and the third time it is indeed of type “remoteRegistration”.

I can even print out my token and of course set it like in your example, but again:

Token Mismatch :frowning:

Here is a list of screenshots hopefully proving that my settings are correct:

In my apple developer portal, I have

  • an iOS certificate:

  • my test devices registered:

  • an appID:

  • an APNSKey:

  • a provisioning profile:

In firebase console, I have

  • my iOS registered:

  • added the APNSKey:

My main.lua has

  • added the runtime listener to listen for notifications as Scott posted above:

  • this listener coded as you can see here:

  • and of course a function called when a button is pressed:

In my config.lua

  • types of notifications your app will use are set:

In my build.settings

- all is set according to the docs:

Now, when I build my app:

  • my provisioning profile is chosen:

and when I finally launch my app:

  • console can print out my token:

But, I get a “token mismatch” error.

Hope that Scott or anyone who is familiar with this topic can give any advise here!

Problem solved!

So, it finally works. I have PMed with user hraza back and forth and double … wait … triple-checked all my settings in Apple developer portal and firebase without success. Last night he told me to go from developer to distribution provisioning and give that a try. I did, without success. After staring at the “token mismatch” error for five minutes in pure frustration mood, I tapped again on my registerbutton and boom, I received the verification SMS (without reloading the app).

I figured that the APNSToken might have been set too late as it needed a second tap on my button to make it work. So I reinstalled the app, tried again and this time it worked right away.

As I found out later, firebaseAuth.isSignedIn() returns true even if the app is de- and reinstalled. So I included a firebaseAuth.SignOut() to make sure the whole process starts from scratch: Registration still worked like a charme.

So, I dont know what exactly changed, but I figured it must at least have something to do with development and distribution mode.

To verify this, I just revoked all my production-certificates and keys and created new dev-certificates and keys. My “hope” was that this would cause the “token mismatch” error again and one could identify the dev-mode as the problem.

But now it works, even in dev-mode.

So, at this moment it feels a little unsafe since I cannot really identify what caused the error. But the most important thing: It works :slight_smile:

Sidenote: Last night I could not build the app in like 3 of 4 attempts, but got a plugin error instead, saying that the notification plugin cannot be included. Dont know if that has something to do with my case, but maybe Corona devs changed something?

Anyway, this can be considered as solved.

Thanks to anybody who took his time to read this.

Nice!

The build errors sounds like a transient network event or a temporary outage with the build servers.

Rob

Update: I just installed the app on another new iOS device and got the “token mismatch” error again. After some hours of testing I could not only solve the issue, but now can even reproduce it:

Remember: On my first iOS device I could only use phonenumber-registration after I had changed from dev to production profile in apple developer portal. When I tapped my register-button the first time after this change, I got the “token mismatch” error again, but after tapping a second time after a couple of seconds, it worked for the first time. I could then even go back to development profile and it worked.

Now, still in development-profile, I installed my app on a new device: Token mismatch. So I again changed everything to production mode, installed it, and again got the “token mismatch” error on first tap, but a successfull registration after second tap on my button.

Once this works, it does work even after I reinstall the app. But as soon as I factory-reset the iphone and install the app on this fresh device, I get the “token mismatch” error again for the first tap and it takes a second tap after a couple of seconds to make it work.

So, these observations make me conclude that

firebaseAuth.setAPNSToken(token, mode)

stores the token somewhere independent from the app (since deleting and reinstalling the app allows registration right away), but gets - of course - lost when iphone is factoryreset and of course is not yet there on a new device. In these two cases it is necessary to call

firebaseAuth.sendVerificationCode(phoneNumber,listener)

twice: The first time to receive the token through

local function onNotification(event) if event.type == "remoteRegistration" then firebaseAuth.setAPNSToken(event.token, "prod") end end -- Set up a notification listener. Runtime:addEventListener("notification", onNotification)

even though at this time a “token mismatch” error occurs, and then a second time to use the token.

Scott: Does this make sense to you? Can you confirm my guesses or am I wrong in my conclusions? Most important question though: How can I get the token before actually using it in “sendVerificationCode(phoneNumber,listener)” - function?

Would appreciate any clarification here :slight_smile:

Would it be a valid workaround to catch the first “token mismatch” error on a new device and just call the function again with some delay? Like so:

-- function to register user via phone number function loginSMS() firebaseAuth.sendVerificationCode(numberField.text, function(event) -- in case of an error ("token mismatch" expected on a new device) if(event.isError) then if(errorCounter \< 5) then errorCounter = errorCounter + 1 timer.performWithDelay(500, function() loginSMS end) end -- in case Code was sent successfully else -- as soon as code is received on phone if(firebaseAuth.isSignedIn()) then -- do something -- if code has only been sent, but not received yet (or sent to other device) else -- do something else end end end ) end

An errourCounter would make sure that only 5 (or whatever) attempts are made. With a delay of XYZ miliseconds, the token should be set at some point and the request should go through. Havent tried it yet, just an idea.

Scott?

Sorry for the late response I have been very busy this weekend, please let me look over this tomorrow 

Did you have the chance to have a look at this?

I just a new build which I will be testing out tomorrow

Any news here? :slight_smile:

I have been busy with exam will have some info soon