Laravel 使用 Jetstream 和 Vue.js 将用户角色发送到每个路由。

huangapple go评论78阅读模式
英文:

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.

&lt;!-- Navigation Links --&gt;
&lt;div v-if=&quot;user.role == &#39;admin&#39;&quot; class=&quot;hidden space-x-8 sm:-my-px sm:ml-10 sm:flex max-w-xl md:max-w-7xl flex-wrap justify-around&quot;&gt;
&lt;NavLink :href=&quot;route(&#39;dashboard&#39;)&quot; :active=&quot;route().current(&#39;dashboard&#39;)&quot;&gt;
                                    Dashboard
&lt;/NavLink&gt;
&lt;/div&gt;

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(&#39;auth:api&#39;)-&gt;group(function () {
    Route::get(&#39;/user&#39;, function (Request $request) {
        $user = $request-&gt;user();
        $role = $user-&gt;roles()-&gt;first(); // Assuming your user has a &quot;roles&quot; relationship
        return response()-&gt;json([&#39;user&#39; =&gt; $user, &#39;role&#39; =&gt; $role-&gt;name])-&gt;header(&#39;X-User-Role&#39;, $role-&gt;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 &#39;vue-router&#39;

Vue.use(Router)

const router = new Router({
  // ...
})

router.beforeEach((to, from, next) =&gt; {
  const userRole = localStorage.getItem(&#39;userRole&#39;) || null // Retrieve the user&#39;s role from local storage or set it to null
  const xhr = new XMLHttpRequest()
  xhr.open(&#39;GET&#39;, &#39;/api/user&#39;)
  xhr.setRequestHeader(&#39;Authorization&#39;, `Bearer ${localStorage.getItem(&#39;token&#39;)}`)
  xhr.onload = () =&gt; {
    if (xhr.status === 200) {
      const response = JSON.parse(xhr.responseText)
      localStorage.setItem(&#39;userRole&#39;, response.role) // Store the user&#39;s role in local storage
      next(vm =&gt; {
        vm.$options.propsData.userRole = response.role // Attach the user&#39;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.

huangapple
  • 本文由 发表于 2023年4月19日 18:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定