-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.vue
94 lines (81 loc) · 2.63 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<script setup lang="ts">
import { useOnline } from '@vueuse/core'
let errorToast = useToast()
onErrorCaptured((err) => {
errorToast.add({
title: err.name,
description: err.message,
icon: 'i-heroicons-exclamation-triangle',
})
})
import PouchDB from 'pouchdb'
import auth from './utils/authorization/Authorizer'
import LoginState from '~/utils/authorization/LoginState'
import { loginStateKey } from '~/utils/keys'
import { useSync } from '~/composables/useSync'
PouchDB.plugin(auth)
let pdb = new PouchDB(couchDBBaseURL + '/basic')
let session = await pdb.getSession()
let loginState = useState<LoginState>('login-state', () => {
return session.userCtx.name == null ? LoginState.loggedOut : LoginState.loggedIn
})
let route = useRoute()
if (loginState.value === LoginState.loggedOut && route.matched[0].name != 'login') await navigateTo('/login')
else if (route.matched[0].name == 'index') navigateTo('/dashboard')
let sessionState = ref(session)
let usernameState = useState('username', () => session.userCtx.name)
let online = useOnline()
async function updateUsernameState(): Promise<boolean> {
session = await pdb.getSession()
if (session.ok) {
loginState.value = session.userCtx.name == null ? LoginState.loggedOut : LoginState.loggedIn
usernameState.value = session.userCtx.name
sessionState.value = session
} else if (!online.value) {
return true
} else return false
return true
}
async function logout() {
let logout
let usernameState
try {
logout = await pdb.logOut()
usernameState = await updateUsernameState()
await navigateTo('/login')
} catch (e) {
throw createError({
name: 'Logout Failed',
message: 'Logout or logout validation failed to connect with the server.',
})
}
if (logout?.ok !== true) {
throw createError({
name: 'Logout Failed',
message: 'Logout rejected by server.',
})
}
if (usernameState !== true) {
throw createError({
name: 'Logout Failed',
message: 'Failed to verify logout.',
})
}
}
const loginStateObject = { loginState, usernameState, sessionState, updateUsernameState, logout }
provide(loginStateKey, loginStateObject)
useSync()
</script>
<template>
<NuxtPwaManifest />
<div class="min-h-screen min-w-screen">
<LazyNuxtLoadingIndicator />
<v-app>
<!-- <UContainer class="p-0 m-0">-->
<NuxtPage />
<!-- </UContainer>-->
</v-app>
<LazyUNotifications />
</div>
</template>
<style scoped></style>