Files
oauth/resources/js/pages/Tokens.vue
Josep Piguillem 8d5710a726 fix: personal access tokens failed with a 500 in production
Passport needs a "personal access client" to issue personal access tokens,
and only the seeder created it (production runs migrations, not the seeder),
so createToken threw "Personal access client not found".

- New idempotent command passport:ensure-personal-client, run by the
  container entrypoint after the migrations.
- The tokens page now shows an error when the server fails instead of
  doing nothing (es, en, ca).
- Tests: the command creates the client once; tokens can be created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 19:30:40 +02:00

152 lines
4.8 KiB
Vue

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import axios from 'axios';
import { useI18n } from 'vue-i18n';
import type { PersonalAccessToken, Scope } from '../types/models';
const { t } = useI18n();
const tokens = ref<PersonalAccessToken[]>([]);
const scopes = ref<Scope[]>([]);
const loading = ref(true);
const creating = ref(false);
const newToken = ref<string | null>(null);
const form = reactive<{ name: string; scopes: string[] }>({ name: '', scopes: [] });
const errors = ref<Record<string, string[]>>({});
const failed = ref(false);
async function load(): Promise<void> {
loading.value = true;
const [tokensRes, scopesRes] = await Promise.all([
axios.get<PersonalAccessToken[]>('/web-api/personal-access-tokens'),
axios.get<Scope[]>('/web-api/scopes'),
]);
tokens.value = tokensRes.data;
scopes.value = scopesRes.data;
loading.value = false;
}
async function create(): Promise<void> {
creating.value = true;
errors.value = {};
failed.value = false;
newToken.value = null;
try {
const { data } = await axios.post<{ access_token: string }>('/web-api/personal-access-tokens', form);
newToken.value = data.access_token;
form.name = '';
form.scopes = [];
await load();
} catch (e: any) {
errors.value = e.response?.data?.errors ?? {};
// Sin errores de validación (p. ej. un 500): avisar en vez de no hacer nada.
failed.value = !e.response?.data?.errors;
} finally {
creating.value = false;
}
}
async function revoke(token: PersonalAccessToken): Promise<void> {
if (!confirm(t('tokens.revokeConfirm', { name: token.name }))) return;
await axios.delete(`/web-api/personal-access-tokens/${token.id}`);
await load();
}
onMounted(load);
</script>
<template>
<div>
<h1 class="page-title">{{ t('tokens.title') }}</h1>
<p class="page-subtitle">{{ t('tokens.subtitle') }}</p>
<div v-if="newToken" class="banner banner--warning">
<p class="banner__title">{{ t('tokens.tokenWarning') }}</p>
<p class="banner__line tokens__token"><code class="banner__code">{{ newToken }}</code></p>
<button class="banner__close" @click="newToken = null">{{ t('common.close') }}</button>
</div>
<form class="card tokens__form" @submit.prevent="create">
<h2 class="card__title">{{ t('tokens.newToken') }}</h2>
<div class="field">
<label class="field__label">{{ t('tokens.name') }}</label>
<input v-model="form.name" required :placeholder="t('tokens.namePlaceholder')" class="field__input" />
<p v-if="errors.name" class="field__error">{{ errors.name[0] }}</p>
</div>
<div class="field">
<label class="field__label">{{ t('tokens.scopes') }}</label>
<div class="tokens__scopes">
<label v-for="scope in scopes" :key="scope.id" class="field__checkbox-row">
<input type="checkbox" :value="scope.id" v-model="form.scopes" class="field__checkbox" />
{{ scope.id }} &mdash; {{ scope.description }}
</label>
</div>
</div>
<p v-if="failed" class="field__error">{{ t('common.unexpectedError') }}</p>
<div>
<button type="submit" :disabled="creating" class="btn btn--primary">
{{ creating ? t('common.creating') : t('tokens.create') }}
</button>
</div>
</form>
<div class="tokens__list">
<p v-if="loading" class="page-subtitle">{{ t('common.loading') }}</p>
<p v-else-if="!tokens.length" class="page-subtitle">{{ t('tokens.empty') }}</p>
<div v-for="token in tokens" :key="token.id" class="card tokens__item">
<div>
<p class="card__title">{{ token.name }}</p>
<p class="tokens__item-meta">{{ t('tokens.scopesLabel') }}: {{ token.scopes?.join(', ') || t('common.none') }}</p>
</div>
<button @click="revoke(token)" class="link link--danger">{{ t('tokens.revoke') }}</button>
</div>
</div>
</div>
</template>
<style scoped>
.tokens__form {
margin-top: 1.5rem;
display: flex;
flex-direction: column;
gap: 1rem;
}
.tokens__scopes {
margin-top: 0.25rem;
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.tokens__token {
word-break: break-all;
}
.tokens__list {
margin-top: 1.5rem;
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.tokens__item {
display: flex;
align-items: center;
justify-content: space-between;
}
.tokens__item-meta {
font-size: 0.75rem;
color: var(--color-text-muted);
}
</style>