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

Added Facebook Login for Android #74

Merged
merged 4 commits into from
Jul 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions docs/AUTHENTICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Version 1.1.0 of this plugin added the capability to log your users in, either
* anonymously,
* by email and password,
* using a custom token,
* using Facebook (iOS: solid, Android: experimental).
* using Facebook.

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

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

### Facebook login
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).

Don't forget to enable Facebook login in your firebase instance.
First, enable Facebook login in your firebase instance and add the App-ID and App-Secret.

Then add the following lines to your code and check for setup instructions for your platform below.

```js
firebase.login({
Expand All @@ -197,6 +198,17 @@ Don't forget to enable Facebook login in your firebase instance.
)
```

#### iOS
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).

#### Android

1. Uncomment the facebook plugin in `node_modules\nativescript-plugin-firebase\platforms\android\include.gradle`
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`
3. Add `<string name="facebook_app_id">1234</string>` to `platforms\android\src\main\res\values\strings.xml`
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`.
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.`

### logout
Shouldn't be more complicated than:

Expand Down
2 changes: 1 addition & 1 deletion firebase-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ firebase.notifyAuthStateListeners = function(data) {
firebase.authStateListeners.forEach(function (listener) {
try {
if (listener.thisArg) {
listener.onAuthStateChanged.apply(thisArg, data);
listener.onAuthStateChanged.call(listener.thisArg, data);
} else {
listener.onAuthStateChanged(data);
}
Expand Down
50 changes: 34 additions & 16 deletions firebase.android.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var appModule = require("application");
var utils = require("utils/utils");
var fs = require("file-system");
var firebase = require("./firebase-common");

firebase._launchNotification = null;

var fbCallbackManager = null;

(function() {
if (typeof(com.google.firebase.messaging) === "undefined") {
return;
Expand Down Expand Up @@ -183,9 +186,19 @@ firebase.init = function (arg) {
return;
}
firebase.storage = com.google.firebase.storage.FirebaseStorage.getInstance().getReferenceFromUrl(arg.storageBucket);
}
}

if (typeof(com.facebook) !== "undefined") {
com.facebook.FacebookSdk.sdkInitialize(appModule.android.context);
fbCallbackManager = com.facebook.CallbackManager.Factory.create();
appModule.android.on("activityResult",function(eventData){
fbCallbackManager.onActivityResult(eventData.requestCode, eventData.resultCode, eventData.intent);
})
}

resolve(firebase.instance);


resolve(firebase.instance);
} catch (ex) {
console.log("Error in firebase.init: " + ex);
reject(ex);
Expand Down Expand Up @@ -375,7 +388,7 @@ firebase.getCurrentUser = function (arg) {
uid: user.getUid(),
name: user.getDisplayName(),
email: user.getEmail(),
profileImageURL: user.getPhotoUrl()
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
});
} else {
reject();
Expand Down Expand Up @@ -416,7 +429,7 @@ function toLoginResult(user) {
name: user.getDisplayName(),
email: user.getEmail(),
// expiresAtUnixEpochSeconds: authData.getExpires(),
profileImageURL: user.getPhotoUrl()
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
// token: user.getToken() // can be used to auth with a backend server
};
}
Expand Down Expand Up @@ -479,29 +492,34 @@ firebase.login = function (arg) {
return;
}

// TODO see https://firebase.google.com/docs/auth/android/facebook-login#authenticate_with_firebase
var fbLoginManager = com.facebook.login.LoginManager.getInstance();
var callbackManager = com.facebook.CallbackManager.Factory.create();

fbLoginManager.registerCallback(
callbackManager,
new com.facebook.FacebookCallback({
onSuccess: function (loginResult) {
console.log("------------- fb callback");
console.log("------------- fb loginResult " + loginResult);
}
})
fbCallbackManager,
new com.facebook.FacebookCallback({
onSuccess: function (loginResult) {
var token = loginResult.getAccessToken().getToken();
var authCredential = com.google.firebase.auth.FacebookAuthProvider.getCredential(token);
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
},
onCancel: function() {
reject("Facebook Login canceled");
},
onError: function(ex) {
reject("Error while trying to login with Fb "+ex);
}
})
);

var permissions = ["public_profile", "email"];
var permissions = utils.ad.collections.stringArrayToStringSet(["public_profile", "email"]);
var activity = appModule.android.foregroundActivity;
fbLoginManager.logInWithReadPermissions(foregroundActivity, permissions);
fbLoginManager.logInWithReadPermissions(activity, permissions);

} else {
reject ("Unsupported auth type: " + arg.type);
}
}


try {
if (appModule.android.foregroundActivity) {
_resolve();
Expand Down
7 changes: 4 additions & 3 deletions firebase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ declare module "nativescript-plugin-firebase" {
export interface User {
uid: string;
email?: string;
provider: LoginType;
expiresAtUnixEpochSeconds: number;
name?:string;
provider?: LoginType;
expiresAtUnixEpochSeconds?: number;
profileImageURL?: string;
token: string;
token?: string;
}

/**
Expand Down