useCwaAccount, useCwaChangePassword and useCwaChangeEmail
The admin's My account page (/_cwa/account) is built from these three composables, and you can build an account page of your own from them. Each one works on the signed-in user, so a user can only change their own account with them.
const { account, loading, loadFailed, reload } = useCwaAccount()
const { oldPassword, password, repeatPassword, submit, submitting, fieldErrors, saved } = useCwaChangePassword()
const { newEmail, oldPassword, submit, cancel, pending, submitting, fieldErrors } = useCwaChangeEmail()
None of them takes parameters.
useCwaAccount
It loads the signed-in user from /me when the component mounts.
| Return | Type | Purpose |
|---|---|---|
account | Ref<CwaUser | undefined> | The user as /me returns it, such as username, emailAddress, emailAddressVerified, newEmailAddress and roles |
loading | Ref<boolean> | true while /me is loading |
loadFailed | Ref<boolean> | true when /me failed. The user stays signed in |
reload | () => Promise<void> | Load /me again, for a Try again button |
useCwaChangePassword
Bind the three fields, then call submit(). It sends oldPassword and plainPassword in a PATCH to the signed-in user (see Authentication › Setting a Password).
| Return | Type | Purpose |
|---|---|---|
oldPassword | Ref<string> | The current password |
password | Ref<string> | The new password |
repeatPassword | Ref<string> | The new password again |
submit | () => Promise<boolean> | Send the change. true when it saved; the three fields are then emptied |
submitting | Ref<boolean> | true while the request runs |
fieldErrors | Ref<Record<string, string>> | Messages by field: oldPassword, password and repeatPassword |
saved | Ref<boolean> | true after a change saved, until the next submit() |
When password and repeatPassword differ, submit() sends nothing and sets fieldErrors.repeatPassword to "The passwords don't match." The API's 422 violations land on their fields: a wrong current password on oldPassword, and a rule on the new password (see Authentication › Setting a Password) on password. Any other failure shows an error toast, "Your password wasn't changed.", and changes nothing.
useCwaChangeEmail
Bind newEmail and oldPassword, then call submit(). It sends newEmailAddress and oldPassword in a PATCH to the signed-in user. The email doesn't change straight away: the API emails a link to the new address, and the address changes once it's opened (see User Emails › Email Address Change Flow).
| Return | Type | Purpose |
|---|---|---|
newEmail | Ref<string> | The new address |
oldPassword | Ref<string> | The current password, which an email change needs |
submit | () => Promise<boolean> | Send the change. true when it saved; both fields are then emptied |
cancel | () => Promise<boolean> | Cancel a pending change by sending newEmailAddress: null, so its link stops working |
pending | ComputedRef<string | undefined> | The address waiting to be confirmed, if any |
submitting | Ref<boolean> | true while a request runs |
fieldErrors | Ref<Record<string, string>> | Messages by field: newEmailAddress and oldPassword |
A failure other than a 422 shows an error toast, "Your email wasn't changed." (or "The email change wasn't cancelled."). To resend the confirmation for a pending change, call useResendVerifyEmail's resendVerifyEmail(username, 'new').
An Account Page of Your Own
<template>
<p v-if="loading">Loading…</p>
<p v-else-if="loadFailed">Your account couldn't be loaded. <button @click="reload">Try again</button></p>
<form v-else @submit.prevent="submit">
<p>Signed in as {{ account?.emailAddress }}</p>
<input v-model="oldPassword" type="password" autocomplete="current-password">
<span v-if="fieldErrors.oldPassword">{{ fieldErrors.oldPassword }}</span>
<input v-model="password" type="password" autocomplete="new-password">
<span v-if="fieldErrors.password">{{ fieldErrors.password }}</span>
<input v-model="repeatPassword" type="password" autocomplete="new-password">
<span v-if="fieldErrors.repeatPassword">{{ fieldErrors.repeatPassword }}</span>
<button :disabled="submitting">Change password</button>
<p v-if="saved">Your password has been changed.</p>
</form>
</template>
<script setup lang="ts">
const { account, loading, loadFailed, reload } = useCwaAccount()
const { oldPassword, password, repeatPassword, submit, submitting, fieldErrors, saved } = useCwaChangePassword()
</script>
An account page of your own also needs cwa: { disabled: true } (see Module Setup › Disable CWA Route Fetching) and the cwa-auth middleware, so only a signed-in user reaches it (see Authentication › Protecting Pages).