Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy E. Johansson committed Apr 25, 2014
1 parent ce80a30 commit fdd4128
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 UserApp, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
userapp-phonegap
UserApp PhoneGap
================

Library that extends the UserApp JavaScript SDKs with functionality that facilitates use with PhoneGap.
This library extends the [UserApp](https://www.userapp.io/) JavaScript SDKs with functionality that facilitates use with [PhoneGap](http://phonegap.com/).

It extends the `UserApp` JavaScript object with methods to create and remove persistent tokens based on the device id.

## Install

Download the `phonegap.userapp.js` file or install with bower: `$ bower install userapp-phonegap`

## Use with the UserApp AngularJS SDK

Automatically sets up a persistent session at login, so that the user only has to sign in once.
Just include `phonegap.userapp.js` after `angularjs.userapp.js`, like this:

```html
<script src="js/angularjs.userapp.js"></script>
<script src="js/phonegap.userapp.js"></script>
```

## Use with the UserApp Ember.js SDK

*Coming soon*

## Use with the UserApp JavaScript SDK

Include `phonegap.userapp.js` after `userapp.client.js`, like this:

```html
<script src="js/userapp.client.js"></script>
<script src="js/phonegap.userapp.js"></script>
```

This will extend the `UserApp` object with the methods `setupPersistentToken(callback)` and `removePersistentToken(callback)`.
After a successful login, call `setupPersistentToken()` to create a persistent token and then set the SDK to use it instead.

```javascript
UserApp.User.login(user, function(error, result) {
if (result) {
if (result.locks && result.locks.length > 0) {
UserApp.setToken(null);
} else {
UserApp.setupPersistentToken(function(error, token) {
if (token) {
UserApp.setToken(token.value);
} else {
UserApp.setToken(null);
}
});
}
}
});
```

And use the method `removePersistentToken()` to destroy the persistent session.

```javascript
UserApp.removePersistentToken(function(error) {
UserApp.setToken(null);
});
```

## Example

See the [Ionic Example]() for a demo app based on [Ionic](http://ionicframework.com/) (AngularJS + PhoneGap) and UserApp.

## Help

Contact us via email at support@userapp.io or visit our [support center](https://help.userapp.io). You can also see the [UserApp documentation](https://app.userapp.io/#/docs/) for more information.

## License

MIT, see LICENSE.
5 changes: 5 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "userapp-phonegap",
"version": "1.0.0",
"main": "./phonegap.userapp.js"
}
81 changes: 81 additions & 0 deletions phonegap.userapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(function() {
// Phonegap device object
var device = window.device || { uuid: 'account-token' };

function findPersistentToken(callback, idOnly) {
if (callback) {
UserApp.Token.search({ fields: ['token_id', 'name'], page_size: 100 }, function(error, tokens) {
if (!error && tokens) {
for (var i = 0; i < tokens.items.length; ++i) {
if (tokens.items[i].name == (device.uuid || 'account-token')) {
if (!idOnly) {
UserApp.Token.get({ token_id: tokens.items[i].token_id }, function(error, token) {
if (!error && token.length == 1) {
callback(null, token[0]);
} else {
callback(error, null);
}
});
} else {
callback(null, tokens.items[i]);
}

return;
}
}
}

callback(error, null);
});
}
}

function createPersistentToken(callback) {
UserApp.Token.save({
name: (device.uuid || 'account-token'),
enabled: true
}, function(error, token){
if (!error) {
callback && callback(null, token);
} else {
callback && callback(error, null);
}
});
}

UserApp.setupPersistentToken = function(callback) {
findPersistentToken(function(error, token) {
if (token) {
callback && callback(null, token);
} else if (!token) {
createPersistentToken(function(error, newToken) {
if (!error && newToken) {
callback && callback(null, newToken);
} else {
callback && callback(error, null);
}
});
} else {
callback && callback(error, null);
}
});
};

UserApp.removePersistentToken = function(callback) {
findPersistentToken(function(error, token) {
if (!error && token) {
UserApp.Token.remove({
token_id: token.token_id
}, function(error, token){
if (!error) {
callback && callback(null, true);
} else {
callback && callback(error, false);
}
});
} else {
callback && callback(error, false);
}
}, true);
};
})();

0 comments on commit fdd4128

Please sign in to comment.