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

Commit

Permalink
- Added auth state listener support in the init function
Browse files Browse the repository at this point in the history
- Auto-login revisiting users
- Added Facebook authentication (iOS is done, Android is a bit WIP due to build issues)
  • Loading branch information
EddyVerbruggen committed Jun 17, 2016
1 parent ebb7724 commit 50efca4
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 120 deletions.
71 changes: 45 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ And here's the comprehensive list of supported functions:
var firebase = require("nativescript-plugin-firebase");

firebase.init({
persist: true // Allow disk persistence. Default false.
persist: true, // Allow disk persistence. Default false.
onAuthStateChanged: function(data) { // optional but useful to immediately re-logon the user when he re-visits your app
console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
if (data.loggedIn) {
console.log("user's email address: " + (data.user.email ? data.user.email : "N/A"));
}
}
}).then(
function (instance) {
console.log("firebase.init done");
Expand Down Expand Up @@ -225,11 +231,10 @@ but if you only want to wipe everything at '/users', do this:
### login
v 1.1.0 of this plugin adds the capability to log your users in, either

* anonymously,

* by email and password or

* using a custom token.
- anonymously,
- by email and password, or
- using a custom token,
- using Facebook (experimental)

You need to add support for those features in your Firebase instance at the 'Login & Auth' tab.

Expand All @@ -240,7 +245,7 @@ As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_

> The recommended way to get the current user is by setting a listener on the Auth object
To listen to auth state changes you can register a listener like this:
To listen to auth state changes you can register a listener like this (you may have done this already during `init` (see above):

```js
var listener = {
Expand All @@ -263,8 +268,9 @@ To stop listening to auth state changed:
```

To check if already listening to auth state changes

```js
firebase.removeAuthStateListener(listener);
hasAuthStateListener(listener);
```

#### Anonymous login
Expand Down Expand Up @@ -321,30 +327,43 @@ See these [instructions on how to generate the authentication token](https://fir
)
```

#### Creating a Password account
#### Custom login
Use this login type to authenticate against firebase using a token generated by your own backend server.
See these [instructions on how to generate the authentication token](https://firebase.google.com/docs/auth/server).

```js
firebase.createUser({
email: '[email protected]',
password: 'firebase'
firebase.login({
// note that you need to generate the login token in your existing backend server
type: firebase.LoginType.CUSTOM,
token: token
}).then(
function (result) {
dialogs.alert({
title: "User created",
message: "userid: " + result.key,
okButtonText: "Nice!"
})
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
JSON.stringify(result);
},
function (errorMessage) {
console.log(errorMessage);
}
)
```

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

```js
firebase.login({
type: firebase.LoginType.FACEBOOK
}).then(
function (result) {
JSON.stringify(result);
},
function (errorMessage) {
dialogs.alert({
title: "No user created",
message: errorMessage,
okButtonText: "OK, got it"
})
console.log(errorMessage);
}
)
```

#### Resetting a password
### Resetting a password
```js
firebase.resetPassword({
email: '[email protected]'
Expand All @@ -359,7 +378,7 @@ See these [instructions on how to generate the authentication token](https://fir
)
```

#### Changing a password
### Changing a password
```js
firebase.changePassword({
email: '[email protected]',
Expand Down Expand Up @@ -391,7 +410,7 @@ com.android.dex.DexIndexOverflowException: method ID not in..

Congrats, you ran into [this issue](https://github.com/NativeScript/android-runtime/issues/344)
which can be solved by adding `multiDexEnabled true` to your `app/App_Resources/Android/app.gradle`
so it becomes somehthing like this:
so it becomes something like this:

```
android {
Expand Down Expand Up @@ -423,7 +442,7 @@ or start a geny emulator first and do: `tns run android`
## Future work
- Add support for `removeEventListener`.
- Possibly add more login mechanisms.

- Add other Firebase 3.x SDK features (there's already a few feature requests in the GitHub issue tracker.

## Credits
The starting point for this plugin was [this great Gist](https://gist.github.com/jbristowe/c89a7bcae7fc9a035ee7) by [John Bristowe](https://github.com/jbristowe).
5 changes: 4 additions & 1 deletion firebase-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ declare module "nativescript-plugin-firebase" {
*/
CUSTOM,
/**
* This requires you to setup Facebook Auth in the Firebase console.
* This requires you to setup Facebook Auth in the Firebase console,
* as well as uncommenting the SDK includes in include.gradle (Android) and Podfile (iOS).
*
* Note that this works well on iOS, but Android is work in progress.
*/
FACEBOOK
}
Expand Down
Loading

0 comments on commit 50efca4

Please sign in to comment.