Initial commit 🚀

This commit is contained in:
2021-09-23 21:28:45 +02:00
commit 2fb96b2ca4
83 changed files with 9890 additions and 0 deletions

11
src/modules/README.md Normal file
View File

@@ -0,0 +1,11 @@
## Modules
A custom user module system. Place a `.ts` file with the following template, it will be installed automatically.
```ts
import { UserModule } from '~/types'
export const install: UserModule = ({ app, router, isClient }) => {
// do something
}
```

24
src/modules/auth.ts Normal file
View File

@@ -0,0 +1,24 @@
import { store } from '~/store'
import { useAuth } from '~/logic/auth'
import { UserModule } from '~/types'
export const install: UserModule = ({ isClient, router }) => {
if (isClient) {
router.beforeEach(async(from, to, next) => {
const { isAuthenticated, user } = useAuth()
if (user === null)
await store.dispatch('FETCH_USER')
// redirect to login page if not connected
if (from.meta.auth && !isAuthenticated)
next({ name: 'login' })
// redirect to home page if connected
else if (from.meta.guest && isAuthenticated)
next({ name: 'index' })
else next()
})
}
}

21
src/modules/i18n.ts Normal file
View File

@@ -0,0 +1,21 @@
import { createI18n } from 'vue-i18n'
import { UserModule } from '~/types'
const messages = Object.fromEntries(
Object.entries(
import.meta.globEager('../../locales/*.json'))
.map(([key, value]) => {
return [key.slice(14, -5), value.default]
}),
)
export const install: UserModule = ({ app }) => {
const i18n = createI18n({
legacy: false,
locale: 'en',
messages,
fallbackLocale: 'en',
})
app.use(i18n)
}

9
src/modules/nprogress.ts Normal file
View File

@@ -0,0 +1,9 @@
import NProgress from 'nprogress'
import { UserModule } from '~/types'
export const install: UserModule = ({ isClient, router }) => {
if (isClient) {
router.beforeEach(() => { NProgress.start() })
router.afterEach(() => { NProgress.done() })
}
}

12
src/modules/pwa.ts Normal file
View File

@@ -0,0 +1,12 @@
import { UserModule } from '~/types'
// https://github.com/antfu/vite-plugin-pwa#automatic-reload-when-new-content-available
export const install: UserModule = ({ isClient, router }) => {
if (!isClient)
return
router.isReady().then(async() => {
const { registerSW } = await import('virtual:pwa-register')
registerSW({ immediate: true })
})
}

7
src/modules/store.ts Normal file
View File

@@ -0,0 +1,7 @@
import { UserModule } from '~/types'
import { store } from '~/store'
export const install: UserModule = ({ isClient, app }) => {
if (isClient)
app.use(store)
}