diff --git a/.env.json.dist b/.env.json.dist index f54e030..44bc0e2 100644 --- a/.env.json.dist +++ b/.env.json.dist @@ -6,4 +6,5 @@ "REGISTER_EMAIL": "", "REGISTER_USERNAME": "", "REGISTER_PASSWORD": "", + "RESET_CODE": "" } diff --git a/dist/auth.js b/dist/auth.js index b610a0d..67aa3f6 100644 --- a/dist/auth.js +++ b/dist/auth.js @@ -9,6 +9,7 @@ exports.prelogin = prelogin; exports.check = check; exports.forgot = forgot; exports.register = register; +exports.reset = reset; var _fetch = require('./lib/fetch'); @@ -180,4 +181,39 @@ function register() { return (0, _fetch.post)('auth/register', { username: username, email: email, password: password, confirm: confirm }).then(_fetch.postProcess).then(function (message) { return Promise.resolve({ message: message }); }); +} + +/** + * Resets the password + * + * @param {Object} args + * @param {String} args.code Password reset code + * @param {String} args.password The password (min: 8) + * @param {String} args.confirm This should be the same as password + * + * @return {Promise} Resolves true if successful + * + * @example + * auth + * .reset({ code: '', password: '', confirm: '' }) + * .then((success) => success) + * .catch((error) => error.message) + */ +function reset() { + var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + var code = args.code; + var password = args.password; + var confirm = args.confirm; + + if (!(code && password && confirm)) { + return Promise.reject({ message: _error2.default.INVALID_ARGUMENTS }); + } + + if (password !== confirm) { + return Promise.reject({ message: _error2.default.PASSWORD_CONFIRM_NOT_MATCHED }); + } + + return (0, _fetch.post)('auth/reset/' + code, { password: password, confirm: confirm }).then(_fetch.postProcess).then(function () { + return Promise.resolve({ message: 'Successful!' }); + }); } \ No newline at end of file diff --git a/src/auth.js b/src/auth.js index cefec89..607584a 100644 --- a/src/auth.js +++ b/src/auth.js @@ -158,3 +158,35 @@ export function register(args = {}) { .then(postProcess) .then(message => Promise.resolve({ message })); } + +/** + * Resets the password + * + * @param {Object} args + * @param {String} args.code Password reset code + * @param {String} args.password The password (min: 8) + * @param {String} args.confirm This should be the same as password + * + * @return {Promise} Resolves true if successful + * + * @example + * auth + * .reset({ code: '', password: '', confirm: '' }) + * .then((success) => success) + * .catch((error) => error.message) + */ +export function reset(args = {}) { + const { code, password, confirm } = args; + + if (!(code && password && confirm)) { + return Promise.reject({ message: error.INVALID_ARGUMENTS }); + } + + if (password !== confirm) { + return Promise.reject({ message: error.PASSWORD_CONFIRM_NOT_MATCHED }); + } + + return post(`auth/reset/${code}`, { password, confirm }) + .then(postProcess) + .then(() => Promise.resolve({ message: 'Successful!' })); +} diff --git a/test-dist/auth/check.js b/test-dist/auth/check.js index af25e42..545576d 100644 --- a/test-dist/auth/check.js +++ b/test-dist/auth/check.js @@ -1,5 +1,7 @@ 'use strict'; +require('./reset'); + var _mocha = require('mocha'); var _chai = require('chai'); diff --git a/test-dist/auth/forgot.js b/test-dist/auth/forgot.js index 98136b6..0f8770f 100644 --- a/test-dist/auth/forgot.js +++ b/test-dist/auth/forgot.js @@ -1,5 +1,7 @@ 'use strict'; +require('./reset'); + var _mocha = require('mocha'); var _chai = require('chai'); diff --git a/test-dist/auth/reset.js b/test-dist/auth/reset.js new file mode 100644 index 0000000..38c4fb3 --- /dev/null +++ b/test-dist/auth/reset.js @@ -0,0 +1,37 @@ +'use strict'; + +var _mocha = require('mocha'); + +var _chai = require('chai'); + +var _utils = require('../lib/utils'); + +var _auth = require('../../dist/auth'); + +(0, _mocha.describe)('auth.reset', function () { + var code = process.env.RESET_CODE; + var password = process.env.PASSWORD; + var confirm = password; + + (0, _mocha.it)('should be defined,\n returns a Promise,\n and reject if arguments are not valid', function (done) { + _chai.assert.ok(_auth.reset); + _chai.assert.typeOf((0, _auth.reset)(), 'Promise'); + (0, _utils.shouldFail)((0, _auth.reset)(), done); + }); + + (0, _mocha.it)('should fail if code is invalid', function (done) { + return (0, _utils.shouldFail)((0, _auth.reset)({ code: 'invalidcode', password: password, confirm: confirm }), done); + }); + + (0, _mocha.it)('should fail if password and confirm are not the same', function (done) { + return (0, _utils.shouldFail)((0, _auth.reset)({ code: code, password: password, confirm: confirm + 1 }), done); + }); + + if (!code) return; // skip the tests below if there is no valid code + + (0, _mocha.it)('should resolve with a success message if successful', function (done) { + return (0, _utils.shouldSucceed)((0, _auth.reset)({ code: code, password: password, confirm: confirm }), function (success) { + return _chai.assert.ok(success.message); + }, done); + }); +}); \ No newline at end of file diff --git a/test/auth/check.js b/test/auth/check.js index dbc2125..b764102 100644 --- a/test/auth/check.js +++ b/test/auth/check.js @@ -1,3 +1,5 @@ +import './reset'; + import { describe, it } from 'mocha'; import { assert } from 'chai'; import { shouldFail, shouldSucceed } from '../lib/utils'; diff --git a/test/auth/forgot.js b/test/auth/forgot.js index 1469c8c..3f60c39 100644 --- a/test/auth/forgot.js +++ b/test/auth/forgot.js @@ -1,3 +1,5 @@ +import './reset'; + import { describe, it } from 'mocha'; import { assert } from 'chai'; import { shouldFail, shouldSucceed } from '../lib/utils'; diff --git a/test/auth/reset.js b/test/auth/reset.js new file mode 100644 index 0000000..71dd938 --- /dev/null +++ b/test/auth/reset.js @@ -0,0 +1,49 @@ +import { describe, it } from 'mocha'; +import { assert } from 'chai'; +import { shouldFail, shouldSucceed } from '../lib/utils'; + +import { reset } from '../../dist/auth'; + +describe('auth.reset', () => { + const code = process.env.RESET_CODE; + const password = process.env.PASSWORD; + const confirm = password; + + it( + `should be defined, + returns a Promise, + and reject if arguments are not valid`, + done => { + assert.ok(reset); + assert.typeOf(reset(), 'Promise'); + shouldFail(reset(), done); + } + ); + + it( + `should fail if code is invalid`, + done => shouldFail( + reset({ code: 'invalidcode', password, confirm }), + done + ) + ); + + it( + `should fail if password and confirm are not the same`, + done => shouldFail( + reset({ code, password, confirm: confirm + 1 }), + done + ) + ); + + if (!code) return; // skip the tests below if there is no valid code + + it( + `should resolve with a success message if successful`, + done => shouldSucceed( + reset({ code, password, confirm }), + success => assert.ok(success.message), + done + ) + ); +});