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

Commit 96eaba0

Browse files
Merge pull request #74 from t-moe/master
Added Facebook Login for Android
2 parents d1e29f7 + 2896b69 commit 96eaba0

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

docs/AUTHENTICATION.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Version 1.1.0 of this plugin added the capability to log your users in, either
66
* anonymously,
77
* by email and password,
88
* using a custom token,
9-
* using Facebook (iOS: solid, Android: experimental).
9+
* using Facebook.
1010

1111
Each of these login mechanisms need to be enabled in your Firebase console at the 'Login & Auth' tab.
1212

@@ -180,9 +180,10 @@ Use this login type to authenticate against firebase using a token generated by
180180
```
181181

182182
### Facebook login
183-
On iOS this is rock solid, but on Android it's work in progress. If you want to use it for iOS open the `Podfile` in the plugin's `platforms/ios` folder and uncomment the Facebook line (you can't miss it).
184183

185-
Don't forget to enable Facebook login in your firebase instance.
184+
First, enable Facebook login in your firebase instance and add the App-ID and App-Secret.
185+
186+
Then add the following lines to your code and check for setup instructions for your platform below.
186187

187188
```js
188189
firebase.login({
@@ -197,6 +198,17 @@ Don't forget to enable Facebook login in your firebase instance.
197198
)
198199
```
199200

201+
#### iOS
202+
If you want to use it for iOS open the `Podfile` in the plugin's `platforms/ios` folder and uncomment the Facebook line (you can't miss it).
203+
204+
#### Android
205+
206+
1. Uncomment the facebook plugin in `node_modules\nativescript-plugin-firebase\platforms\android\include.gradle`
207+
2. Add `<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>` to the manifest/application tag in `app\App_Resources\Android\AndroidManifest.xml`
208+
3. Add `<string name="facebook_app_id">1234</string>` to `platforms\android\src\main\res\values\strings.xml`
209+
4. In your facebook dev console, add the Android app and set the Google Play Packagename to your applicationId (see your `package.config` or in `app\App_Resources\Android\app.gradle`) and the Classname to `com.tns.NativeScriptActivity`.
210+
5. Set the Key-Hash as well. If you don't know it you can also try facebook login in your app and observe the `adb logcat` output for something like `Key hash <......> does not match any stored key hashes.`
211+
200212
### logout
201213
Shouldn't be more complicated than:
202214

firebase-common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ firebase.notifyAuthStateListeners = function(data) {
5555
firebase.authStateListeners.forEach(function (listener) {
5656
try {
5757
if (listener.thisArg) {
58-
listener.onAuthStateChanged.apply(thisArg, data);
58+
listener.onAuthStateChanged.call(listener.thisArg, data);
5959
} else {
6060
listener.onAuthStateChanged(data);
6161
}

firebase.android.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
var appModule = require("application");
2+
var utils = require("utils/utils");
23
var fs = require("file-system");
34
var firebase = require("./firebase-common");
45

56
firebase._launchNotification = null;
67

8+
var fbCallbackManager = null;
9+
710
(function() {
811
if (typeof(com.google.firebase.messaging) === "undefined") {
912
return;
@@ -183,9 +186,19 @@ firebase.init = function (arg) {
183186
return;
184187
}
185188
firebase.storage = com.google.firebase.storage.FirebaseStorage.getInstance().getReferenceFromUrl(arg.storageBucket);
186-
}
189+
}
190+
191+
if (typeof(com.facebook) !== "undefined") {
192+
com.facebook.FacebookSdk.sdkInitialize(appModule.android.context);
193+
fbCallbackManager = com.facebook.CallbackManager.Factory.create();
194+
appModule.android.on("activityResult",function(eventData){
195+
fbCallbackManager.onActivityResult(eventData.requestCode, eventData.resultCode, eventData.intent);
196+
})
197+
}
187198

188-
resolve(firebase.instance);
199+
200+
201+
resolve(firebase.instance);
189202
} catch (ex) {
190203
console.log("Error in firebase.init: " + ex);
191204
reject(ex);
@@ -375,7 +388,7 @@ firebase.getCurrentUser = function (arg) {
375388
uid: user.getUid(),
376389
name: user.getDisplayName(),
377390
email: user.getEmail(),
378-
profileImageURL: user.getPhotoUrl()
391+
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
379392
});
380393
} else {
381394
reject();
@@ -416,7 +429,7 @@ function toLoginResult(user) {
416429
name: user.getDisplayName(),
417430
email: user.getEmail(),
418431
// expiresAtUnixEpochSeconds: authData.getExpires(),
419-
profileImageURL: user.getPhotoUrl()
432+
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
420433
// token: user.getToken() // can be used to auth with a backend server
421434
};
422435
}
@@ -479,29 +492,34 @@ firebase.login = function (arg) {
479492
return;
480493
}
481494

482-
// TODO see https://firebase.google.com/docs/auth/android/facebook-login#authenticate_with_firebase
483495
var fbLoginManager = com.facebook.login.LoginManager.getInstance();
484-
var callbackManager = com.facebook.CallbackManager.Factory.create();
485-
486496
fbLoginManager.registerCallback(
487-
callbackManager,
488-
new com.facebook.FacebookCallback({
489-
onSuccess: function (loginResult) {
490-
console.log("------------- fb callback");
491-
console.log("------------- fb loginResult " + loginResult);
492-
}
493-
})
497+
fbCallbackManager,
498+
new com.facebook.FacebookCallback({
499+
onSuccess: function (loginResult) {
500+
var token = loginResult.getAccessToken().getToken();
501+
var authCredential = com.google.firebase.auth.FacebookAuthProvider.getCredential(token);
502+
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
503+
},
504+
onCancel: function() {
505+
reject("Facebook Login canceled");
506+
},
507+
onError: function(ex) {
508+
reject("Error while trying to login with Fb "+ex);
509+
}
510+
})
494511
);
495512

496-
var permissions = ["public_profile", "email"];
513+
var permissions = utils.ad.collections.stringArrayToStringSet(["public_profile", "email"]);
497514
var activity = appModule.android.foregroundActivity;
498-
fbLoginManager.logInWithReadPermissions(foregroundActivity, permissions);
515+
fbLoginManager.logInWithReadPermissions(activity, permissions);
499516

500517
} else {
501518
reject ("Unsupported auth type: " + arg.type);
502519
}
503520
}
504521

522+
505523
try {
506524
if (appModule.android.foregroundActivity) {
507525
_resolve();

firebase.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ declare module "nativescript-plugin-firebase" {
9595
export interface User {
9696
uid: string;
9797
email?: string;
98-
provider: LoginType;
99-
expiresAtUnixEpochSeconds: number;
98+
name?:string;
99+
provider?: LoginType;
100+
expiresAtUnixEpochSeconds?: number;
100101
profileImageURL?: string;
101-
token: string;
102+
token?: string;
102103
}
103104

104105
/**

0 commit comments

Comments
 (0)