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 verify OTP received via email #620

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,57 @@ AuthenticationClient.prototype.requestEmailCode = function(data, cb) {
return this.passwordless.sendEmail(data, cb);
};

/**
* Verify the given OTP which was sent on the given email.
*
* @method verifyEmailCode
* @memberOf module:auth.AuthenticationClient.prototype
*
* @example <caption>
* Given the user credentials (`email` and `otp`), authenticates
* with the provider using the `/oauth/token` endpoint. Upon successful
* authentication, returns a JSON object containing the `access_token` and
* `id_token`.
* </caption>
*
* var data = {
* username: '{EMAIL}'
alexesprit marked this conversation as resolved.
Show resolved Hide resolved
* otp: '{VERIFICATION_CODE}'
* };
*
* auth0.verifyEmailCode(data, function (err) {
* if (err) {
* // Handle error.
* }
* });
*
* @example <caption>
* The user data object has the following structure.
* </caption>
*
* {
* id_token: String,
* access_token: String,
* token_type: String
* }
*
* @param {Object} data Credentials object.
* @param {String} data.email Email.
* @param {String} data.otp Verification code.
* @param {Function} [cb] Method callback.
*
* @return {Promise|undefined}
*/
AuthenticationClient.prototype.verifyEmailCode = function(data, cb) {
var translatedData = {
username: data.email || data.username,
alexesprit marked this conversation as resolved.
Show resolved Hide resolved
realm: 'email',
otp: data.otp
};

return this.passwordless.signIn(translatedData, cb);
};

/**
* Start passwordless flow sending an SMS.
*
Expand Down
36 changes: 36 additions & 0 deletions test/auth/authentication-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,40 @@ describe('AuthenticationClient', function() {
this.client.verifySMSCode({ phone_number: '123', password: 'code' }, this.callback);
});
});

describe(`verifyEmailCode`, () => {
before(function() {
this.client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' });
this.passwordlessMock = sinon.mock(this.client.passwordless);
this.callback = function() {};
});
it('should call signIn with otp if provided', function() {
this.passwordlessMock
.expects('signIn')
.once()
.withExactArgs(
{
username: '123',
realm: 'email',
otp: 'code'
},
this.callback
);
this.client.verifyEmailCode({ email: '123', otp: 'code' }, this.callback);
});
it('should call signIn with otp if provided', function() {
this.passwordlessMock
.expects('signIn')
.once()
.withExactArgs(
{
username: '123',
realm: 'email',
otp: 'code'
},
this.callback
);
this.client.verifyEmailCode({ username: '123', otp: 'code' }, this.callback);
});
});
});