mirror of https://github.com/wg-easy/wg-easy
7 changed files with 911 additions and 1145 deletions
@ -0,0 +1,28 @@ |
|||
export default defineNuxtRouteMiddleware(async (to) => { |
|||
// api & setup handled server side
|
|||
if (to.path.startsWith('/api/') || to.path.startsWith('/setup')) { |
|||
return; |
|||
} |
|||
|
|||
const authStore = useAuthStore(); |
|||
const userData = await authStore.getSession(); |
|||
|
|||
// skip login if already logged in
|
|||
if (to.path === '/login') { |
|||
if (userData?.username) { |
|||
return navigateTo('/', { redirectCode: 302 }); |
|||
} |
|||
return; |
|||
} |
|||
// Require auth for every page other than Login
|
|||
if (!userData?.username) { |
|||
return navigateTo('/login', { redirectCode: 302 }); |
|||
} |
|||
|
|||
// Check for admin access
|
|||
if (to.path.startsWith('/admin')) { |
|||
if (userData.role !== roles.ADMIN) { |
|||
return abortNavigation('Not allowed to access Admin Panel'); |
|||
} |
|||
} |
|||
}); |
File diff suppressed because it is too large
@ -1,36 +0,0 @@ |
|||
export default defineEventHandler(async (event) => { |
|||
// TODO: improve, wrapper or smth
|
|||
const url = getRequestURL(event); |
|||
const session = await useWGSession(event); |
|||
|
|||
// Api handled by session, Setup handled with setup middleware
|
|||
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/setup')) { |
|||
return; |
|||
} |
|||
|
|||
if (url.pathname === '/login') { |
|||
if (session.data.userId) { |
|||
return sendRedirect(event, '/', 302); |
|||
} |
|||
return; |
|||
} |
|||
|
|||
// Require auth for every page other than Login
|
|||
// TODO: investigate /__nuxt_error (error page when unauthenticated)
|
|||
if (!session.data.userId) { |
|||
return sendRedirect(event, '/login', 302); |
|||
} |
|||
|
|||
if (url.pathname.startsWith('/admin')) { |
|||
const user = await Database.users.get(session.data.userId); |
|||
if (!user) { |
|||
return sendRedirect(event, '/login', 302); |
|||
} |
|||
if (user.role !== roles.ADMIN) { |
|||
throw createError({ |
|||
statusCode: 403, |
|||
statusMessage: 'Not allowed to access Admin Panel', |
|||
}); |
|||
} |
|||
} |
|||
}); |
Loading…
Reference in new issue