Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(authService): add request options to signup and login as well as…
Browse files Browse the repository at this point in the history
… optional redirecUri overwrite

unobsoletes login/signup with credentials as string arguments
  • Loading branch information
doktordirk committed Apr 12, 2016
1 parent 4d0f8a9 commit e8072e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
45 changes: 24 additions & 21 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,31 +184,30 @@ export class AuthService {
/**
* Signup locally
*
* @param {String|{}} displayName | object with signup data.
* @param {[String]} [email]
* @param {[String]} [password]
* @param {String|{}} displayName | object with signup data.
* @param {[String]|{}} [email | options for post request]
* @param {[String]} [password | redirectUri overwrite]
* @param {[{}]} [options]
* @param {[String]} [redirectUri overwrite]
*
* @return {Promise<response>}
*
*/
signup(displayName, email, password) {
signup(displayName, email, password, options, redirectUri) {
let content;

if (typeof arguments[0] === 'object') {
content = arguments[0];
options = arguments[1];
redirectUri = arguments[2];
} else {
console.warn('DEPRECATED: AuthService.signup(displayName, email, password). Provide an object with signup data instead.');
content = {
'displayName': displayName,
'email': email,
'password': password
};
}
return this._signup(content);
}

_signup(data, redirectUri) {
return this.client.post(this.config.withBase(this.config.signupUrl), data)
return this.client.post(this.config.withBase(this.config.signupUrl), content, options)
.then(response => {
if (this.config.loginOnSignup) {
this.authentication.responseObject = response;
Expand All @@ -222,30 +221,34 @@ export class AuthService {
/**
* login locally. Redirect depending on config
*
* @param {{}} object with login data.
* @param {[String]|{}} email | object with signup data.
* @param {[String]} [password | options for post request]
* @param {[{}]} [options | redirectUri overwrite]]
* @param {[String]} [redirectUri overwrite]
*
* @return {Promise<response>}
*
*/
login(email, password) {
let content = {};
login(email, password, options, redirectUri) {
let content;

if (typeof arguments[1] !== 'string') {
if (typeof arguments[0] === 'object') {
content = arguments[0];
options = arguments[1];
redirectUri = arguments[2];
} else {
console.warn('DEPRECATED: AuthService.login(email, password). Provide an object with login data instead.');
content = {email: email, password: password};
content = {
'email': email,
'password': password
};
options = options;
}

return this._login(content);
}

_login(data, redirectUri) {
if (this.config.clientId) {
data.client_id = this.config.clientId;
}

return this.client.post(this.config.withBase(this.config.loginUrl), data)
return this.client.post(this.config.withBase(this.config.loginUrl), content, options)
.then(response => {
this.authentication.responseObject = response;

Expand Down
24 changes: 17 additions & 7 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,38 +380,44 @@ describe('AuthService', () => {
});
});

it('Should signup with signup data object and not login.', done => {
it('Should signup with signup data object, not login and not redirect.', done => {
authService.config.loginOnSignup = false;
authService.config.signupRedirect = true;

authService.signup({user: 'some', access_token: 'aToken'})
authService.signup({user: 'some', access_token: 'aToken'}, {headers: {Authorization: 'none'}}, false)
.then(response => {
expect(response.path).toBe('/auth/signup');
expect(response.body.user).toBe('some');
expect(response.Authorization).toBe('none');
expect(authService.isAuthenticated()).toBe(false);

expect(authService.getRefreshToken()).toBe(null);
authService.updateToken()
.catch(err => {
expect(err instanceof Error).toBe(true);
authService.config.signupRedirect = false;
done();
});
});
});

it('Should signup with signup data object and login.', done => {
it('Should signup with signup data object, login and not redirect.', done => {
authService.config.loginOnSignup = true;
authService.config.loginRedirect = true;

authService.signup({user: 'some', access_token: 'aToken'})
authService.signup({user: 'some', access_token: 'aToken'}, {headers: {Authorization: 'none'}}, false)
.then(response => {
expect(response.path).toBe('/auth/signup');
expect(response.body.user).toBe('some');
expect(authService.getAccessToken()).toBe('aToken');
expect(response.body.user).toBe('some');
expect(response.Authorization).toBe('none');
expect(authService.isAuthenticated()).toBe(true);

expect(authService.getRefreshToken()).toBe(null);
authService.updateToken()
.catch(err => {
expect(err instanceof Error).toBe(true);
authService.config.loginRedirect = false;
done();
});
});
Expand Down Expand Up @@ -441,18 +447,22 @@ describe('AuthService', () => {
});
});

it('Should login with login data object.', done => {
authService.login({user: 'some', access_token: 'aToken'})
it('Should login with login data object and not redirect.', done => {
authService.config.loginRedirect = true;

authService.login({user: 'some', access_token: 'aToken'}, {headers: {Authorization: 'none'}}, false)
.then(response => {
expect(response.path).toBe('/auth/login');
expect(response.body.user).toBe('some');
expect(response.Authorization).toBe('none');
expect(authService.getAccessToken()).toBe('aToken');
expect(authService.isAuthenticated()).toBe(true);

expect(authService.getRefreshToken()).toBe(null);
authService.updateToken()
.catch(err => {
expect(err instanceof Error).toBe(true);
authService.config.loginRedirect = false;
done();
});
});
Expand Down

0 comments on commit e8072e5

Please sign in to comment.