You've already forked AstralRinth
forked from didirus/AstralRinth
More secure custom authentication solution (#126)
* Remove Nuxt Auth from the project, and switch to a custom solution * Replace old testing code * Remove warnings * Add comments to hard to understand function calls in middleware * Use arrow functions
This commit is contained in:
63
middleware/auth.js
Normal file
63
middleware/auth.js
Normal file
@@ -0,0 +1,63 @@
|
||||
export default async function (context) {
|
||||
if (!context.from) {
|
||||
if (context.route.query.code) {
|
||||
context.app.$cookies.set('auth-token', context.route.query.code, {
|
||||
secure: true,
|
||||
sameSite: 'Strict',
|
||||
httpOnly: true,
|
||||
})
|
||||
|
||||
return context.redirect(context.route.path)
|
||||
}
|
||||
|
||||
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]
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user