reflect User composable changes to default layout and edit page

This commit is contained in:
axolotle 2023-09-04 16:43:51 +02:00
parent 623d3b4483
commit 11d303a8e6
2 changed files with 17 additions and 11 deletions

View file

@ -1,7 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import type { User } from '@/composables/states'
const { t } = useI18n() const { t } = useI18n()
const isLoggedIn = useIsLoggedIn() const isLoggedIn = useIsLoggedIn()
const { userData: me } = await useUserInfo() const settings = await useSettings()
const user = await useUser<User | null>()
const skipLink: Ref<HTMLLinkElement | null> = ref(null) const skipLink: Ref<HTMLLinkElement | null> = ref(null)
const colorMode = useColorMode() const colorMode = useColorMode()
const themes = [ const themes = [
@ -56,9 +59,10 @@ async function logout() {
const { error } = await useApi('/logout') const { error } = await useApi('/logout')
if (!error.value) { if (!error.value) {
// FIXME : meh, turns out the cookie is still valid after successfully calling the route for some reason ... !? // Delete user infos
user.value = null
isLoggedIn.value = false isLoggedIn.value = false
await navigateTo('/login') await navigateTo(settings.value.public ? '/' : '/login')
} else { } else {
// FIXME : display an error or something // FIXME : display an error or something
} }
@ -87,10 +91,10 @@ async function logout() {
<div> <div>
<h2 class="text-2xl font-extrabold leading-none tracking-tight"> <h2 class="text-2xl font-extrabold leading-none tracking-tight">
{{ me.username }} {{ user?.username || t('visitor') }}
</h2> </h2>
<h3>{{ me.fullname }}</h3> <h3 v-if="user">{{ user.fullname }}</h3>
<h4 class="opacity-50">{{ me.mail }}</h4> <h4 v-if="user" class="opacity-50">{{ user.mail }}</h4>
</div> </div>
<!-- FIXME temp --> <!-- FIXME temp -->
@ -106,10 +110,12 @@ async function logout() {
</div> </div>
<YButton <YButton
v-if="isLoggedIn"
icon="mdi:logout" icon="mdi:logout"
:text="t('logout')" :text="t('logout')"
@click.prevent="logout" @click.prevent="logout"
/> />
<YButton v-else icon="mdi:login" :text="t('login')" to="/login" />
</div> </div>
</slot> </slot>
</header> </header>

View file

@ -3,10 +3,10 @@ import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/yup' import { toTypedSchema } from '@vee-validate/yup'
import * as yup from 'yup' import * as yup from 'yup'
import { pick, exclude } from '@/utils/common' import { pick, exclude } from '@/utils/common'
import type { UserData } from '@/composables/api' import type { User } from '@/composables/states'
const { t } = useI18n() const { t } = useI18n()
const { userData, update } = await useUserInfo() const user = await useUser()
const loading: Ref<boolean | null> = ref(false) const loading: Ref<boolean | null> = ref(false)
const feedback: Ref<{ const feedback: Ref<{
@ -44,7 +44,7 @@ const { handleSubmit, setFieldError, resetForm, meta } = useForm({
currentpassword: '', currentpassword: '',
newpassword: '', newpassword: '',
confirmpassword: '', confirmpassword: '',
...pick(userData.value, 'fullname', 'mailalias', 'mailforward'), ...pick(user.value, 'fullname', 'mailalias', 'mailforward'),
}, },
}) })
@ -63,7 +63,7 @@ const onSubmit = handleSubmit(async (form) => {
loading.value = true loading.value = true
const { error, data } = await useApi< const { error, data } = await useApi<
Pick<UserData, 'fullname' | 'mailalias' | 'mailforward'> Pick<User, 'fullname' | 'mailalias' | 'mailforward'>
>('/update', { >('/update', {
method: 'PUT', method: 'PUT',
body: exclude(form, 'confirmpassword'), body: exclude(form, 'confirmpassword'),
@ -89,7 +89,7 @@ const onSubmit = handleSubmit(async (form) => {
return navigateTo('/login') return navigateTo('/login')
} }
update(data.value) Object.assign(user.value, data)
resetForm({ resetForm({
values: { values: {
...data.value, ...data.value,