mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-10 11:33:26 +08:00
Persist learn progress per user
This commit is contained in:
21
app/middleware/require-auth.ts
Normal file
21
app/middleware/require-auth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { defineNuxtRouteMiddleware, navigateTo } from '#app'
|
||||
import { useAuthStore } from '~/stores/auth'
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to) => {
|
||||
const auth = useAuthStore()
|
||||
|
||||
if (!auth.accessToken) {
|
||||
await auth.tryRefresh().catch(() => false)
|
||||
}
|
||||
|
||||
if (!auth.initialized) {
|
||||
await auth.fetchUser().catch(() => null)
|
||||
} else if (!auth.user) {
|
||||
await auth.fetchUser().catch(() => null)
|
||||
}
|
||||
|
||||
if (!auth.user) {
|
||||
const redirect = encodeURIComponent(to.fullPath || '/')
|
||||
return navigateTo(`/login?redirect=${redirect}`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user