Skip to content

Commit

Permalink
EddyVerbruggen#2 Add Login / Logout
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyverbruggen@gmail.com committed Dec 12, 2015
1 parent bb76149 commit f8653ee
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 49 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ If you can spare 41 seconds, please check this video of the [demo app](https://g
### Use when
* you need to store JSON data in the cloud,
* you want to sync that data to other devices and platforms,
* you want to optionally protect that data by having users log in,
* you want to update clients at the moment the data changes (think chat and multiplayer games).

## Prerequisites
Expand Down Expand Up @@ -38,7 +39,7 @@ And here's the comprehensive list of supported functions:
firebase.init({
url: 'https://resplendent-fire-4211.firebaseio.com'
}).then(
function (result) {
function (instance) {
console.log("firebase.init done");
},
function (error) {
Expand Down Expand Up @@ -133,6 +134,54 @@ but if you only want to wipe everything at '/users', do this:
firebase.remove("/users");
```

### login
v 1.1.0 of this plugin adds the capability to log your users in. Either anonymously or by email and password.
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.

#### Anonymous login
```js
firebase.login({
// note that you need to enable anonymous login in your firebase instance
type: firebase.loginType.ANONYMOUS
}).then(
function (result) {
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
JSON.stringify(result);
},
function (errorMessage) {
console.log(errorMessage);
}
)
};
```

#### Password login
```js
firebase.login({
// note that you need to enable email-password login in your firebase instance
type: firebase.loginType.PASSWORD,
email: 'useraccount@provider.com',
password: 'theirpassword'
}).then(
function (result) {
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
JSON.stringify(result);
},
function (errorMessage) {
console.log(errorMessage);
}
)
};
```

### logout
Shouldn't be more complicated than:

```js
firebase.logout();
```

## Pro tips

Expand Down
18 changes: 18 additions & 0 deletions firebase-common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
var firebase = {};

firebase.loginType = {
ANONYMOUS: "anonymous",
PASSWORD: "password"
};

firebase.instance = null;

// this implementation is actually the same for both platforms, woohoo :)
firebase.logout = function (arg) {
return new Promise(function (resolve, reject) {
try {
instance.unauth();
resolve();
} catch (ex) {
console.log("Error in firebase.logout: " + ex);
reject(ex);
}
});
};

module.exports = firebase;
37 changes: 24 additions & 13 deletions firebase.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,44 @@ firebase.init = function (arg) {
var Firebase = com.firebase.client.Firebase;
Firebase.setAndroidContext(appModule.android.context);
instance = new Firebase(arg.url);
resolve();
resolve(instance);
} catch (ex) {
console.log("Error in firebase.init: " + ex);
reject(ex);
}
});
};

// TODO
firebase.login = function (arg) {
return new Promise(function (resolve, reject) {
try {

var authorizer = new com.firebase.client.Firebase.AuthResultHandler({
onAuthenticated: function (authData) {
resolve(authData);
resolve({
uid: authData.getUid(),
provider: authData.getProvider(),
expiresAtUnixEpochSeconds: authData.getExpires(),
profileImageURL: authData.getProviderData().get("profileImageURL"),
token: authData.getToken()
});
},
onAuthenticationError: function (firebaseError) {
reject({
error: firebaseError,
errorMessage: firebaseError.message
});
reject(firebaseError.getMessage());
}
});

instance.authAnonymously(authorizer);
var type = arg.type;
if (type === firebase.loginType.ANONYMOUS) {
instance.authAnonymously(authorizer);
} else if (type === firebase.loginType.PASSWORD) {
if (!arg.email || !arg.password) {
reject("Auth type emailandpassword requires an email and password argument");
} else {
instance.authWithPassword(arg.email, arg.password, authorizer);
}
} else {
reject ("Unsupported auth type: " + type);
}
} catch (ex) {
console.log("Error in firebase.login: " + ex);
reject(ex);
Expand All @@ -141,7 +153,7 @@ firebase.addChildEventListener = function (updateCallback, path) {
}
});
instance.child(path).addChildEventListener(listener);
resolve(listener);
resolve();
} catch (ex) {
console.log("Error in firebase.addChildEventListener: " + ex);
reject(ex);
Expand All @@ -158,13 +170,12 @@ firebase.addValueEventListener = function (updateCallback, path) {
},
onCancelled: function (firebaseError) {
updateCallback({
error: firebaseError,
errorMessage: firebaseError.message
error: firebaseError.getMessage()
});
}
});
instance.child(path).addValueEventListener(listener);
resolve(listener);
resolve();
} catch (ex) {
console.log("Error in firebase.addValueEventListener: " + ex);
reject(ex);
Expand Down
86 changes: 52 additions & 34 deletions firebase.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,6 @@ firebase.toJsObject = function(objCObj) {
return node;
};

firebase.addChildEventListener = function (updateCallback, path) {
return new Promise(function (resolve, reject) {
try {
var where = instance;
if (path !== undefined) {
where = instance.childByAppendingPath(path);
}
where.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildAdded', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildRemoved', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildChanged', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildMoved, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildMoved', snapshot));
});
resolve();
} catch (ex) {
console.log("Error in firebase.addChildEventListener: " + ex);
reject(ex);
}
});
};

firebase.getCallbackData = function(type, snapshot) {
return {
type: type,
Expand All @@ -80,7 +53,7 @@ firebase.init = function (arg) {
return new Promise(function (resolve, reject) {
try {
instance = new Firebase(arg.url);
resolve();
resolve(instance);
} catch (ex) {
console.log("Error in firebase.init: " + ex);
reject(ex);
Expand All @@ -91,20 +64,66 @@ firebase.init = function (arg) {
firebase.login = function (arg) {
return new Promise(function (resolve, reject) {
try {
instance.authAnonymouslyWithCompletionBlock(function(error, result) {
var onCompletion = function(error, authData) {
if (error) {
reject(error);
reject(error.localizedDescription);
} else {
resolve(result);
resolve({
uid: authData.uid,
provider: authData.provider,
expiresAtUnixEpochSeconds: authData.expires,
profileImageURL: authData.providerData["profileImageURL"],
token: authData.token
});
}
});
};

var type = arg.type;
if (type === firebase.loginType.ANONYMOUS) {
instance.authAnonymouslyWithCompletionBlock(onCompletion);
} else if (type === firebase.loginType.PASSWORD) {
if (!arg.email || !arg.password) {
reject("Auth type emailandpassword requires an email and password argument");
} else {
instance.authUserPasswordWithCompletionBlock(arg.email, arg.password, onCompletion);
}
} else {
reject ("Unsupported auth type: " + type);
}
} catch (ex) {
console.log("Error in firebase.login: " + ex);
reject(ex);
}
});
};

firebase.addChildEventListener = function (updateCallback, path) {
return new Promise(function (resolve, reject) {
try {
var where = instance;
if (path !== undefined) {
where = instance.childByAppendingPath(path);
}
where.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildAdded', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildRemoved', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildChanged', snapshot));
});
where.observeEventTypeWithBlock(FEventType.FEventTypeChildMoved, function (snapshot) {
updateCallback(firebase.getCallbackData('ChildMoved', snapshot));
});
resolve();
} catch (ex) {
console.log("Error in firebase.addChildEventListener: " + ex);
reject(ex);
}
});
};

firebase.addValueEventListener = function (updateCallback, path) {
return new Promise(function (resolve, reject) {
try {
Expand All @@ -119,8 +138,7 @@ firebase.addValueEventListener = function (updateCallback, path) {
},
function (firebaseError) {
updateCallback({
error: firebaseError,
errorMessage: firebaseError.localizedDescription
error: firebaseError.localizedDescription
});
});
resolve();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-plugin-firebase",
"version": "1.1.0-dev",
"version": "1.1.0",
"description" : "Fire. Base. Firebase!",
"main" : "firebase.js",
"nativescript": {
Expand Down

0 comments on commit f8653ee

Please sign in to comment.