Skip to content

Commit d15ad49

Browse files
author
Timothy E. Johansson
committed
Added support for storing tokens in localStorage with PhoneGap.
1 parent 1ec1b6e commit d15ad49

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ user.onAuthenticationRequired(function(toState, toParams) {
244244
```
245245
For the Angular router the target route is passed to the handler. For the UI router, the target state and the target state parameters are passed.
246246

247-
248247
To add required permissions to a route, use the `hasPermission` property and specify all the permissions as an array, like this:
249248

250249
```javascript
@@ -278,6 +277,17 @@ user.onAccessDenied(function(user, state, params) {
278277

279278
For the Angular router the target route is passed as second parameter to the handler. For the UI router, the target state and the target state parameters are passed as parameters two and three.
280279

280+
After a successful login, the default behaviour is to transition to the default route. To change this behaviour, override the `authenticationSuccessHandler` and return false to abort the transition. Override the default `authenticationSuccessHandler` like this:
281+
282+
```javascript
283+
user.onAuthenticationSuccess(function() {
284+
// hide popup, do transition, ...
285+
286+
// return true to transition to the default route.
287+
// return false to do nothing.
288+
return false;
289+
});
290+
```
281291

282292
## Loaders
283293

angularjs.userapp.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ var userappModule = angular.module('UserApp', []);
88
// Expose the UserApp API
99
userappModule.value('UserApp', UserApp);
1010

11+
/**
12+
* Token storage, default is in a cookie. The PhoneGap integration will override this
13+
* to store the token in localStorage instead.
14+
*/
15+
UserApp.tokenStorage || (UserApp.tokenStorage = {
16+
get: function() {
17+
return Cookies.get('ua_session_token');
18+
},
19+
set: function(token) {
20+
Cookies.set('ua_session_token', token, { expires: new Date(new Date().getTime() + 31536000000) });
21+
},
22+
remove: function() {
23+
Cookies.expire('ua_session_token');
24+
}
25+
});
26+
1127
// Directive error handler
1228
var handleError = function(scope, error, elementId) {
1329
if (!error) {
@@ -34,7 +50,7 @@ var userappModule = angular.module('UserApp', []);
3450
var user = {};
3551
var appId = null;
3652
var options = null;
37-
var token = Cookies.get('ua_session_token');
53+
var token = UserApp.tokenStorage.get();
3854
var status = { authorized: false, authenticated: false };
3955
var heartBeatInterval = -1;
4056
var defaultRoute = null;
@@ -305,7 +321,7 @@ var userappModule = angular.module('UserApp', []);
305321
status.authenticated = false;
306322

307323
// Remove session cookie
308-
Cookies.expire('ua_session_token');
324+
UserApp.tokenStorage.remove();
309325

310326
for (var key in user) {
311327
delete user[key];
@@ -341,7 +357,7 @@ var userappModule = angular.module('UserApp', []);
341357
$rootScope.user.authenticated = true;
342358

343359
// Set session cookie
344-
Cookies.set('ua_session_token', token, { expires: new Date(new Date().getTime() + 31536000000) });
360+
UserApp.tokenStorage.set(token);
345361
}
346362

347363
return token;

bower.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "userapp-angular",
3-
"version": "1.6.2",
4-
"main": "./angularjs.userapp.js",
2+
"name": "userapp-angular",
3+
"version": "1.7.0",
4+
"main": "./angularjs.userapp.js",
55
"dependencies": {
66
"userapp": "latest"
77
}

0 commit comments

Comments
 (0)