Skip to content

Commit 8e187e7

Browse files
committed
feat(users): add deleteUser
1 parent 80ed0e0 commit 8e187e7

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getSession, logIn, logOut } from "./authentication";
44
import {
55
changePassword,
66
changeUsername,
7+
deleteUser,
78
getUser,
89
getUsersDatabaseUrl,
910
putUser,
@@ -23,6 +24,7 @@ plugin.signup = signUp;
2324
plugin.signUp = signUp;
2425
plugin.getUser = getUser;
2526
plugin.putUser = putUser;
27+
plugin.deleteUser = deleteUser;
2628
plugin.changePassword = changePassword;
2729
plugin.changeUsername = changeUsername;
2830

src/users.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,33 @@ var putUser = toPromise(function (username, opts, callback) {
115115
});
116116
});
117117

118+
var deleteUser = toPromise(function (username, opts, callback) {
119+
var db = this;
120+
if (typeof callback === 'undefined') {
121+
callback = typeof opts === 'undefined' ? username : opts;
122+
opts = {};
123+
}
124+
if (['http', 'https'].indexOf(db.type()) === -1) {
125+
return callback(new AuthError('This plugin only works for the http/https adapter. ' +
126+
'So you should use new PouchDB("http://mysite.com:5984/mydb") instead.'));
127+
} else if (!username) {
128+
return callback(new AuthError('You must provide a username'));
129+
}
130+
131+
db.getUser(username, opts, function (error, user) {
132+
if (error) {
133+
return callback(error);
134+
}
135+
136+
var url = getUsersUrl(db) + '/' + encodeURIComponent(user._id) + '?rev=' + user._rev;
137+
var ajaxOpts = assign({
138+
method: 'DELETE',
139+
url: url,
140+
}, opts.ajax || {});
141+
ajaxCore(ajaxOpts, wrapError(callback));
142+
});
143+
});
144+
118145
var changePassword = toPromise(function (username, password, opts, callback) {
119146
var db = this;
120147
if (typeof callback === 'undefined') {
@@ -209,4 +236,12 @@ var changeUsername = toPromise(function (oldUsername, newUsername, opts, callbac
209236
}).catch(callback);
210237
});
211238

212-
export { getUsersDatabaseUrl, signUp, getUser, putUser, changePassword, changeUsername };
239+
export {
240+
getUsersDatabaseUrl,
241+
signUp,
242+
getUser,
243+
putUser,
244+
deleteUser,
245+
changePassword,
246+
changeUsername,
247+
};

test/test.users.js

+23
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ describe('users', function () {
132132
});
133133
});
134134

135+
it('Test delete user', function () {
136+
return db.signup('robin', 'dickgrayson').then(function () {
137+
return db.login('robin', 'dickgrayson');
138+
}).then(function () {
139+
return db.getUser('robin');
140+
}).then(function (res) {
141+
res.name.should.equal('robin');
142+
}).then(function () {
143+
return db.deleteUser('robin');
144+
}).then(function (res) {
145+
res.ok.should.equal(true);
146+
}).then(function () {
147+
return db.login('robin', 'dickgrayson');
148+
}).then(function () {
149+
throw new Error('shouldn\'t have worked');
150+
}, function (err) {
151+
should.exist(err);
152+
err.error.should.equal('unauthorized');
153+
err.reason.should.equal('Name or password is incorrect.');
154+
err.status.should.equal(401);
155+
});
156+
});
157+
135158
it('Test change password', function () {
136159
return db.signup('spiderman', 'will-forget').then(function () {
137160
return db.changePassword('spiderman', 'will-remember').then(function (res) {

0 commit comments

Comments
 (0)