Fix PAT duplicates on creation & Visible token value not being cleared

This commit is contained in:
Bubka 2023-11-21 16:41:31 +01:00
parent 0ebb352df0
commit 3d1c25a1d1

View File

@ -15,8 +15,8 @@
const isFetching = ref(false) const isFetching = ref(false)
const isRemoteUser = ref(false) const isRemoteUser = ref(false)
const createPATModalIsVisible = ref(false) const createPATModalIsVisible = ref(false)
const accessToken = ref(null) const visibleToken = ref(null)
const token_id = ref(null) const visibleTokenId = ref(null)
onMounted(() => { onMounted(() => {
fetchTokens() fetchTokens()
@ -34,9 +34,10 @@
userService.getPersonalAccessTokens({returnError: true}) userService.getPersonalAccessTokens({returnError: true})
.then(response => { .then(response => {
tokens.value = []
response.data.forEach((data) => { response.data.forEach((data) => {
if (data.id === token_id.value) { if (data.id === visibleTokenId.value) {
data.value = accessToken.value data.value = visibleToken.value
tokens.value.unshift(data) tokens.value.unshift(data)
} }
else { else {
@ -56,8 +57,8 @@
}) })
.finally(() => { .finally(() => {
isFetching.value = false isFetching.value = false
token_id.value = null visibleTokenId.value = null
accessToken.value = null visibleToken.value = null
}) })
} }
@ -66,6 +67,8 @@
* Open the PAT creation view * Open the PAT creation view
*/ */
function showPATcreationForm() { function showPATcreationForm() {
clearTokenValues()
if (isRemoteUser.value) { if (isRemoteUser.value) {
notify.warn({ text: trans('errors.unsupported_with_reverseproxy') }) notify.warn({ text: trans('errors.unsupported_with_reverseproxy') })
} }
@ -78,10 +81,11 @@
function generatePAToken() { function generatePAToken() {
form.post('/oauth/personal-access-tokens') form.post('/oauth/personal-access-tokens')
.then(response => { .then(response => {
accessToken.value = response.data.accessToken visibleToken.value = response.data.accessToken
token_id.value = response.data.token.id visibleTokenId.value = response.data.token.id
fetchTokens() fetchTokens()
createPATModalIsVisible.value = false createPATModalIsVisible.value = false
form.reset()
}) })
} }
@ -99,11 +103,32 @@
} }
} }
/**
* Removes visible tokens data
*/
function clearTokenValues() {
tokens.value.forEach(token => {
token.value = null
})
visibleToken.value = null
}
/**
* Copies data to clipboard and notifies on success
*/
function copyToClipboard(data) { function copyToClipboard(data) {
copy(data) copy(data)
notify.success({ text: trans('commons.copied_to_clipboard') }) notify.success({ text: trans('commons.copied_to_clipboard') })
} }
/**
* Closes & resets the PAT creation form
*/
function cancelPATcreation() {
createPATModalIsVisible.value = false
form.reset()
}
onBeforeRouteLeave((to) => { onBeforeRouteLeave((to) => {
if (! to.name.startsWith('settings.')) { if (! to.name.startsWith('settings.')) {
notify.clear() notify.clear()
@ -172,7 +197,7 @@
</VueButton> </VueButton>
</div> </div>
<div class="control"> <div class="control">
<VueButton @click="createPATModalIsVisible = false" :nativeType="'button'" id="btnCancel" :color="'is-text'"> <VueButton @click="cancelPATcreation" :nativeType="'button'" id="btnCancel" :color="'is-text'">
{{ $t('commons.cancel') }} {{ $t('commons.cancel') }}
</VueButton> </VueButton>
</div> </div>