Skip to content

Commit

Permalink
Add parameter to login method to allow dynamic redirect
Browse files Browse the repository at this point in the history
An extra parameter on the `$auth.login` method allows redirect
URLs to be determined in runtime.
  • Loading branch information
amullins83 committed Mar 13, 2015
1 parent 7385b25 commit 4f5d88f
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 16 deletions.
30 changes: 21 additions & 9 deletions examples/client/vendor/satellizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@
return oauth.authenticate(name, false, userData);
};

$auth.login = function(user) {
return local.login(user);
$auth.login = function(user, redirect) {
return local.login(user, redirect);
};

$auth.signup = function(user) {
Expand Down Expand Up @@ -278,7 +278,7 @@
}
};

shared.setToken = function(response, isLinking) {
shared.setToken = function(response, redirect) {
var accessToken = response && response.access_token;
var token;

Expand All @@ -304,9 +304,12 @@

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
if (config.loginRedirect && !redirect) {
$location.path(config.loginRedirect);
}
else if (redirect && angular.isString(redirect)) {
$location.path(encodeURI(redirect));
}
};

shared.removeToken = function() {
Expand Down Expand Up @@ -390,10 +393,10 @@
function($q, $http, $location, utils, shared, config) {
var local = {};

local.login = function(user) {
local.login = function(user, redirect) {
return $http.post(config.loginUrl, user)
.then(function(response) {
shared.setToken(response);
shared.setToken(response, redirect);
return response;
});
};
Expand Down Expand Up @@ -525,14 +528,15 @@
var defaults = {
url: null,
name: null,
popupOptions: null
popupOptions: null,
redirectUri: null
};

var oauth1 = {};

oauth1.open = function(options, userData) {
angular.extend(defaults, options);
return popup.open(defaults.url, defaults.popupOptions)
return popup.open(defaults.url, defaults.popupOptions, defaults.redirectUri)
.then(function(response) {
return oauth1.exchangeForToken(response, userData);
});
Expand Down Expand Up @@ -597,6 +601,7 @@
var parser = document.createElement('a');
parser.href = event.url;

if(parser.search || parser.hash){
var queryParams = parser.search.substring(1).replace(/\/$/, '');
var hashParams = parser.hash.substring(1).replace(/\/$/, '');
var hash = utils.parseQueryString(hashParams);
Expand All @@ -607,10 +612,17 @@
if (qs.error) {
deferred.reject({ error: qs.error });
} else {
deferred.resolve({ code: qs.code });
deferred.resolve(qs);
}

popupWindow.close();
}
});
popupWindow.addEventListener('exit', function() {
deferred.reject({data: 'Provider Popup was closed'});
});
popupWindow.addEventListener('loaderror', function() {
deferred.reject({data: 'Authorization Failed'});
});

return deferred.promise;
Expand Down
15 changes: 9 additions & 6 deletions satellizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@
return oauth.authenticate(name, false, userData);
};

$auth.login = function(user) {
return local.login(user);
$auth.login = function(user, redirect) {
return local.login(user, redirect);
};

$auth.signup = function(user) {
Expand Down Expand Up @@ -278,7 +278,7 @@
}
};

shared.setToken = function(response, isLinking) {
shared.setToken = function(response, redirect) {
var accessToken = response && response.access_token;
var token;

Expand All @@ -304,9 +304,12 @@

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
if (config.loginRedirect && !redirect) {
$location.path(config.loginRedirect);
}
else if (redirect && angular.isString(redirect)) {
$location.path(encodeURI(redirect));
}
};

shared.removeToken = function() {
Expand Down Expand Up @@ -390,10 +393,10 @@
function($q, $http, $location, utils, shared, config) {
var local = {};

local.login = function(user) {
local.login = function(user, redirect) {
return $http.post(config.loginUrl, user)
.then(function(response) {
shared.setToken(response);
shared.setToken(response, redirect);
return response;
});
};
Expand Down
2 changes: 1 addition & 1 deletion satellizer.min.js

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions test/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ describe('$auth', function() {
// expect(angular.isFunction($auth.login)).toBe(true);
});

it('should be able to call loign with a redirect parameter', function() {
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Il9pZCI6IjUzZjYxZTEwNmZjNjFhNmMxM2I1Mjc4ZCIsImVtYWlsIjoic2FoYXQ_QG1lLmNvbSIsIl9fdiI6MH0sImlhdCI6MTQwODgyMTA5MTY3NiwiZXhwIjoxNDA5NDI1ODkxNjc2fQ.0l-ql-ZVjHiILMcMegNb3bNqapt3TZwjHy_ieduioiQ';
var user = {
email: '[email protected]',
password: '1234'
};
var redirect = '/new/path';
this.config.tokenRoot = 'tokenRoot';
this.config.loginUrl = '/auth/login';
var response = {
tokenRoot: {
token: token
}
};

this.$httpBackend.expectPOST('/auth/login').respond(response);

this.$auth.login(user, redirect);

this.$httpBackend.flush();

expect(this.$location.path()).toBe(redirect);
});
});

describe('signup()', function() {
Expand Down
24 changes: 24 additions & 0 deletions test/local.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe('satellizer.local', function() {
// });
// });

it('should redirect to the redirect parameter on successful login', function() {
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Il9pZCI6IjUzZjYxZTEwNmZjNjFhNmMxM2I1Mjc4ZCIsImVtYWlsIjoic2FoYXQ_QG1lLmNvbSIsIl9fdiI6MH0sImlhdCI6MTQwODgyMTA5MTY3NiwiZXhwIjoxNDA5NDI1ODkxNjc2fQ.0l-ql-ZVjHiILMcMegNb3bNqapt3TZwjHy_ieduioiQ';
var user = {
email: '[email protected]',
password: '1234'
};
var redirect = '/new/path';
this.config.tokenRoot = 'tokenRoot';
this.config.loginUrl = '/auth/login';
var response = {
tokenRoot: {
access_token: token
}
};

this.$httpBackend.expectPOST('/auth/login').respond(response);

this.local.login(user, redirect);

this.$httpBackend.flush();

expect(this.$location.path()).toBe(redirect);
});

it('should fail login with incorrect credentials', function() {
var result = null;
var user = {
Expand Down
60 changes: 60 additions & 0 deletions test/shared.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,66 @@ describe('satellizer.shared', function() {
expect(token).toEqual(this.shared.getToken());
});

it('should redirect when the redirect parameter is set', function() {
this.config.tokenRoot = 'tokenRoot'
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb…YzMn0.YATZN37JENCQWeNAoN4M7KxJl7OAIJL4ka_fSM_gYkE'

var response = {
data: {
tokenRoot: {
access_token: token
}
}
};

var redirect = "/new/path";

this.shared.setToken(response, redirect);

expect(this.$location.path()).toEqual(redirect);
});

it('should redirect when the loginRedirect config property is set', function() {
var configRedirect = "/my-login-url";
this.config.loginRedirect = configRedirect;
this.config.tokenRoot = 'tokenRoot';
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb…YzMn0.YATZN37JENCQWeNAoN4M7KxJl7OAIJL4ka_fSM_gYkE'

var response = {
data: {
tokenRoot: {
access_token: token
}
}
};

this.shared.setToken(response);

expect(this.$location.path()).toEqual(configRedirect);
});


it('should redirect to the redirect parameter even if the loginRedirect config property is set', function() {
var configRedirect = "/my-login-url";
this.config.loginRedirect = configRedirect;
this.config.tokenRoot = 'tokenRoot';
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb…YzMn0.YATZN37JENCQWeNAoN4M7KxJl7OAIJL4ka_fSM_gYkE'

var response = {
data: {
tokenRoot: {
access_token: token
}
}
};

var redirect = "/new/path";

this.shared.setToken(response, redirect);

expect(this.$location.path()).toEqual(redirect);
});

});

describe('removeToken()', function() {
Expand Down

0 comments on commit 4f5d88f

Please sign in to comment.