M
M
Maxim Volkov2020-12-09 16:25:41
Laravel
Maxim Volkov, 2020-12-09 16:25:41

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

2 answer(s)
S
Sanes, 2020-12-09
@Sanes

I use Fortify + Laravel-permission
Examples

D
Dmitry, 2020-12-11
@DKWLB

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();

    }

user..
$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());

and CheckRole middleware.
Of course, I spied on the network, but it works as it should for such a simple task.
If necessary, unsubscribe - I will throw off more details

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question