| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-container class="narrow-container"> | 
					
						
							|  |  |  |     <BasePageTitle divider> | 
					
						
							|  |  |  |       <template #header> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <v-img | 
					
						
							|  |  |  |           width="100%" | 
					
						
							|  |  |  |           max-height="200px" | 
					
						
							|  |  |  |           max-width="200px" | 
					
						
							|  |  |  |           :src="require('~/static/svgs/manage-api-tokens.svg')" | 
					
						
							|  |  |  |         /> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       </template> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       <template #title> | 
					
						
							|  |  |  |         {{ $t("settings.token.api-tokens") }} | 
					
						
							|  |  |  |       </template> | 
					
						
							|  |  |  |       {{ $t('settings.token.you-have-token-count', user.tokens!.length) }} | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |     </BasePageTitle> | 
					
						
							|  |  |  |     <section class="d-flex justify-center"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       <v-card | 
					
						
							|  |  |  |         class="mt-4" | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |         width="100%" | 
					
						
							|  |  |  |         flat | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       > | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |         <v-card-text class="px-0"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           <v-form | 
					
						
							|  |  |  |             ref="domNewTokenForm" | 
					
						
							|  |  |  |             @submit.prevent | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             <v-text-field | 
					
						
							|  |  |  |               v-model="name" | 
					
						
							|  |  |  |               :label="$t('settings.token.token-name')" | 
					
						
							|  |  |  |             /> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |           </v-form> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           <template v-if="createdToken != ''"> | 
					
						
							|  |  |  |             <v-textarea | 
					
						
							|  |  |  |               v-model="createdToken" | 
					
						
							|  |  |  |               class="mb-0 pb-0" | 
					
						
							|  |  |  |               :label="$t('settings.token.api-token')" | 
					
						
							|  |  |  |               readonly | 
					
						
							| 
									
										
										
										
											2021-10-30 15:46:44 -08:00
										 |  |  |               rows="3" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |             <p> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |               {{ | 
					
						
							|  |  |  |                 $t( | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |                   "settings.token.copy-this-token-for-use-with-an-external-application-this-token-will-not-be-viewable-again", | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |                 ) | 
					
						
							|  |  |  |               }} | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |             </p> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |           </template> | 
					
						
							|  |  |  |         </v-card-text> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |         <v-card-actions class="px-0"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           <BaseButton | 
					
						
							|  |  |  |             v-if="createdToken" | 
					
						
							|  |  |  |             cancel | 
					
						
							|  |  |  |             @click="resetCreate()" | 
					
						
							|  |  |  |           > | 
					
						
							|  |  |  |             {{ $t('general.close') }} | 
					
						
							|  |  |  |           </BaseButton> | 
					
						
							|  |  |  |           <v-spacer /> | 
					
						
							|  |  |  |           <AppButtonCopy | 
					
						
							|  |  |  |             v-if="createdToken" | 
					
						
							|  |  |  |             :icon="false" | 
					
						
							|  |  |  |             color="info" | 
					
						
							|  |  |  |             :copy-text="createdToken" | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |           <BaseButton | 
					
						
							|  |  |  |             v-else | 
					
						
							|  |  |  |             key="generate-button" | 
					
						
							|  |  |  |             :disabled="name == ''" | 
					
						
							|  |  |  |             @click="createToken(name)" | 
					
						
							|  |  |  |           > | 
					
						
							| 
									
										
										
										
											2023-03-21 20:45:27 +01:00
										 |  |  |             {{ $t('settings.token.generate') }} | 
					
						
							| 
									
										
										
										
											2021-10-30 15:46:44 -08:00
										 |  |  |           </BaseButton> | 
					
						
							|  |  |  |         </v-card-actions> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       </v-card> | 
					
						
							|  |  |  |     </section> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     <BaseCardSectionTitle | 
					
						
							|  |  |  |       class="mt-10" | 
					
						
							|  |  |  |       :title="$t('settings.token.active-tokens')" | 
					
						
							|  |  |  |     /> | 
					
						
							| 
									
										
										
										
											2023-10-07 21:36:47 +02:00
										 |  |  |     <section class="d-flex flex-column"> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |       <v-list> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       <div | 
					
						
							|  |  |  |         v-for="(token, index) in user.tokens" | 
					
						
							|  |  |  |         :key="index" | 
					
						
							|  |  |  |       > | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |           <v-list-item> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |             <v-list-item-title> | 
					
						
							|  |  |  |               {{ token.name }} | 
					
						
							|  |  |  |             </v-list-item-title> | 
					
						
							|  |  |  |             <v-list-item-subtitle> | 
					
						
							|  |  |  |               {{ $t('general.created-on-date', [$d(new Date(token.createdAt!))]) }} | 
					
						
							|  |  |  |             </v-list-item-subtitle> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |             <template #append> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |               <BaseButton | 
					
						
							|  |  |  |                 delete | 
					
						
							|  |  |  |                 small | 
					
						
							|  |  |  |                 @click="deleteToken(token.id)" | 
					
						
							|  |  |  |               /> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |             </template> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |           </v-list-item> | 
					
						
							| 
									
										
										
										
											2025-06-28 15:59:58 +02:00
										 |  |  |           <v-divider class="mx-2 my-2" /> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </v-list> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |     </section> | 
					
						
							|  |  |  |   </v-container> | 
					
						
							|  |  |  | </template> | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { VForm } from "~/types/auto-forms"; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | export default defineNuxtComponent({ | 
					
						
							|  |  |  |   middleware: ["sidebase-auth", "advanced-only"], | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   setup() { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const i18n = useI18n(); | 
					
						
							|  |  |  |     const $auth = useMealieAuth(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     useSeoMeta({ | 
					
						
							|  |  |  |       title: i18n.t("settings.token.api-tokens"), | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const user = computed(() => { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       return $auth.user.value; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const domNewTokenForm = ref<VForm | null>(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const createdToken = ref(""); | 
					
						
							|  |  |  |     const name = ref(""); | 
					
						
							|  |  |  |     const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function resetCreate() { | 
					
						
							|  |  |  |       createdToken.value = ""; | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |       name.value = ""; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       $auth.refresh(); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function createToken(name: string) { | 
					
						
							|  |  |  |       if (loading.value) { | 
					
						
							|  |  |  |         resetCreate(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (domNewTokenForm?.value?.validate()) { | 
					
						
							|  |  |  |         console.log("Created"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const { data } = await api.users.createAPIToken({ name }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         createdToken.value = data.token; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |     async function deleteToken(id: number) { | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       const { data } = await api.users.deleteAPIToken(id); | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       $auth.refresh(); | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       return data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 15:46:44 -08:00
										 |  |  |     return { createToken, deleteToken, createdToken, loading, name, user, resetCreate }; | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |