page/login: update cookie from useCookie on login/logout

I think this fixes an issue where auth token cookie
gets deleted when page is not being refreshed
after login.
This commit is contained in:
Wroclaw 2023-12-20 06:03:12 +01:00
parent eabb7b89c7
commit f11b4c9a75

View file

@ -1,8 +1,9 @@
<script setup lang="ts">
import { ref, watch } from "vue";
import { VForm } from "vuetify/components";
import { type CookieRef } from "#app";
import { cookieSettings } from "~/utils/cookieSettings";
import { definePageMeta, navigateTo, useCookie, useFetch, useRoute, useRequestFetch } from "#imports";
import { definePageMeta, navigateTo, useCookie, useFetch, useRoute, useRequestFetch, useRequestEvent } from "#imports";
const route = useRoute();
const fetch = useRequestFetch();
@ -12,7 +13,10 @@ const password = ref("");
const loading = ref(false);
const error = ref<true | string>(true);
const form = ref<VForm | null>(null);
const loggedIn = ref<boolean>(useCookie("token", cookieSettings).value != null);
const tokenCookie = useCookie("token", cookieSettings) as CookieRef<string | undefined>;
const loggedIn = ref<boolean>(tokenCookie.value !== undefined);
watch(tokenCookie, (v) => { loggedIn.value = v !== undefined; });
const redirectTo = ref(route.redirectedFrom);
@ -29,7 +33,7 @@ async function submit() {
method: "POST",
});
console.log(result);
loggedIn.value = true;
tokenCookie.value = result.token;
password.value = "";
} catch (e) {
console.log(typeof e);
@ -47,6 +51,7 @@ async function logout() {
await fetch("/api/logout");
loggedIn.value = false;
} catch (e) {
tokenCookie.value = undefined;
console.error(e);
}
}