From 71f4dd01d404610061b86dba4cdffc7981ed0c72 Mon Sep 17 00:00:00 2001 From: Alexey Date: Tue, 18 May 2021 02:05:44 +0300 Subject: [PATCH] Add method to verify OTP received via email (#620) * Add method to verify OTP sent to email * Use only `email` property in `verifyEmailCode` method --- src/auth/index.js | 51 ++++++++++++++++++++++++ test/auth/authentication-client.tests.js | 22 ++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/auth/index.js b/src/auth/index.js index d2afb1c1a..e0f35409d 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -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 + * 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`. + * + * + * var data = { + * email: '{EMAIL}', + * otp: '{VERIFICATION_CODE}' + * }; + * + * auth0.verifyEmailCode(data, function (err) { + * if (err) { + * // Handle error. + * } + * }); + * + * @example + * The user data object has the following structure. + * + * + * { + * 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. * diff --git a/test/auth/authentication-client.tests.js b/test/auth/authentication-client.tests.js index b02a9c1e6..4f0a744e9 100644 --- a/test/auth/authentication-client.tests.js +++ b/test/auth/authentication-client.tests.js @@ -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); + }); + }); });