|
| 1 | +<template> |
| 2 | + <main> |
| 3 | + <Loading v-if="loading" :message="loading" /> |
| 4 | + <div v-else> |
| 5 | + <div class="row"> |
| 6 | + <div> |
| 7 | + <nuxt-link |
| 8 | + :to="`/users/${$route.params.slug}/access-tokens`" |
| 9 | + aria-label="Back" |
| 10 | + data-balloon-pos="down" |
| 11 | + class="button button--type-icon button--type-back" |
| 12 | + > |
| 13 | + <font-awesome-icon class="icon" icon="arrow-left" fixed-width /> |
| 14 | + </nuxt-link> |
| 15 | + <h1>Access tokens</h1> |
| 16 | + </div> |
| 17 | + <div class="text text--align-right"> |
| 18 | + <button |
| 19 | + aria-label="Refresh" |
| 20 | + data-balloon-pos="down" |
| 21 | + class="button button--type-icon" |
| 22 | + @click="load" |
| 23 | + > |
| 24 | + <font-awesome-icon class="icon" icon="sync" fixed-width /> |
| 25 | + </button> |
| 26 | + </div> |
| 27 | + </div> |
| 28 | + <div v-if="accessToken"> |
| 29 | + <h2>Use access token</h2> |
| 30 | + <Input |
| 31 | + label="access token" |
| 32 | + :value="accessToken.jwtAccessToken" |
| 33 | + disabled |
| 34 | + /> |
| 35 | + <button class="button" @click="copy(accessToken.jwtAccessToken)"> |
| 36 | + <font-awesome-icon class="icon icon--mr-1" icon="copy" /> |
| 37 | + <span v-if="copied">Copied</span> |
| 38 | + <span v-else>Copy</span> |
| 39 | + </button> |
| 40 | + <button |
| 41 | + type="button" |
| 42 | + class="button button--color-danger" |
| 43 | + style="margin-left: 0.5rem" |
| 44 | + @click.prevent="showDelete = accessToken" |
| 45 | + > |
| 46 | + <font-awesome-icon class="icon icon--mr-1" icon="trash" /> |
| 47 | + <span>Delete</span> |
| 48 | + </button> |
| 49 | + <h2>Edit access token</h2> |
| 50 | + <form |
| 51 | + v-meta-ctrl-enter="() => (showUpdate = true)" |
| 52 | + @submit.prevent="() => (showUpdate = true)" |
| 53 | + > |
| 54 | + <Input |
| 55 | + label="Name" |
| 56 | + placeholder="Enter a name for this access token" |
| 57 | + :value="accessToken.name" |
| 58 | + @input="val => (accessToken.name = val)" |
| 59 | + /> |
| 60 | + <CheckList |
| 61 | + label="API restrictions" |
| 62 | + :options="scopes" |
| 63 | + :value="accessToken.scopes" |
| 64 | + @input="val => (accessToken.scopes = val)" |
| 65 | + /> |
| 66 | + <button class="button">Update access token</button> |
| 67 | + </form> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + <transition name="modal"> |
| 71 | + <Confirm v-if="showDelete" :on-close="() => (showDelete = false)"> |
| 72 | + <h2>Are you sure you want to delete this access token?</h2> |
| 73 | + <p> |
| 74 | + Deleting an access token is not reversible, and you'll need to update |
| 75 | + any apps using this key. |
| 76 | + </p> |
| 77 | + <button |
| 78 | + class="button button--color-danger button--state-cta" |
| 79 | + @click="deleteAccessToken(showDelete.id)" |
| 80 | + > |
| 81 | + Yes, delete access token |
| 82 | + </button> |
| 83 | + <button type="button" class="button" @click="showDelete = false"> |
| 84 | + No, don't delete |
| 85 | + </button> |
| 86 | + </Confirm> |
| 87 | + </transition> |
| 88 | + <transition name="modal"> |
| 89 | + <Confirm v-if="showUpdate" :on-close="() => (showUpdate = false)"> |
| 90 | + <h2> |
| 91 | + Are you sure you want to update and regenerate this access token? |
| 92 | + </h2> |
| 93 | + <p> |
| 94 | + Updating your access token will generate a new access token, so you'll |
| 95 | + have to update it wherever you're using it. |
| 96 | + </p> |
| 97 | + <p>The current access token will stop working instantly.</p> |
| 98 | + <button |
| 99 | + class="button button--color-primary button--state-cta" |
| 100 | + @click="updateAccessToken" |
| 101 | + > |
| 102 | + Yes, regenerate access token |
| 103 | + </button> |
| 104 | + <button type="button" class="button" @click="showUpdate = false"> |
| 105 | + No, don't update |
| 106 | + </button> |
| 107 | + </Confirm> |
| 108 | + </transition> |
| 109 | + </main> |
| 110 | +</template> |
| 111 | + |
| 112 | +<script lang="ts"> |
| 113 | +import { Component, Vue, Watch } from "vue-property-decorator"; |
| 114 | +import { mapGetters } from "vuex"; |
| 115 | +import { getAllCountries } from "countries-and-timezones"; |
| 116 | +import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; |
| 117 | +import { library } from "@fortawesome/fontawesome-svg-core"; |
| 118 | +import { |
| 119 | + faPencilAlt, |
| 120 | + faArrowDown, |
| 121 | + faSync, |
| 122 | + faTrash, |
| 123 | + faEye, |
| 124 | + faEyeSlash, |
| 125 | + faArrowLeft, |
| 126 | + faCopy |
| 127 | +} from "@fortawesome/free-solid-svg-icons"; |
| 128 | +import copy from "copy-to-clipboard"; |
| 129 | +import Loading from "@/components/Loading.vue"; |
| 130 | +import Confirm from "@/components/Confirm.vue"; |
| 131 | +import TimeAgo from "@/components/TimeAgo.vue"; |
| 132 | +import LargeMessage from "@/components/LargeMessage.vue"; |
| 133 | +import Input from "@/components/form/Input.vue"; |
| 134 | +import CheckList from "@/components/form/CheckList.vue"; |
| 135 | +import CommaList from "@/components/form/CommaList.vue"; |
| 136 | +import Checkbox from "@/components/form/Checkbox.vue"; |
| 137 | +import Select from "@/components/form/Select.vue"; |
| 138 | +import { User } from "@/types/auth"; |
| 139 | +import { AccessTokens, emptyPagination, AccessToken } from "@/types/users"; |
| 140 | +import translations from "@/locales/en"; |
| 141 | +import { removeNulls } from "@/helpers/crud"; |
| 142 | +const scopes = translations.scopes; |
| 143 | +library.add( |
| 144 | + faPencilAlt, |
| 145 | + faArrowDown, |
| 146 | + faSync, |
| 147 | + faTrash, |
| 148 | + faCopy, |
| 149 | + faEye, |
| 150 | + faEyeSlash, |
| 151 | + faArrowLeft |
| 152 | +); |
| 153 | +
|
| 154 | +@Component({ |
| 155 | + components: { |
| 156 | + Loading, |
| 157 | + Confirm, |
| 158 | + Input, |
| 159 | + TimeAgo, |
| 160 | + CommaList, |
| 161 | + FontAwesomeIcon, |
| 162 | + CheckList, |
| 163 | + Select, |
| 164 | + LargeMessage, |
| 165 | + Checkbox |
| 166 | + }, |
| 167 | + middleware: "auth" |
| 168 | +}) |
| 169 | +export default class ManageSettings extends Vue { |
| 170 | + accessTokens: AccessTokens = emptyPagination; |
| 171 | + showDelete = false; |
| 172 | + showUpdate = false; |
| 173 | + loading = ""; |
| 174 | + accessToken: AccessToken | null = null; |
| 175 | + scopes = scopes; |
| 176 | + copied = false; |
| 177 | +
|
| 178 | + private created() { |
| 179 | + this.accessTokens = { |
| 180 | + ...this.$store.getters["users/accessTokens"](this.$route.params.slug) |
| 181 | + }; |
| 182 | + } |
| 183 | +
|
| 184 | + private load() { |
| 185 | + this.loading = "Loading your access tokens"; |
| 186 | + this.$store |
| 187 | + .dispatch("users/getAccessToken", { |
| 188 | + slug: this.$route.params.slug, |
| 189 | + id: this.$route.params.key |
| 190 | + }) |
| 191 | + .then(accessToken => { |
| 192 | + this.accessToken = { ...accessToken }; |
| 193 | + }) |
| 194 | + .catch(error => { |
| 195 | + throw new Error(error); |
| 196 | + }) |
| 197 | + .finally(() => (this.loading = "")); |
| 198 | + } |
| 199 | +
|
| 200 | + private mounted() { |
| 201 | + this.load(); |
| 202 | + } |
| 203 | +
|
| 204 | + private updateAccessToken() { |
| 205 | + this.showUpdate = false; |
| 206 | + this.loading = "Updating your access token"; |
| 207 | + const accessToken = { ...this.accessToken }; |
| 208 | + if (accessToken) { |
| 209 | + [ |
| 210 | + "jwtAccessToken", |
| 211 | + "userId", |
| 212 | + "expiresAt", |
| 213 | + "createdAt", |
| 214 | + "updatedAt" |
| 215 | + ].forEach(k => delete accessToken[k]); |
| 216 | + } |
| 217 | + this.$store |
| 218 | + .dispatch("users/updateAccessToken", { |
| 219 | + slug: this.$route.params.slug, |
| 220 | + id: this.$route.params.key, |
| 221 | + ...accessToken |
| 222 | + }) |
| 223 | + .then(accessToken => { |
| 224 | + this.accessToken = { ...accessToken }; |
| 225 | + }) |
| 226 | + .catch(error => { |
| 227 | + throw new Error(error); |
| 228 | + }) |
| 229 | + .finally(() => { |
| 230 | + this.loading = ""; |
| 231 | + }); |
| 232 | + } |
| 233 | +
|
| 234 | + private deleteAccessToken(key: number) { |
| 235 | + this.showDelete = false; |
| 236 | + this.loading = "Deleting your access token"; |
| 237 | + this.$store |
| 238 | + .dispatch("users/deleteAccessToken", { |
| 239 | + slug: this.$route.params.slug, |
| 240 | + id: key |
| 241 | + }) |
| 242 | + .then(accessTokens => { |
| 243 | + this.accessTokens = { ...accessTokens }; |
| 244 | + this.$router.push(`/users/${this.$route.params.slug}/access-tokens`); |
| 245 | + }) |
| 246 | + .catch(error => { |
| 247 | + throw new Error(error); |
| 248 | + }) |
| 249 | + .finally(() => (this.loading = "")); |
| 250 | + } |
| 251 | +
|
| 252 | + private copy(text: string) { |
| 253 | + copy(text); |
| 254 | + this.copied = true; |
| 255 | + setTimeout(() => { |
| 256 | + this.copied = false; |
| 257 | + }, 3000); |
| 258 | + } |
| 259 | +} |
| 260 | +</script> |
| 261 | + |
| 262 | +<style lang="scss" scoped></style> |
0 commit comments