英文:
laravel send user role into every route [using jetstream vuejs]
问题
Sure, here's the translated content:
嘿,我想在每个路由中使用后端将用户角色(使用 Spatie)作为 props 发送,以根据角色和权限在 Vue.js 中显示不同的视图。
<!-- 导航链接 -->
<div v-if="user.role == 'admin'" class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex max-w-xl md:max-w-7xl flex-wrap justify-around">
<NavLink :href="route('dashboard')" :active="route().current('dashboard')">
仪表盘
</NavLink>
</div>
即使可以通过中间件或类似的方式在每个路由中发送用户角色,也会很好。
你有什么想法吗?
英文:
Hey I want to send user role(using Spatie) as props with backend in every route to show different views based on role and permissions in Vuejs.
<!-- Navigation Links -->
<div v-if="user.role == 'admin'" class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex max-w-xl md:max-w-7xl flex-wrap justify-around">
<NavLink :href="route('dashboard')" :active="route().current('dashboard')">
Dashboard
</NavLink>
</div>
even if could send user role in every route by a middleware or something like that would be good.
do you have any idea?
答案1
得分: 1
Considering you've already created the required relations so I'm shooting this one in the dark.
Lets say you created the roles and associated them with users, you can get the user roles with something like this:
Route::middleware('auth:api')->group(function () {
Route::get('/user', function (Request $request) {
$user = $request->user();
$role = $user->roles()->first(); // Assuming your user has a "roles" relationship
return response()->json(['user' => $user, 'role' => $role->name])->header('X-User-Role', $role->name);
});
});
Once that's done then in your ajax call or vue router to intercept the request and then check the user roles to render vue js components based on the required role. You can do something like this in your vuejs :
// Vue router/index.js
import Router from 'vue-router'
Vue.use(Router)
const router = new Router({
// ...
})
router.beforeEach((to, from, next) => {
const userRole = localStorage.getItem('userRole') || null // Retrieve the user's role from local storage or set it to null
const xhr = new XMLHttpRequest()
xhr.open('GET', '/api/user')
xhr.setRequestHeader('Authorization', `Bearer ${localStorage.getItem('token')}`)
xhr.onload = () => {
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText)
localStorage.setItem('userRole', response.role) // Store the user's role in local storage
next(vm => {
vm.$options.propsData.userRole = response.role // Attach the user's role as a prop to the current route
})
} else {
next()
}
}
xhr.send()
})
This way you can easily render the required pages based on the roles. You can easily modify it according to your requirement it's not accurate but this can give you an idea how to implement it.
英文:
Considering you've already created the required relations so i'm shooting this one in the dark.
Lets say you created the roles and associated them with users, you can get the user roles with something like this:
Route::middleware('auth:api')->group(function () {
Route::get('/user', function (Request $request) {
$user = $request->user();
$role = $user->roles()->first(); // Assuming your user has a "roles" relationship
return response()->json(['user' => $user, 'role' => $role->name])->header('X-User-Role', $role->name);
});
});
Once that's done then in your ajax call or vue router to intercept the request and then check the user roles to render vue js components based on the required role. You can do something like this in your vuejs :
// Vue router/index.js
import Router from 'vue-router'
Vue.use(Router)
const router = new Router({
// ...
})
router.beforeEach((to, from, next) => {
const userRole = localStorage.getItem('userRole') || null // Retrieve the user's role from local storage or set it to null
const xhr = new XMLHttpRequest()
xhr.open('GET', '/api/user')
xhr.setRequestHeader('Authorization', `Bearer ${localStorage.getItem('token')}`)
xhr.onload = () => {
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText)
localStorage.setItem('userRole', response.role) // Store the user's role in local storage
next(vm => {
vm.$options.propsData.userRole = response.role // Attach the user's role as a prop to the current route
})
} else {
next()
}
}
xhr.send()
})
This way you can easily render the required pages based on the roles. You can easily modify it according to your requirement it's not accurate but this can give you an idea how to implement it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论