Redblueflame af8fc53704
Layout refactor (#150)
* Revert recent commits

* Move things around, and do preparation work for nested page systems.

* Fixed issue on side bar not appearing
2021-03-31 19:09:18 +02:00

69 lines
1.8 KiB
JavaScript

export default async function (context) {
if (!context.from) {
if (context.app.$cookies.get('auth-token-reset')) {
context.app.$cookies.removeAll()
return
}
if (context.route.query.code) {
context.app.$cookies.set('auth-token', context.route.query.code, {
secure: true,
sameSite: 'Strict',
httpOnly: true,
})
await context.store.dispatch('auth/fetchUser', {
token: context.route.query.code,
})
} else if (context.app.$cookies.get('auth-token')) {
const cookie = context.app.$cookies.get('auth-token')
await context.store.dispatch('auth/fetchUser', { token: cookie })
}
}
// Disable middleware if options: { auth: false } is set on the route
if (routeOption(context.route, 'auth', false)) return
// Disable middleware if no route was matched to allow 404/error page
if (!getMatchedComponents(context.route, []).length) {
return
}
if (!context.$auth.user) {
return context.redirect(
`https://api.modrinth.com/api/v1/auth/init?url=https://modrinth.com${context.route.fullPath}`
)
}
}
function routeOption(route, key, value) {
return route.matched.some((m) => {
if (process.client) {
// Client
return Object.values(m.components).some(
(component) => component.options && component.options[key] === value
)
} else {
// SSR
return Object.values(m.components).some((component) =>
Object.values(component._Ctor).some(
(ctor) => ctor.options && ctor.options[key] === value
)
)
}
})
}
function getMatchedComponents(route, matches) {
return [].concat(
...[],
...route.matched.map((m, index) => {
return Object.keys(m.components).map((key) => {
matches.push(index)
return m.components[key]
})
})
)
}