Skip to content

Commit

Permalink
merge all token.delete methodes into a single methode
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA authored and julien-f committed Jun 18, 2022
1 parent b41efc4 commit 174be3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
40 changes: 4 additions & 36 deletions packages/xo-server/src/api/token.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,19 @@ create.params = {

// -------------------------------------------------------------------

async function delete_({ token: id }) {
await this.deleteAuthenticationToken(id)
async function delete_({ patern, tokens }) {
await this.deleteAuthenticationTokens({ filter: patern ?? { id: { __or: tokens } } })
}

export { delete_ as delete }

delete_.description = 'delete an existing authentication token'

delete_.params = {
token: { type: 'string' },
tokens: { type: 'array', optional: true, items: { type: 'string' } },
patern: { type: 'object', optional: true },
}

// -------------------------------------------------------------------

// TODO: an user should be able to delete its own tokens.
export async function delete_many({ tokens }) {
await this.deleteAuthenticationTokens({ filter: { id: { __or: tokens } } })
}

delete_many.description = 'delete a collection of existing authentication token'

delete_many.permission = 'admin'

delete_many.params = {
tokens: { type: 'array', items: { type: 'string' } },
}

// -------------------------------------------------------------------

export async function deleteAll({ except }) {
await this.deleteAuthenticationTokens({
filter: {
user_id: this.apiContext.user.id,
id: {
__not: except,
},
},
})
}

deleteAll.description = 'delete all tokens of the current user except the current one'

deleteAll.params = {
except: { type: 'string', optional: true },
}

// -------------------------------------------------------------------

Expand Down
15 changes: 10 additions & 5 deletions packages/xo-web/src/common/xo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2785,7 +2785,14 @@ export const deleteUsers = users =>
export const editUser = (user, { email, password, permission }) =>
_call('user.set', { id: resolveId(user), email, password, permission })::tap(subscribeUsers.forceRefresh)

const _signOutFromEverywhereElse = () => _call('token.deleteAll', { except: cookies.get('token') })
const _signOutFromEverywhereElse = () =>
_call('token.delete', {
patern: {
id: {
__not: cookies.get('token'),
},
},
})

export const signOutFromEverywhereElse = () =>
_signOutFromEverywhereElse().then(
Expand Down Expand Up @@ -2914,7 +2921,7 @@ export const deleteAuthToken = async ({ id }) => {
icon: 'user',
title: _('deleteAuthTokenConfirm'),
})
return _call('token.delete', { token: id })::tap(subscribeUserAuthTokens.forceRefresh)
return _call('token.delete', { tokens: [id] })::tap(subscribeUserAuthTokens.forceRefresh)
}

export const deleteAuthTokens = async tokens => {
Expand All @@ -2925,9 +2932,7 @@ export const deleteAuthTokens = async tokens => {
icon: 'user',
title: _('deleteAuthTokensConfirm', { nTokens: tokens.length }),
})
return _call('token.delete_many', { tokens: tokens.map(token => token.id) })::tap(
subscribeUserAuthTokens.forceRefresh
)
return _call('token.delete', { tokens: tokens.map(token => token.id) })::tap(subscribeUserAuthTokens.forceRefresh)
}

export const editAuthToken = ({ description, id }) =>
Expand Down

0 comments on commit 174be3c

Please sign in to comment.