Skip to content

Commit

Permalink
Add method to verify OTP received via email (#620)
Browse files Browse the repository at this point in the history
* Add method to verify OTP sent to email

* Use only `email` property in `verifyEmailCode` method
  • Loading branch information
alexesprit authored May 17, 2021
1 parent 8713a17 commit 71f4dd0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
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 = {
* email: '{EMAIL}',
* 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,
realm: 'email',
otp: data.otp
};

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

/**
* Start passwordless flow sending an SMS.
*
Expand Down
22 changes: 22 additions & 0 deletions test/auth/authentication-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,26 @@ 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);
});
});
});

0 comments on commit 71f4dd0

Please sign in to comment.