This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added auth state listener support in the
init
function
- Auto-login revisiting users - Added Facebook authentication (iOS is done, Android is a bit WIP due to build issues)
- Loading branch information
1 parent
ebb7724
commit 50efca4
Showing
8 changed files
with
220 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
@@ -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. | ||
|
||
|
@@ -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 = { | ||
|
@@ -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 | ||
|
@@ -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]' | ||
|
@@ -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]', | ||
|
@@ -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 { | ||
|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.