mirror of
https://github.com/ArthurDanjou/Hermes.git
synced 2026-01-24 09:00:27 +01:00
Initial commit 🚀
This commit is contained in:
11
src/modules/README.md
Normal file
11
src/modules/README.md
Normal 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
24
src/modules/auth.ts
Normal 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
21
src/modules/i18n.ts
Normal 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
9
src/modules/nprogress.ts
Normal 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
12
src/modules/pwa.ts
Normal 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
7
src/modules/store.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { UserModule } from '~/types'
|
||||
import { store } from '~/store'
|
||||
|
||||
export const install: UserModule = ({ isClient, app }) => {
|
||||
if (isClient)
|
||||
app.use(store)
|
||||
}
|
||||
Reference in New Issue
Block a user