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

Commit 50efca4

Browse files
- 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)
1 parent ebb7724 commit 50efca4

File tree

8 files changed

+220
-120
lines changed

8 files changed

+220
-120
lines changed

README.md

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ And here's the comprehensive list of supported functions:
6565
var firebase = require("nativescript-plugin-firebase");
6666

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

228-
* anonymously,
229-
230-
* by email and password or
231-
232-
* using a custom token.
234+
- anonymously,
235+
- by email and password, or
236+
- using a custom token,
237+
- using Facebook (experimental)
233238

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

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

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

245250
```js
246251
var listener = {
@@ -263,8 +268,9 @@ To stop listening to auth state changed:
263268
```
264269

265270
To check if already listening to auth state changes
271+
266272
```js
267-
firebase.removeAuthStateListener(listener);
273+
hasAuthStateListener(listener);
268274
```
269275

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

324-
#### Creating a Password account
330+
#### Custom login
331+
Use this login type to authenticate against firebase using a token generated by your own backend server.
332+
See these [instructions on how to generate the authentication token](https://firebase.google.com/docs/auth/server).
333+
325334
```js
326-
firebase.createUser({
327-
328-
password: 'firebase'
335+
firebase.login({
336+
// note that you need to generate the login token in your existing backend server
337+
type: firebase.LoginType.CUSTOM,
338+
token: token
329339
}).then(
330340
function (result) {
331-
dialogs.alert({
332-
title: "User created",
333-
message: "userid: " + result.key,
334-
okButtonText: "Nice!"
335-
})
341+
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
342+
JSON.stringify(result);
343+
},
344+
function (errorMessage) {
345+
console.log(errorMessage);
346+
}
347+
)
348+
```
349+
350+
#### Facebook login
351+
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).
352+
353+
```js
354+
firebase.login({
355+
type: firebase.LoginType.FACEBOOK
356+
}).then(
357+
function (result) {
358+
JSON.stringify(result);
336359
},
337360
function (errorMessage) {
338-
dialogs.alert({
339-
title: "No user created",
340-
message: errorMessage,
341-
okButtonText: "OK, got it"
342-
})
361+
console.log(errorMessage);
343362
}
344363
)
345364
```
346365

347-
#### Resetting a password
366+
### Resetting a password
348367
```js
349368
firebase.resetPassword({
350369
@@ -359,7 +378,7 @@ See these [instructions on how to generate the authentication token](https://fir
359378
)
360379
```
361380

362-
#### Changing a password
381+
### Changing a password
363382
```js
364383
firebase.changePassword({
365384
@@ -391,7 +410,7 @@ com.android.dex.DexIndexOverflowException: method ID not in..
391410

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

396415
```
397416
android {
@@ -423,7 +442,7 @@ or start a geny emulator first and do: `tns run android`
423442
## Future work
424443
- Add support for `removeEventListener`.
425444
- Possibly add more login mechanisms.
426-
445+
- Add other Firebase 3.x SDK features (there's already a few feature requests in the GitHub issue tracker.
427446

428447
## Credits
429448
The starting point for this plugin was [this great Gist](https://gist.github.com/jbristowe/c89a7bcae7fc9a035ee7) by [John Bristowe](https://github.com/jbristowe).

firebase-common.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ declare module "nativescript-plugin-firebase" {
1919
*/
2020
CUSTOM,
2121
/**
22-
* This requires you to setup Facebook Auth in the Firebase console.
22+
* This requires you to setup Facebook Auth in the Firebase console,
23+
* as well as uncommenting the SDK includes in include.gradle (Android) and Podfile (iOS).
24+
*
25+
* Note that this works well on iOS, but Android is work in progress.
2326
*/
2427
FACEBOOK
2528
}

0 commit comments

Comments
 (0)