2023-12-01 17:57:14 +00:00
|
|
|
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) => {
|
2023-12-01 17:57:14 +00:00
|
|
|
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...
|
2023-12-01 17:57:14 +00:00
|
|
|
if (!roles.includes(authStore.user?.role)) {
|
2022-09-20 19:59:52 +00:00
|
|
|
next('/unauthorized')
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|