Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to signup and login using password-realm #277

Merged
merged 1 commit into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ <h2>Console:</h2>
$('.client-login-db').click(function (e) {
e.preventDefault();
webAuth.client.login({
connection: 'tests',
realm: 'tests',
username: $('.client-login-username').val(),
password: $('.client-login-password').val(),
scope: 'openid profile',
Expand Down
24 changes: 24 additions & 0 deletions src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,30 @@ WebAuth.prototype.login = function (options) {
windowHelper.redirect(this.client.buildAuthorizeUrl(params));
};

/**
* Signs up a new user, automatically logs the user in after the signup and returns the user token.
* The login will be done using /oauth/token with password-realm grant type.
*
* @method signupAndLogin
* @param {Object} options: https://auth0.com/docs/api/authentication#!#post--dbconnections-signup
* @param {Function} cb
*/
WebAuth.prototype.signupAndLogin = function (options, cb) {
var _this = this;

return this.client.dbConnection.signup(objectHelper.blacklist(options, ['popupHandler']),
function (err) {
if (err) {
return cb(err);
}
options.realm = options.connection;
if (!options.username) {
options.username = options.email;
}
_this.client.login(options, cb);
});
};

/**
* Redirects to the auth0 logout page
*
Expand Down
135 changes: 135 additions & 0 deletions test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,139 @@ describe('auth0.WebAuth', function () {
});
});
});

context('signup and login', function () {
before(function () {
this.auth0 = new WebAuth({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'token',
_sendTelemetry: false
});
});

afterEach(function () {
request.post.restore();
});

it('should call db-connection signup with all the options', function (done) {

stub(request, 'post', function (url) {

if (url === 'https://me.auth0.com/oauth/token') {
return new RequestMock({
body: {
client_id: '...',
realm: 'the_connection',
grant_type: 'http://auth0.com/oauth/grant-type/password-realm',
username: 'me@example.com',
password: '123456',
scope: 'openid'
},
headers: {
'Content-Type': 'application/json'
},
cb: function(cb) {
cb(null, {
body: {
'token_type': 'Bearer',
'expires_in': 36000,
'id_token': 'eyJ...'
}
});
}
});
}

if (url === 'https://me.auth0.com/dbconnections/signup') {
return new RequestMock({
body: {
client_id: '...',
connection: 'the_connection',
email: 'me@example.com',
password: '123456'
},
headers: {
'Content-Type': 'application/json'
},
cb: function (cb) {
cb(null, {
body: {
_id: '...',
email_verified: false,
email: 'me@example.com'
}
});
}
});
}

throw new Error('Invalid url in request post stub');
});

this.auth0.signupAndLogin({
connection: 'the_connection',
email: 'me@example.com',
password: '123456',
scope: 'openid'
}, function (err, data) {
done();
});
});

it('should propagate signup errors', function (done) {
stub(request, 'post', function (url) {

expect(url).to.be('https://me.auth0.com/dbconnections/signup');

return new RequestMock({
body: {
client_id: '...',
connection: 'the_connection',
email: 'me@example.com',
password: '123456'
},
headers: {
'Content-Type': 'application/json'
},
cb: function (cb) {
cb({
response: {
"statusCode":400,
body: {
"code":"user_exists",
"description":"The user already exists."
}
}
});
}
});
});

this.auth0.signupAndLogin({
connection: 'the_connection',
email: 'me@example.com',
password: '123456',
scope: 'openid'
}, function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: {
response: {
"statusCode":400,
body: {
"code":"user_exists",
"description":"The user already exists."
}
}
},
"code":"user_exists",
"description":"The user already exists.",
"statusCode":400
});
done();
});
});
});
});