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

Commit

Permalink
Merge pull request #49 from Jpaagt/custom-auth
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
EddyVerbruggen committed Jun 10, 2016
2 parents 0d5c538 + 37c04cd commit 2052bdd
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 63 deletions.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,50 @@ 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 or by email and password.
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.

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

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

#### Listening to auth state changes
As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user)

> 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:

```js
var listener = {
onAuthStateChanged: function(data) {
console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
if (data.loggedIn) {
console.log("User info", data.user);
}
},
thisArg: this
};

firebase.addAuthStateListener(listener);
```

To stop listening to auth state changed:

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

To check if already listening to auth state changes
```js
firebase.removeAuthStateListener(listener);
```

#### Anonymous login
```js
firebase.login({
Expand Down Expand Up @@ -262,6 +301,26 @@ You can expect more login mechanisms to be added in the future.
)
```

#### 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.login({
// note that you need to generate the login token in your existing backend server
type: firebase.LoginType.CUSTOM,
token: token
}).then(
function (result) {
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
JSON.stringify(result);
},
function (errorMessage) {
console.log(errorMessage);
}
)
```

#### Creating a Password account
```js
firebase.createUser({
Expand Down
146 changes: 146 additions & 0 deletions es6-promise.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* tslint:disable */
// Type definitions for es6-promise
// Project: https://github.com/jakearchibald/ES6-Promise
// Definitions by: François de Campredon <https://github.com/fdecampredon/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface Thenable<R> {
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => Thenable<U>): Thenable<U>;
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => U): Thenable<U>;
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => Thenable<U>): Thenable<U>;
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Thenable<U>;
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => void): Thenable<U>;
}

interface Promise<R> extends Thenable<R> {
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => Thenable<U>): Promise<U>;
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => U): Promise<U>;
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => Thenable<U>, onRejected?: (error: any) => void): Promise<U>;
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => Thenable<U>): Promise<U>;
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Promise<U>;
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => void): Promise<U>;

/**
* Sugar for promise.then(undefined, onRejected)
* @param onRejected called when/if "promise" rejects
*/
catch<U>(onRejected?: (error: any) => Thenable<U>): Promise<U>;
/**
* Sugar for promise.then(undefined, onRejected)
* @param onRejected called when/if "promise" rejects
*/
catch<U>(onRejected?: (error: any) => U): Promise<U>;
/**
* Sugar for promise.then(undefined, onRejected)
* @param onRejected called when/if "promise" rejects
*/
catch<U>(onRejected?: (error: any) => void): Promise<U>;
}

interface PromiseConstructor {
/**
* A reference to the prototype.
*/
prototype: Promise<any>;

/**
* Creates a new Promise.
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
* a resolve callback used resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;

/**
* Returns promise (only if promise.constructor == Promise)
*/
cast<R>(promise: Promise<R>): Promise<R>;
/**
* Make a promise that fulfills to obj.
*/
cast<R>(object: R): Promise<R>;

/**
* Make a new promise from the thenable.
* A thenable is promise-like in as far as it has a "then" method.
* This also creates a new promise if you pass it a genuine JavaScript promise, making it less efficient for casting than Promise.cast.
*/
resolve<R>(thenable?: Thenable<R>): Promise<R>;
/**
* Make a promise that fulfills to obj. Same as Promise.cast(obj) in this situation.
*/
resolve<R>(object?: R): Promise<R>;

/**
* Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error
*/
reject(error: any): Promise<any>;

/**
* Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects.
* the array passed to all can be a mixture of promise-like objects and other objects.
* The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.
*/
all<R>(promises: Promise<R>[]): Promise<R[]>;

/**
* Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
*/
race<R>(promises: Promise<R>[]): Promise<R>;
}

declare var Promise: PromiseConstructor;
8 changes: 7 additions & 1 deletion firebase-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ declare module "nativescript-plugin-firebase" {
/**
* This requires you to pass in email and password properties as well.
*/
PASSWORD
PASSWORD,
/**
* This requires you to pass either an authentication token generated by your backend server
* or the tokenProviderFn function that returns a promise to provide the token.
* See: https://firebase.google.com/docs/auth/server
*/
CUSTOM
}

/**
Expand Down
39 changes: 38 additions & 1 deletion firebase-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var firebase = {};

firebase.LoginType = {
ANONYMOUS: "anonymous",
PASSWORD: "password"
PASSWORD: "password",
CUSTOM: "custom"
};

firebase.QueryOrderByType = {
Expand All @@ -25,4 +26,40 @@ firebase.QueryRangeType = {

firebase.instance = null;

firebase.authStateListeners = [];

firebase.addAuthStateListener = function(listener) {
if (firebase.authStateListeners.indexOf(listener) === -1) {
firebase.authStateListeners.push(listener);
}
return true;
};

firebase.removeAuthStateListener = function(listener) {
var index = firebase.authStateListeners.indexOf(listener);
if (index >= 0) {
firebase.authStateListeners.splice(index, 1);
} else {
return false;
}
};

firebase.hasAuthStateListener = function(listener) {
return firebase.authStateListeners.indexOf(listener) >= 0;
}

firebase.notifyAuthStateListeners = function(data) {
firebase.authStateListeners.forEach(function (listener) {
try {
if (listener.thisArg) {
listener.onAuthStateChanged.apply(thisArg, data);
} else {
listener.onAuthStateChanged(data);
}
} catch (ex) {
console.error("Firebase AuthStateListener failed to trigger", listener, ex);
}
});
}

module.exports = firebase;
Loading

0 comments on commit 2052bdd

Please sign in to comment.