Merge init and checkSession

This commit is contained in:
ekzyis 2023-11-07 18:55:20 +01:00
parent c44346774a
commit 559936e565
2 changed files with 9 additions and 15 deletions

View File

@ -8,7 +8,7 @@
<script setup>
import { useSession } from './stores/session'
const session = useSession()
session.init()
session.checkSession()
</script>
<!-- eslint-disable -->

View File

@ -6,18 +6,6 @@ export const useSession = defineStore('session', () => {
const isAuthenticated = computed(() => !!pubkey.value)
const initialized = ref(false)
async function init () {
try {
const { pubkey } = await checkSession()
if (pubkey) {
console.log('authenticated as', pubkey)
} else console.log('unauthenticated')
} catch (err) {
console.error('error:', err.reason || err)
}
initialized.value = true
}
function checkSession () {
const url = window.origin + '/api/session'
return fetch(url, {
@ -25,8 +13,14 @@ export const useSession = defineStore('session', () => {
})
.then(async r => {
const body = await r.json()
pubkey.value = body.pubkey
if (body.pubkey) {
pubkey.value = body.pubkey
console.log('authenticated as', body.pubkey)
} else console.log('unauthenticated')
initialized.value = true
return body
}).catch(err => {
console.error('error:', err.reason || err)
})
}
@ -46,5 +40,5 @@ export const useSession = defineStore('session', () => {
})
}
return { pubkey, isAuthenticated, initialized, init, checkSession, login, logout }
return { pubkey, isAuthenticated, initialized, checkSession, login, logout }
})