From da673e3cdac17cde3c942593337ed9f0e9c29258 Mon Sep 17 00:00:00 2001 From: willhlaw Date: Sun, 3 Jul 2016 22:22:31 -0400 Subject: [PATCH] iOSSimulatorFlush for known iOS Simulator issue with invalid_token --- README.md | 11 ++++++++++- firebase.ios.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8803f9ad..aa72901c 100755 --- a/README.md +++ b/README.md @@ -126,10 +126,19 @@ On the simulator you may see this message if you have more than one app with the ``` [FirebaseDatabase] Authentication failed: invalid_token (Invalid claim 'aud' in auth token.) +or +[FirebaseDatabase] Authentication failed: invalid_token (audience was project 'firegroceries-904d0' but should have been project 'your-firebase-project') ``` This is [a known issue in the Firebase SDK](http://stackoverflow.com/questions/37857131/swift-firebase-database-invalid-token-error). -I always use a real device to avoid this problem. +I always use a real device to avoid this problem, but you can pass an 'iOSSimulatorFlush' option to init. +``` +firebase.init({ + // Optionally pass in properties for database, authentication and cloud messaging, + // see their respective docs and 'iOSSimulatorFlush' to flush token before init. + iOSSimulatorFlush: true +}).then() +``` ## Known issues on Android diff --git a/firebase.ios.js b/firebase.ios.js index 7b01426a..879f2f94 100755 --- a/firebase.ios.js +++ b/firebase.ios.js @@ -244,6 +244,19 @@ firebase.init = function (arg) { firebase.instance = FIRDatabase.database().reference(); + if (arg.iOSEmulatorFlush) { + try { + // Attempt to sign out before initializing, useful in case previous + // project token is cached which leads to following type of error: + // "[FirebaseDatabase] Authentication failed: invalid_token ..." + console.log('Attempting to sign out of Firebase before init'); + FIRAuth.auth().signOut(); + console.log('Sign out of Firebase successful'); + } catch(signOutErr) { + console.log('Sign out of Firebase error: ' + signOutErr); + } + } + if (arg.onAuthStateChanged) { firebase.authStateListener = function(auth, user) { arg.onAuthStateChanged({