|
| 1 | +<template> |
| 2 | + <v-card> |
| 3 | + <v-card-title>API Keys</v-card-title> |
| 4 | + <v-card-text> |
| 5 | + <v-data-table :items="Object.values(apiKeys)" :headers="apiKeyHeaders"> |
| 6 | + <template v-slot:item.created="{ item }"> |
| 7 | + {{ moment(item.created).format("YYYY-MM-DD HH:mm:ss ZZ") }} |
| 8 | + </template> |
| 9 | + <template v-slot:item.scopes="{ item }"> |
| 10 | + <v-chip v-for="scope in item.scopes" density="compact" color="green">{{ scope }}</v-chip> |
| 11 | + </template> |
| 12 | + <template v-slot:item.last_used="{ item }"> |
| 13 | + <span v-if="item.last_used"> |
| 14 | + {{ moment(item.last_used).format("YYYY-MM-DD HH:mm:ss ZZ") }} |
| 15 | + </span> |
| 16 | + <span v-else>Never</span> |
| 17 | + </template> |
| 18 | + |
| 19 | + <template v-slot:item.actions="{ item }"> |
| 20 | + <v-dialog max-width="420px"> |
| 21 | + <template v-slot:activator="{ props }"> |
| 22 | + <v-btn v-bind="props" class="ms-2" size="small" variant="outlined" color="error" |
| 23 | + ><v-icon>mdi-delete</v-icon></v-btn |
| 24 | + > |
| 25 | + </template> |
| 26 | + <template v-slot:default="{ isActive }"> |
| 27 | + <v-card> |
| 28 | + <v-card-title class="text-h6">Delete API key !'{{ item.name }}'?</v-card-title> |
| 29 | + <v-card-text>This action is irreversible</v-card-text> |
| 30 | + <v-card-actions> |
| 31 | + <v-spacer></v-spacer> |
| 32 | + <v-btn color="light" variant="text" @click="isActive.value = false">Cancel</v-btn> |
| 33 | + <v-btn color="error" variant="flat" @click="deleteApiKey(item)">Delete</v-btn> |
| 34 | + <v-spacer></v-spacer> |
| 35 | + </v-card-actions> |
| 36 | + </v-card> |
| 37 | + </template> |
| 38 | + </v-dialog> |
| 39 | + </template> |
| 40 | + |
| 41 | + <template v-slot:item.enabled="{ item }"> |
| 42 | + <v-switch |
| 43 | + density="compact" |
| 44 | + v-model="item.enabled" |
| 45 | + color="green" |
| 46 | + hide-details |
| 47 | + @click="toggleApiKey(item)" |
| 48 | + ></v-switch> |
| 49 | + </template> |
| 50 | + </v-data-table> |
| 51 | + </v-card-text> |
| 52 | + <v-card-actions> |
| 53 | + <v-dialog max-width="420px"> |
| 54 | + <template v-slot:activator="{ props }"> |
| 55 | + <v-btn v-bind="props" class="ms-2" size="small" variant="outlined" prepend-icon="mdi-plus">New API key</v-btn> |
| 56 | + </template> |
| 57 | + <template v-slot:default="{ isActive }"> |
| 58 | + <v-card> |
| 59 | + <v-card-title class="text-h6">New API key</v-card-title> |
| 60 | + <v-card-text v-if="!newApiKeyToken"> |
| 61 | + <v-text-field density="compact" label="Key name" v-model="newApiKey.name"></v-text-field> |
| 62 | + <v-combobox |
| 63 | + v-model="newApiKey.scopes" |
| 64 | + label="Scopes" |
| 65 | + :items="['all']" |
| 66 | + clearable |
| 67 | + multiple |
| 68 | + density="compact" |
| 69 | + :delimiters="[',', ' ', ';']" |
| 70 | + /> |
| 71 | + </v-card-text> |
| 72 | + <v-card-text v-else> |
| 73 | + This is the only time your API key will be accessable. Please copy it to your clipboard now and store it |
| 74 | + in a secure location. You'll need it to access the API and won't be able to retrieve it again later. |
| 75 | + <br /> |
| 76 | + <div class="d-flex justify-center"> |
| 77 | + <v-btn class="mt-6" @click="copyApiKey(newApiKeyToken, isActive)" prepend-icon="mdi-content-copy" |
| 78 | + >Copy API key to clipboard</v-btn |
| 79 | + > |
| 80 | + </div> |
| 81 | + </v-card-text> |
| 82 | + <v-card-actions v-if="!newApiKeyToken"> |
| 83 | + <v-spacer></v-spacer> |
| 84 | + <v-btn color="light" variant="text" @click="isActive.value = false">Cancel</v-btn> |
| 85 | + <v-btn variant="flat" @click="addApiKey()">Create</v-btn> |
| 86 | + <v-spacer></v-spacer> |
| 87 | + </v-card-actions> |
| 88 | + </v-card> |
| 89 | + </template> |
| 90 | + </v-dialog> |
| 91 | + </v-card-actions> |
| 92 | + </v-card> |
| 93 | +</template> |
| 94 | + |
| 95 | +<script lang="ts" setup> |
| 96 | +import moment from "moment"; |
| 97 | +import axios from "axios"; |
| 98 | +</script> |
| 99 | + |
| 100 | +<script lang="ts"> |
| 101 | +export default { |
| 102 | + name: "ApiKeyManagement", |
| 103 | + data() { |
| 104 | + return { |
| 105 | + apiKeyHeaders: [ |
| 106 | + { title: "Created", value: "created" }, |
| 107 | + { title: "Name", value: "name" }, |
| 108 | + { title: "Scopes", value: "scopes" }, |
| 109 | + { title: "Last used", value: "last_used" }, |
| 110 | + { title: "Enabled", value: "enabled" }, |
| 111 | + { title: "Actions", value: "actions" } |
| 112 | + ], |
| 113 | + newApiKey: { |
| 114 | + name: "", |
| 115 | + scopes: [] |
| 116 | + }, |
| 117 | + newApiKeyToken: null |
| 118 | + }; |
| 119 | + }, |
| 120 | + props: { |
| 121 | + profileId: { |
| 122 | + type: String, |
| 123 | + default: null |
| 124 | + }, |
| 125 | + apiKeys: { |
| 126 | + type: Object, |
| 127 | + default: {} |
| 128 | + } |
| 129 | + }, |
| 130 | + methods: { |
| 131 | + addApiKey() { |
| 132 | + axios |
| 133 | + .post(`/api/v2/users/new-api-key`, { |
| 134 | + user_id: this.profileId, |
| 135 | + name: this.newApiKey.name, |
| 136 | + scopes: this.newApiKey.scopes |
| 137 | + }) |
| 138 | + .then(response => { |
| 139 | + this.newApiKeyToken = response.data.token; |
| 140 | + this.$emit("apiKeyUpdate", response.data.api_keys); |
| 141 | + }) |
| 142 | + .catch(error => { |
| 143 | + console.log(error); |
| 144 | + }) |
| 145 | + .finally(() => {}); |
| 146 | + }, |
| 147 | + toggleApiKey(item) { |
| 148 | + axios |
| 149 | + .post(`/api/v2/users/toggle-api-key`, { user_id: this.profileId, name: item.name }) |
| 150 | + .then(response => { |
| 151 | + item.enabled = response.data.enabled; |
| 152 | + }) |
| 153 | + .catch(error => { |
| 154 | + console.log(error); |
| 155 | + }) |
| 156 | + .finally(() => {}); |
| 157 | + }, |
| 158 | + deleteApiKey(item) { |
| 159 | + axios |
| 160 | + .post(`/api/v2/users/delete-api-key`, { user_id: this.profileId, name: item.name }) |
| 161 | + .then(response => { |
| 162 | + this.$emit("apiKeyUpdate", response.data.api_keys); |
| 163 | + this.$eventBus.emit("displayMessage", { |
| 164 | + message: `API key '${item.name}' succesfully deleted.`, |
| 165 | + status: "success" |
| 166 | + }); |
| 167 | + }) |
| 168 | + .catch(error => { |
| 169 | + console.log(error); |
| 170 | + }) |
| 171 | + .finally(() => {}); |
| 172 | + }, |
| 173 | + copyApiKey(text, isActive) { |
| 174 | + navigator.clipboard.writeText(text); |
| 175 | + this.$eventBus.emit("displayMessage", { |
| 176 | + status: "info", |
| 177 | + message: "API key copied to clipboard!" |
| 178 | + }); |
| 179 | + this.newApiKeyToken = null; |
| 180 | + isActive.value = false; |
| 181 | + } |
| 182 | + } |
| 183 | +}; |
| 184 | +</script> |
| 185 | + |
| 186 | +<style scoped> |
| 187 | +/* Add custom styles here */ |
| 188 | +</style> |
0 commit comments