Skip to content

Commit

Permalink
feat(users): add deleteUser
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitjes committed Nov 22, 2017
1 parent 80ed0e0 commit 8e187e7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getSession, logIn, logOut } from "./authentication";
import {
changePassword,
changeUsername,
deleteUser,
getUser,
getUsersDatabaseUrl,
putUser,
Expand All @@ -23,6 +24,7 @@ plugin.signup = signUp;
plugin.signUp = signUp;
plugin.getUser = getUser;
plugin.putUser = putUser;
plugin.deleteUser = deleteUser;
plugin.changePassword = changePassword;
plugin.changeUsername = changeUsername;

Expand Down
37 changes: 36 additions & 1 deletion src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ var putUser = toPromise(function (username, opts, callback) {
});
});

var deleteUser = toPromise(function (username, opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
callback = typeof opts === 'undefined' ? username : opts;
opts = {};
}
if (['http', 'https'].indexOf(db.type()) === -1) {
return callback(new AuthError('This plugin only works for the http/https adapter. ' +
'So you should use new PouchDB("http://mysite.com:5984/mydb") instead.'));
} else if (!username) {
return callback(new AuthError('You must provide a username'));
}

db.getUser(username, opts, function (error, user) {
if (error) {
return callback(error);
}

var url = getUsersUrl(db) + '/' + encodeURIComponent(user._id) + '?rev=' + user._rev;
var ajaxOpts = assign({
method: 'DELETE',
url: url,
}, opts.ajax || {});
ajaxCore(ajaxOpts, wrapError(callback));
});
});

var changePassword = toPromise(function (username, password, opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
Expand Down Expand Up @@ -209,4 +236,12 @@ var changeUsername = toPromise(function (oldUsername, newUsername, opts, callbac
}).catch(callback);
});

export { getUsersDatabaseUrl, signUp, getUser, putUser, changePassword, changeUsername };
export {
getUsersDatabaseUrl,
signUp,
getUser,
putUser,
deleteUser,
changePassword,
changeUsername,
};
23 changes: 23 additions & 0 deletions test/test.users.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ describe('users', function () {
});
});

it('Test delete user', function () {
return db.signup('robin', 'dickgrayson').then(function () {
return db.login('robin', 'dickgrayson');
}).then(function () {
return db.getUser('robin');
}).then(function (res) {
res.name.should.equal('robin');
}).then(function () {
return db.deleteUser('robin');
}).then(function (res) {
res.ok.should.equal(true);
}).then(function () {
return db.login('robin', 'dickgrayson');
}).then(function () {
throw new Error('shouldn\'t have worked');
}, function (err) {
should.exist(err);
err.error.should.equal('unauthorized');
err.reason.should.equal('Name or password is incorrect.');
err.status.should.equal(401);
});
});

it('Test change password', function () {
return db.signup('spiderman', 'will-forget').then(function () {
return db.changePassword('spiderman', 'will-remember').then(function (res) {
Expand Down

0 comments on commit 8e187e7

Please sign in to comment.