Skip to content

Commit

Permalink
Release 2.1.2: Really support last attempted URL properly on expired …
Browse files Browse the repository at this point in the history
…authentication

Correction of the permit function and adding fallback for
getLastAttemptedUrl behavior along with removal of the
clearLastAttemptedUrl method. Worked through all validations in an
application and finally proved out how this should behave to support
re-authentication processing and not lose the target URL as hops happen.
  • Loading branch information
justinsa committed Oct 21, 2017
1 parent cc95b28 commit 2093ecf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ $authentication.permit('X', ['Y', 'Z'], [['A']]) === $authentication.permit('X',
$authentication.getConfiguration();
```

### getLastAttemptedUrl()
### getLastAttemptedUrl(fallback)
```JAVASCRIPT
// Return the last attempted url value.
// Return the last attempted url value, or fallback if value is undefined or tracking is disabled,.
$authentication.getLastAttemptedUrl();
```

Expand All @@ -216,12 +216,6 @@ $authentication.getLastAttemptedUrl();
$authentication.setLastAttemptedUrl();
```

### clearLastAttemptedUrl(fallback)
```JAVASCRIPT
// Return the last attempted url value, or fallback if tracking is disabled, and clear the value from storage.
$authentication.clearLastAttemptedUrl();
```

### store()
```JAVASCRIPT
// Returns the configured storage service.
Expand Down
27 changes: 9 additions & 18 deletions ng-authentication-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
* example if you need to pass through details of the user that was logged in.
*/
loginConfirmed: function (data) {
var targetUrl = this.clearLastAttemptedUrl(configuration.onLoginRedirectUrl);
var targetUrl = this.getLastAttemptedUrl(configuration.onLoginRedirectUrl);
storageService().set(configuration.profileStorageKey, data);
configuration.reauthentication.timer = setInterval(configuration.reauthentication.fn, configuration.reauthentication.timeout);
$rootScope.$broadcast(configuration.events.loginConfirmed, data);
Expand Down Expand Up @@ -199,7 +199,7 @@
* call this function to indicate that unauthentication is required.
*/
logoutConfirmed: function () {
var targetUrl = this.clearLastAttemptedUrl(configuration.onLogoutRedirectUrl);
var targetUrl = this.getLastAttemptedUrl(configuration.onLogoutRedirectUrl);
storageService().remove(configuration.profileStorageKey);
$window.clearInterval(configuration.reauthentication.timer);
configuration.reauthentication.timer = undefined;
Expand Down Expand Up @@ -283,7 +283,6 @@
$location.url(url);
$rootScope.$broadcast(event, _.toArray(arguments));
}
this.clearLastAttemptedUrl();
},

/**
Expand All @@ -294,10 +293,14 @@
},

/**
* returns the last attempted url value.
* returns the last attempted url value, or fallback if value is undefined or tracking is disabled,.
*/
getLastAttemptedUrl: function () {
return storageService().get(configuration.lastAttemptedUrlStorageKey);
getLastAttemptedUrl: function (fallback) {
var value = storageService().get(configuration.lastAttemptedUrlStorageKey);
if (configuration.trackLastAttemptedUrl !== true || !_.isString(value)) {
value = fallback;
}
return value;
},

/**
Expand All @@ -307,18 +310,6 @@
return storageService().set(configuration.lastAttemptedUrlStorageKey, value);
},

/**
* returns the last attempted url value, or fallback if tracking is disabled, and clears the value from storage.
*/
clearLastAttemptedUrl: function (fallback) {
var value = this.getLastAttemptedUrl();
if (configuration.trackLastAttemptedUrl !== true || !_.isString(value)) {
value = fallback;
}
storageService().remove(configuration.lastAttemptedUrlStorageKey);
return value;
},

/**
* returns the configured storage service.
*/
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": "ng-authentication-service",
"version": "2.1.1",
"version": "2.1.2",
"author": "Justin Saunders (https://github.com/justinsa)",
"contributors": [
"Josh Mackey (https://github.com/joshmackey)",
Expand Down
12 changes: 11 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$authentication', function () {
'permit',
'getConfiguration',
'getLastAttemptedUrl',
'clearLastAttemptedUrl',
'setLastAttemptedUrl',
'reauthenticate',
'$onLoginConfirmed',
'$onLoginRequired',
Expand Down Expand Up @@ -685,6 +685,16 @@ describe('$authentication', function () {
$rootScope.$broadcast.calledWithExactly('event:auth-notAuthenticated', ['a', 'b']).should.be.true();
})
);

it('should store the last attempted url when the user gets redirected for authentication',
inject(function ($authentication, $location) {
$location.url('/about?a=b#anchor-tag');
$location.url().should.match('/about?a=b#anchor-tag');
$authentication.permit('a', 'b');
$location.url().should.match('/notauthenticated');
$authentication.getLastAttemptedUrl().should.equal('/about?a=b#anchor-tag');
})
);
});

describe('with an authenticated user', function () {
Expand Down

0 comments on commit 2093ecf

Please sign in to comment.