-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathplugin.ts
91 lines (77 loc) · 2.95 KB
/
plugin.ts
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
import { getHeader } from 'h3'
import authMiddleware from './middleware/sidebase-auth'
import { getNitroRouteRules } from './utils/kit'
import { FetchConfigurationError } from './utils/fetch'
import { resolveApiBaseURL } from './utils/url'
import { _refreshHandler, addRouteMiddleware, defineNuxtPlugin, useAuth, useAuthState, useRuntimeConfig } from '#imports'
export default defineNuxtPlugin(async (nuxtApp) => {
// 1. Initialize authentication state, potentially fetch current session
const { data, lastRefreshedAt, loading } = useAuthState()
const { getSession } = useAuth()
// use runtimeConfig
const wholeRuntimeConfig = useRuntimeConfig()
const runtimeConfig = wholeRuntimeConfig.public.auth
const globalAppMiddleware = runtimeConfig.globalAppMiddleware
const routeRules = import.meta.server ? getNitroRouteRules(nuxtApp._route.path) : {}
// Set the correct `baseURL` on the server,
// because the client would not have access to environment variables
if (import.meta.server) {
runtimeConfig.baseURL = resolveApiBaseURL(wholeRuntimeConfig)
}
// Skip auth if we're prerendering
let nitroPrerender = false
if (nuxtApp.ssrContext) {
nitroPrerender
= getHeader(nuxtApp.ssrContext.event, 'x-nitro-prerender') !== undefined
}
// Prioritize `routeRules` setting over `runtimeConfig` settings, fallback to false
let disableServerSideAuth = routeRules.disableServerSideAuth
disableServerSideAuth ??= runtimeConfig?.disableServerSideAuth
disableServerSideAuth ??= false
if (disableServerSideAuth) {
loading.value = true
}
// Only fetch session if it was not yet initialized server-side
const isErrorUrl = nuxtApp.ssrContext?.error === true
const requireAuthOnErrorPage = globalAppMiddleware === true || (typeof globalAppMiddleware === 'object' && globalAppMiddleware.allow404WithoutAuth)
const shouldFetchSession = typeof data.value === 'undefined'
&& !nitroPrerender
&& !disableServerSideAuth
&& !(isErrorUrl && requireAuthOnErrorPage)
if (shouldFetchSession) {
try {
await getSession()
}
catch (e) {
// Do not throw the configuration error as it can lead to infinite recursion
if (!(e instanceof FetchConfigurationError)) {
throw e
}
}
}
// 2. Setup session maintanence, e.g., auto refreshing or refreshing on foux
nuxtApp.hook('app:mounted', () => {
_refreshHandler.init()
if (disableServerSideAuth) {
getSession()
}
})
const _unmount = nuxtApp.vueApp.unmount
nuxtApp.vueApp.unmount = function () {
_refreshHandler.destroy()
// Clear session
lastRefreshedAt.value = undefined
data.value = undefined
// Call original unmount
_unmount()
}
// 3. Enable the middleware, either globally or as a named `auth` option
if (
globalAppMiddleware === true
|| (typeof globalAppMiddleware === 'object' && globalAppMiddleware.isEnabled)
) {
addRouteMiddleware('auth', authMiddleware, {
global: true
})
}
})