feat: auth middleware
This commit is contained in:
18
wp-content/themes/moonshine/app/middleware/hasRole.ts
Normal file
18
wp-content/themes/moonshine/app/middleware/hasRole.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
const { hasRole, isLoggedIn } = useAuth();
|
||||
if (!isLoggedIn.value) {
|
||||
return navigateTo({ path: "/connexion", query: { redirect: to.fullPath } });
|
||||
}
|
||||
if (!hasRole(to.meta.hasRole || "")) {
|
||||
return abortNavigation({
|
||||
statusCode: 403,
|
||||
message: "Vous n'avez pas les permissions requises pour accéder à cette page.",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
declare module "#app" {
|
||||
interface PageMeta {
|
||||
hasRole?: string;
|
||||
}
|
||||
}
|
||||
12
wp-content/themes/moonshine/app/middleware/isAdmin.ts
Normal file
12
wp-content/themes/moonshine/app/middleware/isAdmin.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
const { isAdmin, isLoggedIn } = useAuth();
|
||||
if (!isLoggedIn.value) {
|
||||
return navigateTo({ path: "/connexion", query: { redirect: to.fullPath } });
|
||||
}
|
||||
if (!isAdmin.value) {
|
||||
return abortNavigation({
|
||||
statusCode: 403,
|
||||
message: "Vous n'avez pas les permissions requises pour accéder à cette page.",
|
||||
});
|
||||
}
|
||||
});
|
||||
6
wp-content/themes/moonshine/app/middleware/isLoggedIn.ts
Normal file
6
wp-content/themes/moonshine/app/middleware/isLoggedIn.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
const { isLoggedIn } = useAuth();
|
||||
if (!isLoggedIn.value) {
|
||||
return navigateTo({ path: "/connexion", query: { redirect: to.fullPath } });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
const { isLoggedIn } = useAuth();
|
||||
if (isLoggedIn.value) {
|
||||
return navigateTo({ path: "/connexion", query: { redirect: to.fullPath } });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user