Answer the question
In order to leave comments, you need to log in
Laravel + Jetstream, how to create and distribute user roles?
Deployed a project on Laravel 8 with a package for authenticating Jetstream users. But there were questions about the distribution of user roles. Only two roles are required, Administrator - with full rights and just registered users.
At first glance, Jetstream is functional and convenient, it provides the possibility of registration, authorization, etc. But the logic of the work of the default Jetstream differs from the necessary one.
According to the Jetstream documentation, when installing a package with the --teams option, by default, a new account and team is created for each registered user and a team to which it adds users and assigns roles to them.
But how to create only two types of users on Laravel + Jetstream: an Administrator for working with the site content through the admin panel and a regular User, and so that all registered users belong to the User group by default?
I didn’t find a solution in the documentation for Jetstream, I didn’t find any tutorials on the network either.
Maybe it's better to use not Jetstream, but some other solution? I ask for advice.
Answer the question
In order to leave comments, you need to log in
Are you building a project without api? on pure laravel? I made the issue easier. Breeze for basic authentication.
Then just manyToMany User-Role and through the seeder -
public function run()
{
$role_admin_user = new Role;
$role_admin_user->name = 'admin';
$role_admin_user->description = 'An admin user';
$role_admin_user->save();
$role_regular_user = new Role;
$role_regular_user->name = 'user';
$role_regular_user->description = 'A regular user';
$role_regular_user->save();
}
$admin = new User;
$admin->name = 'Admin Name';
$admin->email = '[email protected]';
$admin->password = bcrypt('secret');
$admin->save();
$admin->roles()->attach(Role::where('name', 'admin')->first());
$user = new User;
$user->name = 'User';
$user->email = '[email protected]';
$user->password = bcrypt('secret');
$user->save();
$user->roles()->attach(Role::where('name', 'user')->first());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question