opnform/resources/js/middleware/role.js

22 lines
437 B
JavaScript
Raw Normal View History

import { useAuthStore } from '../stores/auth';
2022-09-20 19:59:52 +00:00
/**
* This is middleware to check the current user role.
*
* middleware: 'role:admin,manager',
*/
export default (to, from, next, roles) => {
const authStore = useAuthStore()
2022-09-20 19:59:52 +00:00
// Split roles into an array
roles = roles.split(',')
// Check if the user has one of the required roles...
if (!roles.includes(authStore.user?.role)) {
2022-09-20 19:59:52 +00:00
next('/unauthorized')
}
next()
}