* with the browser's cookie) and shared by every component. */
export async function useViewer() {
const viewer = useState<string | null>('viewer', () => null)
await useAsyncData('viewer', async () => {
const data = await graphqlRequest<{ viewer: { login: string } | null }>( `
query {
viewer {
login
}
}
`)
viewer.value = data.viewer?.login ?? null
return viewer.value
})
return viewer
}
* so everything renders as the new user. */
export async function signIn(login: string, password: string, then: string) {
await $fetch('/-/session', { method: 'POST', body: { login, password } })
window.location.assign(then)
}
export async function signOut() {
await $fetch('/-/session', { method: 'DELETE' })
window.location.reload()
}