Skip to content

Commit

Permalink
feat(routes): DELETE /accounts/{id}
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 28, 2015
1 parent fa393d4 commit 3491d9f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
11 changes: 10 additions & 1 deletion api/accounts/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ module.exports = removeAccount

var deleteAccount = require('../../utils/account/delete')

function removeAccount (state, username, options) {
function removeAccount (state, idOrObject, options) {
var id
var username
if (typeof idOrObject === 'string') {
id = idOrObject
} else {
id = idOrObject.id
username = idOrObject.username
}
return new Promise(function (resolve, reject) {
deleteAccount({
couchUrl: state.url,
id: id,
username: username,
bearerToken: options.bearerToken,
includeProfile: options.include === 'account.profile'
Expand Down
4 changes: 3 additions & 1 deletion routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ function accountRoutes (server, options, next) {
})

.then(function (session) {
return accounts.remove(session.account.username, {
return accounts.remove({
username: session.account.username
}, {
bearerToken: sessionId,
include: request.query.include
})
Expand Down
28 changes: 27 additions & 1 deletion routes/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,37 @@ function accountRoutes (server, options, next) {
}
}

var deleteAccountRoute = {
method: 'DELETE',
path: prefix + '/accounts/{id}',
config: {
auth: false,
validate: {
headers: validations.bearerTokenHeader,
failAction: joiFailAction
}
},
handler: function (request, reply) {
var sessionId = toBearerToken(request)

return accounts.remove(request.params.id, {
bearerToken: sessionId
})

.then(function (/* json */) {
reply().code(204)
})

.catch(reply)
}
}

server.route([
postAccountsRoute,
getAccountsRoute,
getAccountRoute,
patchAccountRoute
patchAccountRoute,
deleteAccountRoute
])

next()
Expand Down
27 changes: 27 additions & 0 deletions utils/account/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ function deleteAccount (options, callback) {
timeout: 10000 // 10 seconds
})

if (!options.username) {
return request.get({
url: '/_users/_design/byId/_view/byId?key=' + options.id,
headers: {
cookie: 'AuthSession=' + options.bearerToken
}
}, function (error, response, body) {
if (error) {
return callback(Boom.wrap(error))
}

if (response.statusCode >= 400) {
return callback(Boom.create(response.statusCode, body.reason))
}

if (body.rows.length === 0) {
return callback(Boom.notFound())
}
options.username = body.rows[0].id.substr('org.couchdb.user:'.length)
return sendDeleteRequest(request, options, callback)
})
}

sendDeleteRequest(request, options, callback)
}

function sendDeleteRequest (request, options, callback) {
request.del({
url: '/_users/org.couchdb.user:' + encodeURIComponent(options.username),
headers: {
Expand Down

0 comments on commit 3491d9f

Please sign in to comment.