Randy_Johnson's avatar

Spatie Roles with ReactJS ✌

Hey can you use Spatie Roles with ReactJS and Inertia?

0 likes
3 replies
hupp's avatar
hupp
Best Answer
Level 11

@randy_johnson Absolutely! You can use it with any of them, its works fine. Here is code snippet for ReactJS & Inertia : ReactJS component using Inertia

import { usePage } from '@inertiajs/inertia-react';
const UserProfile = () => {
  const { user } = usePage().props;
  return (
    <div>
      <h2>Welcome, {user.name}!</h2>
      <p>Roles: {user.roles.join(', ')}</p>
      <p>Permissions: {user.permissions.join(', ')}</p>
    </div>
  );
};

Laravel Controller :

public function index()
{
    $user = auth()->user();
    $roles = $user->getRoleNames();
    $permissions = $user->getAllPermissions();
    return inertia('UserProfile', [
        'user' => [
            'name' => $user->name,
            'roles' => $roles,
            'permissions' => $permissions,
        ],
    ]);
}

Please or to participate in this conversation.