bilhej/frontend/src/pages/ChangePasswordPage.vue
Joakim Mörling 86fb946e33
All checks were successful
CI / Lint, type check, unit tests, coverage (push) Successful in 2m2s
CI / E2E browser tests (push) Successful in 1m55s
Add password reset, logged-in change password, and Mailpit email dev/E2E.
Operators can fix prod admin passwords without email via Byt lösenord;
end users can use forgot-password when SMTP is configured. Local and CI
use Mailpit to capture outbound mail and verify reset links end-to-end.

- Backend: V8 password_reset_tokens, PasswordResetService, EmailService,
  POST /api/auth/forgot-password, reset-password, change-password
- Optional testToken in forgot-password response (docker profile only, for E2E)
- Frontend: ForgotPasswordPage, ResetPasswordPage, ChangePasswordPage,
  routes, login link, header Byt lösenord
- Mailpit (ghcr.io/axllent/mailpit:v1.28) in docker-compose + e2e stack
- E2E: password-reset.spec.ts + Mailpit API helper tests SMTP delivery
- Separate dev/e2e Docker image names to avoid overwriting bilhej-frontend
- Docs: README email section, production-email-checklist, .env.example
- Unit/integration tests for reset, change password, and Vitest page specs

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-21 18:05:15 +02:00

190 lines
4.7 KiB
Vue

<script setup lang="ts">
import { ref, computed } from 'vue'
import { RouterLink } from 'vue-router'
import { changePassword } from '@/api/auth'
import { ApiError } from '@/api/client'
const currentPassword = ref('')
const password = ref('')
const confirmPassword = ref('')
const submitting = ref(false)
const errorMessage = ref('')
const successMessage = ref('')
const passwordError = computed(() => {
if (password.value.length === 0) return ''
return password.value.length >= 8
? ''
: 'Lösenordet måste vara minst 8 tecken'
})
const confirmPasswordError = computed(() => {
if (confirmPassword.value.length === 0) return ''
return confirmPassword.value === password.value
? ''
: 'Lösenorden matchar inte'
})
const isValid = computed(
() =>
currentPassword.value.length > 0 &&
passwordError.value === '' &&
confirmPasswordError.value === '' &&
password.value.length > 0 &&
confirmPassword.value.length > 0,
)
async function handleSubmit() {
if (!isValid.value || submitting.value) return
submitting.value = true
errorMessage.value = ''
successMessage.value = ''
try {
const response = await changePassword(currentPassword.value, password.value)
successMessage.value = response.message
currentPassword.value = ''
password.value = ''
confirmPassword.value = ''
} catch (err) {
if (err instanceof ApiError && err.status === 401) {
errorMessage.value = 'Nuvarande lösenord är felaktigt'
} else if (err instanceof ApiError) {
errorMessage.value = err.message
} else {
errorMessage.value = 'Något gick fel. Försök igen senare.'
}
} finally {
submitting.value = false
}
}
</script>
<template>
<div class="page">
<div class="page__card">
<h1 class="page__title">Byt lösenord</h1>
<p class="page__subtitle">
Ange ditt nuvarande lösenord och välj ett nytt.
</p>
<form
v-if="!successMessage"
class="page__form"
@submit.prevent="handleSubmit"
>
<div class="field">
<label for="current-password" class="field__label"
>Nuvarande lösenord</label
>
<input
id="current-password"
v-model="currentPassword"
type="password"
name="currentPassword"
autocomplete="current-password"
class="field__input"
/>
</div>
<div class="field">
<label for="password" class="field__label">Nytt lösenord</label>
<input
id="password"
v-model="password"
type="password"
name="password"
autocomplete="new-password"
class="field__input"
placeholder="Minst 8 tecken"
/>
<p v-if="passwordError" class="field__hint field__hint--error">
{{ passwordError }}
</p>
</div>
<div class="field">
<label for="confirm-password" class="field__label"
>Bekräfta nytt lösenord</label
>
<input
id="confirm-password"
v-model="confirmPassword"
type="password"
name="confirmPassword"
autocomplete="new-password"
class="field__input"
/>
<p v-if="confirmPasswordError" class="field__hint field__hint--error">
{{ confirmPasswordError }}
</p>
</div>
<div v-if="errorMessage" class="message message--error">
{{ errorMessage }}
</div>
<button
type="submit"
class="btn btn--primary btn--lg page__submit"
:disabled="!isValid || submitting"
>
{{ submitting ? 'Sparar...' : 'Spara nytt lösenord' }}
</button>
</form>
<div v-else class="message message--success">
{{ successMessage }}
</div>
<p class="page__footer-link">
<RouterLink to="/">Tillbaka till startsidan</RouterLink>
</p>
</div>
</div>
</template>
<style scoped>
.page {
max-width: 28rem;
margin: var(--space-3xl) auto 0;
padding: 0 var(--space-lg);
}
.page__card {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: var(--radius-xl);
padding: var(--space-xl);
box-shadow: var(--shadow-card);
}
.page__title {
margin: 0 0 var(--space-sm) 0;
font-size: 1.5rem;
color: var(--color-ink);
}
.page__subtitle {
margin: 0 0 var(--space-xl) 0;
font-size: 0.875rem;
color: var(--color-muted);
}
.page__form {
display: flex;
flex-direction: column;
gap: var(--space-md);
}
.page__footer-link {
margin-top: var(--space-lg);
text-align: center;
font-size: 0.875rem;
}
.page__submit {
width: 100%;
}
</style>