Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

firebaseAuth via phonenumber - "token mismatch" error on iPhone
Started by CoronaChris Aug 19 2018 10:35 AM

15 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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 :)

 

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 :)

 

Thanks,

 

Chris



[TOPIC: post.html]
#2

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

This be fixed 

check 

firebaseAuth.setAPNSToken(APNSToken, mode)

https://scotth.tech/plugin-firebaseAuth



[TOPIC: post.html]
#3

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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?



[TOPIC: post.html]
#4

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

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)


[TOPIC: post.html]
#5

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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 :(



[TOPIC: post.html]
#6

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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

 

In my apple developer portal, I have

 

- an iOS certificate:

Attached File  Certificate.jpg   165.44KB   0 downloads

 

- my test devices registered:

Attached File  devices.jpg   277.12KB   0 downloads

 

- an appID:

Attached File  Identifiers.jpg   359.63KB   0 downloads

 

- an APNSKey:

Attached File  APNsKey.jpg   115.15KB   0 downloads

 

- a provisioning profile:

Attached File  provisioning.jpg   245.69KB   0 downloads

 

 

 

In firebase console, I have

 

- my iOS registered:

Attached File  firebase1.jpg   206.58KB   0 downloads

 

- added the APNSKey:

Attached File  firebase2.jpg   280.26KB   0 downloads

 

 

 

My main.lua has

 

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

Attached File  mainLua1.jpg   117.46KB   0 downloads

 

- this listener coded as you can see here:

Attached File  mainLua2.jpg   100.22KB   0 downloads

 

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

Attached File  mainLua3.jpg   109.86KB   0 downloads

 

 

 

In my config.lua

 

- types of notifications your app will use are set:

Attached File  configLua.jpg   102.44KB   0 downloads

 

 

 

In my build.settings

 

- all is set according to the docs:

Attached File  buildSettings.jpg   260.96KB   0 downloads

 

 

 

Now, when I build my app:

 

- my provisioning profile is chosen:

Attached File  BuildProcess.jpg   159.97KB   0 downloads

 

 

and when I finally launch my app:

 

- console can print out my token:

Attached File  DeviceConsole.jpg   208.66KB   0 downloads

 

 

But, I get a "token mismatch" error.

 

 

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

 

 



[TOPIC: post.html]
#7

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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 :)

 

 

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.



[TOPIC: post.html]
#8

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

Nice!



[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,779 posts
  • Enterprise

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

 

Rob



[TOPIC: post.html]
#10

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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 :)



[TOPIC: post.html]
#11

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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?



[TOPIC: post.html]
#12

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

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



[TOPIC: post.html]
#13

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

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



[TOPIC: post.html]
#14

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

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

[TOPIC: post.html]
#15

CoronaChris

[GLOBAL: userInfoPane.html]
CoronaChris
  • Contributor

  • 162 posts
  • Corona SDK

Any news here? :)



[TOPIC: post.html]
#16

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,795 posts
  • Enterprise

I have been busy with exam will have some info soon




[topic_controls]
[/topic_controls]