Files
PRISM/frontend/src/components/MfaPanel.vue
T
boris.waaubandClaude Opus 5.5 0617fa8dc4 Ajoute les pages de compte au front et le favicon de la SSMG
- Inscription, suivi de l'inscription (lien de confirmation), seconde
  étape de connexion (code à 6 chiffres ou code de secours), mot de passe
  oublié et réinitialisation, page « Mon compte » (mot de passe, 2FA avec
  QR code et codes de secours), d'après les maquettes Penpot.
- Les jetons reçus par e-mail sont retirés de l'URL dès l'arrivée.
- Favicon de la SSMG (ssmg.be) ; retour en haut de page à chaque
  navigation.
- 93 tests Vitest ; README et CLAUDE.md mis à jour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 13:27:34 +01:00

267 lines
7.5 KiB
Vue

<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { api, ApiError } from '@/api/client'
import type { TotpSetup } from '@/api/types'
import { useAuth } from '@/composables/useAuth'
import AlertBanner from './AlertBanner.vue'
import CodeInput from './CodeInput.vue'
import RecoveryCodes from './RecoveryCodes.vue'
type Mode = 'idle' | 'setup' | 'codes' | 'disable' | 'regenerate'
const { user, load } = useAuth()
const { t } = useI18n()
const mode = ref<Mode>('idle')
const setup = ref<TotpSetup | null>(null)
const codes = ref<string[]>([])
const code = ref('')
const password = ref('')
const error = ref('')
const loading = ref(false)
const done = ref('')
function reset(next: Mode = 'idle') {
mode.value = next
code.value = ''
password.value = ''
error.value = ''
}
function message(e: unknown) {
if (e instanceof ApiError && e.status === 400) return e.message
if (e instanceof ApiError && e.status === 429) return t('login.throttled')
return t('account.error')
}
async function run(action: () => Promise<void>) {
error.value = ''
loading.value = true
try {
await action()
} catch (e) {
error.value = message(e)
} finally {
loading.value = false
}
}
const start = () =>
run(async () => {
done.value = ''
setup.value = await api.setupTotp()
reset('setup')
})
const enable = () =>
run(async () => {
codes.value = (await api.enableTotp(code.value)).recovery_codes
setup.value = null
reset('codes')
})
const disable = () =>
run(async () => {
await api.disableTotp(password.value, code.value)
await load(true)
reset()
done.value = t('account.mfa.disabled')
})
const regenerate = () =>
run(async () => {
codes.value = (await api.regenerateRecoveryCodes(code.value)).recovery_codes
reset('codes')
})
async function finish() {
codes.value = []
await load(true)
reset()
done.value = t('account.mfa.enabledDone')
}
</script>
<template>
<section class="prism-card panel" aria-labelledby="mfa-heading">
<!-- Étape 1 et 2 de l'activation (maquette 14) -->
<template v-if="mode === 'setup' && setup">
<h2 id="mfa-heading" class="card-title">{{ $t('account.mfa.setupTitle') }}</h2>
<AlertBanner v-if="error" variant="error" class="mb-3">{{ error }}</AlertBanner>
<div class="row g-4">
<div class="col-12">
<h3 class="step">{{ $t('account.mfa.step1') }}</h3>
<p class="small-text">{{ $t('account.mfa.step1Text') }}</p>
<img :src="setup.qr_code" :alt="$t('account.mfa.qrAlt')" class="qr" />
<p class="secret">
{{ $t('account.mfa.secret') }} <span class="font-monospace">{{ setup.secret }}</span>
</p>
</div>
<div class="col-12">
<h3 class="step">{{ $t('account.mfa.step2') }}</h3>
<p class="small-text">{{ $t('account.mfa.step2Text') }}</p>
<form @submit.prevent="enable">
<CodeInput v-model="code" class="mb-3" :label="$t('mfa.codeLabel')" />
<button
type="submit"
class="btn btn-primary action"
:disabled="loading || code.length !== 6"
>
{{ $t('account.mfa.activate') }}
</button>
</form>
<button type="button" class="btn btn-link link-strong px-0 mt-2" @click="reset()">
{{ $t('account.cancel') }}
</button>
</div>
</div>
</template>
<!-- Codes de secours (maquette 15) -->
<RecoveryCodes v-else-if="mode === 'codes'" :codes="codes" @done="finish" />
<template v-else>
<div class="d-flex align-items-start justify-content-between gap-3 mb-3">
<h2 id="mfa-heading" class="card-title mb-0">{{ $t('account.mfa.title') }}</h2>
<span class="status" :class="{ on: user?.mfa_enabled }">
{{ user?.mfa_enabled ? $t('account.mfa.on') : $t('account.mfa.off') }}
</span>
</div>
<AlertBanner v-if="done" variant="info" class="mb-3">{{ done }}</AlertBanner>
<AlertBanner v-if="user?.is_staff && !user.mfa_enabled" variant="error" class="mb-3">
{{ $t('account.mfa.mandatory') }}
</AlertBanner>
<template v-if="!user?.mfa_enabled">
<p>{{ $t('account.mfa.explain') }}</p>
<AlertBanner v-if="error" variant="error" class="mb-3">{{ error }}</AlertBanner>
<button type="button" class="btn btn-outline-primary" :disabled="loading" @click="start">
{{ $t('account.mfa.enable') }}
</button>
</template>
<template v-else>
<p>{{ $t('account.mfa.enabledText') }}</p>
<form
v-if="mode === 'regenerate' || mode === 'disable'"
class="mt-3"
@submit.prevent="mode === 'disable' ? disable() : regenerate()"
>
<AlertBanner v-if="error" variant="error" class="mb-3">{{ error }}</AlertBanner>
<div v-if="mode === 'disable'" class="mb-3">
<label for="mfa-password" class="form-label">{{ $t('account.currentPassword') }}</label>
<input
id="mfa-password"
v-model="password"
type="password"
class="form-control"
autocomplete="current-password"
/>
</div>
<label for="mfa-code" class="form-label">
{{ mode === 'disable' ? $t('account.mfa.codeOrRecovery') : $t('mfa.codeLabel') }}
</label>
<input
id="mfa-code"
v-model.trim="code"
class="form-control mb-3"
autocomplete="one-time-code"
:inputmode="mode === 'disable' ? 'text' : 'numeric'"
/>
<div class="d-flex flex-wrap gap-3 align-items-center">
<button
type="submit"
class="btn"
:class="mode === 'disable' ? 'btn-danger' : 'btn-primary'"
:disabled="loading || !code || (mode === 'disable' && !password)"
>
{{ mode === 'disable' ? $t('account.mfa.disable') : $t('account.mfa.regenerate') }}
</button>
<button type="button" class="btn btn-link link-strong px-0" @click="reset()">
{{ $t('account.cancel') }}
</button>
</div>
</form>
<div v-else class="d-flex flex-wrap gap-3">
<button type="button" class="btn btn-outline-primary" @click="reset('regenerate')">
{{ $t('account.mfa.regenerate') }}
</button>
<button
v-if="!user.is_staff"
type="button"
class="btn btn-link link-strong px-0"
@click="reset('disable')"
>
{{ $t('account.mfa.disable') }}
</button>
</div>
</template>
</template>
</section>
</template>
<style scoped>
.panel {
padding: 24px 28px;
}
.card-title {
font-size: 1.25rem;
margin-bottom: 1.25rem;
}
.step {
font-size: 1rem;
margin-bottom: 0.75rem;
}
.small-text {
font-size: 0.8125rem;
}
.qr {
width: 200px;
height: 200px;
padding: 10px;
border: 1px solid var(--prism-border);
border-radius: 8px;
background: #fff;
}
.secret {
margin-top: 1rem;
font-size: 0.8125rem;
font-weight: 500;
color: var(--prism-navy);
}
.action {
min-width: 160px;
}
.status {
display: inline-flex;
align-items: center;
height: 28px;
padding: 0 14px;
border-radius: 14px;
border: 1px solid var(--prism-border);
background: var(--prism-bg);
font-size: 0.8125rem;
font-weight: 500;
white-space: nowrap;
}
.status.on {
background: var(--prism-level-1);
border-color: var(--prism-level-1);
color: #fff;
}
</style>