Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit eca6e2d

Browse files
Add a step to include authentication libraries (default 'true') #904
1 parent 4242efb commit eca6e2d

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-6.8.0.tgz",
12+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-6.8.1.tgz",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "~4.2.0"

src/firebase.android.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let fbCallbackManager = null;
3535
const GOOGLE_SIGNIN_INTENT_ID = 123;
3636
const REQUEST_INVITE_INTENT_ID = 48;
3737

38+
const authEnabled = lazy(() => typeof(com.google.firebase.auth) !== "undefined");
3839
const messagingEnabled = lazy(() => typeof(com.google.firebase.messaging) !== "undefined");
3940
const dynamicLinksEnabled = lazy(() => typeof(com.google.firebase.dynamiclinks) !== "undefined");
4041

@@ -298,33 +299,35 @@ firebase.init = arg => {
298299
}
299300
}
300301

301-
const firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
302+
if (authEnabled()) {
303+
const firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
302304

303-
if (arg.onAuthStateChanged) {
304-
firebase.authStateListener = new com.google.firebase.auth.FirebaseAuth.AuthStateListener({
305-
onAuthStateChanged: fbAuth => {
306-
const user = fbAuth.getCurrentUser();
307-
arg.onAuthStateChanged({
308-
loggedIn: user !== null,
309-
user: toLoginResult(user)
310-
});
311-
}
312-
});
313-
firebaseAuth.addAuthStateListener(firebase.authStateListener);
314-
}
305+
if (arg.onAuthStateChanged) {
306+
firebase.authStateListener = new com.google.firebase.auth.FirebaseAuth.AuthStateListener({
307+
onAuthStateChanged: fbAuth => {
308+
const user = fbAuth.getCurrentUser();
309+
arg.onAuthStateChanged({
310+
loggedIn: user !== null,
311+
user: toLoginResult(user)
312+
});
313+
}
314+
});
315+
firebaseAuth.addAuthStateListener(firebase.authStateListener);
316+
}
315317

316-
// Listen to auth state changes
317-
if (!firebase.authStateListener) {
318-
firebase.authStateListener = new com.google.firebase.auth.FirebaseAuth.AuthStateListener({
319-
onAuthStateChanged: fbAuth => {
320-
const user = fbAuth.getCurrentUser();
321-
firebase.notifyAuthStateListeners({
322-
loggedIn: user !== null,
323-
user: toLoginResult(user)
324-
});
325-
}
326-
});
327-
firebaseAuth.addAuthStateListener(firebase.authStateListener);
318+
// Listen to auth state changes
319+
if (!firebase.authStateListener) {
320+
firebase.authStateListener = new com.google.firebase.auth.FirebaseAuth.AuthStateListener({
321+
onAuthStateChanged: fbAuth => {
322+
const user = fbAuth.getCurrentUser();
323+
firebase.notifyAuthStateListeners({
324+
loggedIn: user !== null,
325+
user: toLoginResult(user)
326+
});
327+
}
328+
});
329+
firebaseAuth.addAuthStateListener(firebase.authStateListener);
330+
}
328331
}
329332

330333
// Firebase notifications (FCM)

src/firebase.ios.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -371,36 +371,38 @@ firebase.init = arg => {
371371
}
372372
}
373373

374-
if (arg.iOSEmulatorFlush) {
375-
try {
376-
// Attempt to sign out before initializing, useful in case previous
377-
// project token is cached which leads to following type of error:
378-
// "[FirebaseDatabase] Authentication failed: invalid_token ..."
379-
FIRAuth.auth().signOut();
380-
} catch (signOutErr) {
381-
console.log('Sign out of Firebase error: ' + signOutErr);
374+
if (typeof (FIRAuth) !== "undefined") {
375+
if (arg.iOSEmulatorFlush) {
376+
try {
377+
// Attempt to sign out before initializing, useful in case previous
378+
// project token is cached which leads to following type of error:
379+
// "[FirebaseDatabase] Authentication failed: invalid_token ..."
380+
FIRAuth.auth().signOut();
381+
} catch (signOutErr) {
382+
console.log('Sign out of Firebase error: ' + signOutErr);
383+
}
382384
}
383-
}
384385

385-
if (arg.onAuthStateChanged) {
386-
firebase.authStateListener = (auth, user) => {
387-
arg.onAuthStateChanged({
388-
loggedIn: user !== null,
389-
user: toLoginResult(user)
390-
});
391-
};
392-
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
393-
}
386+
if (arg.onAuthStateChanged) {
387+
firebase.authStateListener = (auth, user) => {
388+
arg.onAuthStateChanged({
389+
loggedIn: user !== null,
390+
user: toLoginResult(user)
391+
});
392+
};
393+
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
394+
}
394395

395-
// Listen to auth state changes
396-
if (!firebase.authStateListener) {
397-
firebase.authStateListener = (auth, user) => {
398-
firebase.notifyAuthStateListeners({
399-
loggedIn: user !== null,
400-
user: toLoginResult(user)
401-
});
402-
};
403-
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
396+
// Listen to auth state changes
397+
if (!firebase.authStateListener) {
398+
firebase.authStateListener = (auth, user) => {
399+
firebase.notifyAuthStateListeners({
400+
loggedIn: user !== null,
401+
user: toLoginResult(user)
402+
});
403+
};
404+
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
405+
}
404406
}
405407

406408
// Firebase DynamicLink

0 commit comments

Comments
 (0)