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

Commit ff45125

Browse files
Merge pull request #49 from Jpaagt/custom-auth
Let me merge this now and I will give you commit access to this repo later. First I want to finalize Facebook login before we run into merge conflicts :) Thanks so much for your contribution!
2 parents 1479197 + 1e236d2 commit ff45125

File tree

7 files changed

+371
-63
lines changed

7 files changed

+371
-63
lines changed

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,50 @@ but if you only want to wipe everything at '/users', do this:
223223
```
224224

225225
### login
226-
v 1.1.0 of this plugin adds the capability to log your users in. Either anonymously or by email and password.
226+
v 1.1.0 of this plugin adds the capability to log your users in, either
227+
228+
* anonymously,
229+
230+
* by email and password or
231+
232+
* using a custom token.
233+
227234
You need to add support for those features in your Firebase instance at the 'Login & Auth' tab.
228235

229236
You can expect more login mechanisms to be added in the future.
230237

238+
#### Listening to auth state changes
239+
As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user)
240+
241+
> The recommended way to get the current user is by setting a listener on the Auth object
242+
243+
To listen to auth state changes you can register a listener like this:
244+
245+
```js
246+
var listener = {
247+
onAuthStateChanged: function(data) {
248+
console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
249+
if (data.loggedIn) {
250+
console.log("User info", data.user);
251+
}
252+
},
253+
thisArg: this
254+
};
255+
256+
firebase.addAuthStateListener(listener);
257+
```
258+
259+
To stop listening to auth state changed:
260+
261+
```js
262+
firebase.removeAuthStateListener(listener);
263+
```
264+
265+
To check if already listening to auth state changes
266+
```js
267+
firebase.removeAuthStateListener(listener);
268+
```
269+
231270
#### Anonymous login
232271
```js
233272
firebase.login({
@@ -262,6 +301,26 @@ You can expect more login mechanisms to be added in the future.
262301
)
263302
```
264303

304+
#### Custom login
305+
Use this login type to authenticate against firebase using a token generated by your own backend server.
306+
See these [instructions on how to generate the authentication token](https://firebase.google.com/docs/auth/server).
307+
308+
```js
309+
firebase.login({
310+
// note that you need to generate the login token in your existing backend server
311+
type: firebase.LoginType.CUSTOM,
312+
token: token
313+
}).then(
314+
function (result) {
315+
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
316+
JSON.stringify(result);
317+
},
318+
function (errorMessage) {
319+
console.log(errorMessage);
320+
}
321+
)
322+
```
323+
265324
#### Creating a Password account
266325
```js
267326
firebase.createUser({

es6-promise.d.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/* tslint:disable */
2+
// Type definitions for es6-promise
3+
// Project: https://github.com/jakearchibald/ES6-Promise
4+
// Definitions by: François de Campredon <https://github.com/fdecampredon/>
5+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
6+
interface Thenable<R> {
7+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => Thenable<U>): Thenable<U>;
8+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => U): Thenable<U>;
9+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
10+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => Thenable<U>): Thenable<U>;
11+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Thenable<U>;
12+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => void): Thenable<U>;
13+
}
14+
15+
interface Promise<R> extends Thenable<R> {
16+
/**
17+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
18+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
19+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
20+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
21+
* If an error is thrown in the callback, the returned promise rejects with that error.
22+
* @param onFulfilled called when/if "promise" resolves
23+
* @param onRejected called when/if "promise" rejects
24+
*/
25+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => Thenable<U>): Promise<U>;
26+
/**
27+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
28+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
29+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
30+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
31+
* If an error is thrown in the callback, the returned promise rejects with that error.
32+
* @param onFulfilled called when/if "promise" resolves
33+
* @param onRejected called when/if "promise" rejects
34+
*/
35+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => U): Promise<U>;
36+
/**
37+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
38+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
39+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
40+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
41+
* If an error is thrown in the callback, the returned promise rejects with that error.
42+
* @param onFulfilled called when/if "promise" resolves
43+
* @param onRejected called when/if "promise" rejects
44+
*/
45+
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => void): Promise<U>;
46+
/**
47+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
48+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
49+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
50+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
51+
* If an error is thrown in the callback, the returned promise rejects with that error.
52+
* @param onFulfilled called when/if "promise" resolves
53+
* @param onRejected called when/if "promise" rejects
54+
*/
55+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => Thenable<U>): Promise<U>;
56+
/**
57+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
58+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
59+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
60+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
61+
* If an error is thrown in the callback, the returned promise rejects with that error.
62+
* @param onFulfilled called when/if "promise" resolves
63+
* @param onRejected called when/if "promise" rejects
64+
*/
65+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Promise<U>;
66+
/**
67+
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
68+
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
69+
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
70+
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
71+
* If an error is thrown in the callback, the returned promise rejects with that error.
72+
* @param onFulfilled called when/if "promise" resolves
73+
* @param onRejected called when/if "promise" rejects
74+
*/
75+
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => void): Promise<U>;
76+
77+
/**
78+
* Sugar for promise.then(undefined, onRejected)
79+
* @param onRejected called when/if "promise" rejects
80+
*/
81+
catch<U>(onRejected?: (error: any) => Thenable<U>): Promise<U>;
82+
/**
83+
* Sugar for promise.then(undefined, onRejected)
84+
* @param onRejected called when/if "promise" rejects
85+
*/
86+
catch<U>(onRejected?: (error: any) => U): Promise<U>;
87+
/**
88+
* Sugar for promise.then(undefined, onRejected)
89+
* @param onRejected called when/if "promise" rejects
90+
*/
91+
catch<U>(onRejected?: (error: any) => void): Promise<U>;
92+
}
93+
94+
interface PromiseConstructor {
95+
/**
96+
* A reference to the prototype.
97+
*/
98+
prototype: Promise<any>;
99+
100+
/**
101+
* Creates a new Promise.
102+
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
103+
* a resolve callback used resolve the promise with a value or the result of another promise,
104+
* and a reject callback used to reject the promise with a provided reason or error.
105+
*/
106+
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
107+
108+
/**
109+
* Returns promise (only if promise.constructor == Promise)
110+
*/
111+
cast<R>(promise: Promise<R>): Promise<R>;
112+
/**
113+
* Make a promise that fulfills to obj.
114+
*/
115+
cast<R>(object: R): Promise<R>;
116+
117+
/**
118+
* Make a new promise from the thenable.
119+
* A thenable is promise-like in as far as it has a "then" method.
120+
* This also creates a new promise if you pass it a genuine JavaScript promise, making it less efficient for casting than Promise.cast.
121+
*/
122+
resolve<R>(thenable?: Thenable<R>): Promise<R>;
123+
/**
124+
* Make a promise that fulfills to obj. Same as Promise.cast(obj) in this situation.
125+
*/
126+
resolve<R>(object?: R): Promise<R>;
127+
128+
/**
129+
* Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error
130+
*/
131+
reject(error: any): Promise<any>;
132+
133+
/**
134+
* Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects.
135+
* the array passed to all can be a mixture of promise-like objects and other objects.
136+
* The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.
137+
*/
138+
all<R>(promises: Promise<R>[]): Promise<R[]>;
139+
140+
/**
141+
* Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
142+
*/
143+
race<R>(promises: Promise<R>[]): Promise<R>;
144+
}
145+
146+
declare var Promise: PromiseConstructor;

firebase-common.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ declare module "nativescript-plugin-firebase" {
1111
/**
1212
* This requires you to pass in email and password properties as well.
1313
*/
14-
PASSWORD
14+
PASSWORD,
15+
/**
16+
* This requires you to pass either an authentication token generated by your backend server
17+
* or the tokenProviderFn function that returns a promise to provide the token.
18+
* See: https://firebase.google.com/docs/auth/server
19+
*/
20+
CUSTOM
1521
}
1622

1723
/**

firebase-common.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var firebase = {};
22

33
firebase.LoginType = {
44
ANONYMOUS: "anonymous",
5-
PASSWORD: "password"
5+
PASSWORD: "password",
6+
CUSTOM: "custom"
67
};
78

89
firebase.QueryOrderByType = {
@@ -25,4 +26,40 @@ firebase.QueryRangeType = {
2526

2627
firebase.instance = null;
2728

29+
firebase.authStateListeners = [];
30+
31+
firebase.addAuthStateListener = function(listener) {
32+
if (firebase.authStateListeners.indexOf(listener) === -1) {
33+
firebase.authStateListeners.push(listener);
34+
}
35+
return true;
36+
};
37+
38+
firebase.removeAuthStateListener = function(listener) {
39+
var index = firebase.authStateListeners.indexOf(listener);
40+
if (index >= 0) {
41+
firebase.authStateListeners.splice(index, 1);
42+
} else {
43+
return false;
44+
}
45+
};
46+
47+
firebase.hasAuthStateListener = function(listener) {
48+
return firebase.authStateListeners.indexOf(listener) >= 0;
49+
}
50+
51+
firebase.notifyAuthStateListeners = function(data) {
52+
firebase.authStateListeners.forEach(function (listener) {
53+
try {
54+
if (listener.thisArg) {
55+
listener.onAuthStateChanged.apply(thisArg, data);
56+
} else {
57+
listener.onAuthStateChanged(data);
58+
}
59+
} catch (ex) {
60+
console.error("Firebase AuthStateListener failed to trigger", listener, ex);
61+
}
62+
});
63+
}
64+
2865
module.exports = firebase;

0 commit comments

Comments
 (0)