mirror your GitHub repos to tangled.org automatically
1export default defineNuxtRouteMiddleware(async () => {
2 // On SSR we must forward the request cookies so the whoami probe sees the
3 // session. On the client, same-origin `$fetch` already sends cookies.
4 const headers: Record<string, string> = {}
5 if (import.meta.server) {
6 const cookie = useRequestHeader('cookie')
7 if (cookie) headers.cookie = cookie
8 }
9
10 try {
11 await $fetch('/api/me/whoami', { headers })
12 }
13 catch (err: unknown) {
14 const status = err && typeof err === 'object'
15 ? (('statusCode' in err && typeof err.statusCode === 'number' ? err.statusCode : undefined)
16 ?? ('status' in err && typeof err.status === 'number' ? err.status : undefined))
17 : undefined
18 if (status === 401) {
19 return navigateTo('/', { redirectCode: 302 })
20 }
21 throw err
22 }
23})