Fix session store sync

This commit is contained in:
ekzyis 2023-11-07 09:48:01 +01:00
parent a641733444
commit 57673f7b39
3 changed files with 17 additions and 6 deletions

View File

@ -0,0 +1,8 @@
<template>
<div v-if="session.pubkey">authenticated as {{ session.pubkey.slice(0,8) }}</div>
</template>
<script setup>
import { useSession } from '@/stores/session'
const session = useSession()
</script>

View File

@ -7,6 +7,7 @@ import './index.css'
import HomeView from '@/components/HomeView' import HomeView from '@/components/HomeView'
import LoginView from '@/components/LoginView' import LoginView from '@/components/LoginView'
import UserView from '@/components/UserView'
const routes = [ const routes = [
{ {
@ -14,6 +15,9 @@ const routes = [
}, },
{ {
path: '/login', component: LoginView path: '/login', component: LoginView
},
{
path: '/user', component: UserView
} }
] ]
const router = VueRouter.createRouter({ const router = VueRouter.createRouter({

View File

@ -2,9 +2,8 @@ import { defineStore } from 'pinia'
import { computed, ref } from 'vue' import { computed, ref } from 'vue'
export const useSession = defineStore('session', () => { export const useSession = defineStore('session', () => {
let pubkey = ref(null) const pubkey = ref(null)
// eslint-disable-next-line vue/no-ref-as-operand const isAuthenticated = computed(() => !!pubkey.value)
const isAuthenticated = computed(() => !!pubkey)
async function init () { async function init () {
try { try {
@ -24,9 +23,9 @@ export const useSession = defineStore('session', () => {
return fetch(url, { return fetch(url, {
credentials: 'include' credentials: 'include'
}) })
.then(r => { .then(async r => {
const body = r.json() const body = await r.json()
pubkey = body.pubkey pubkey.value = body.pubkey
return body return body
}) })
} }