Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bozsikarmand's avatar

Laravel Jetstream multi auth with Laratrust

Hello,

Firstly, I have to mention that I am fairly new to the Laravel world, but I am eager to learn. In my current project, I would like to create a role-based authentication with Laravel 8, Jetstream and Laratrust.

For that I modified CreateNewUser.php as:

$user = User::create([
    'username' => $input['username'],
        'email' => $input['email'],
        'password' => Hash::make($input['password']),
    ]);
    $user->attachRole('visitor');
    return $user;

It links the desired role to the user. After the registration, I send out a verification email via the built-in functionality of Jetstream. When I click on the link in the mail I am redirected to /dashboard. There in the Profile I must provide additional data (I also put these columns in the corresponding migration file earlier).

UpdateUserProfileInformation:

if ($input['email'] !== $user->email &&
        $user instanceof MustVerifyEmail) {
        $this->updateVerifiedUser($user, $input);
    } else {
        if ($user->first_login === false) {
            $user->forceFill([
                'first_login' => true,
            ])->save();
        }

and

$user->forceFill([
            'firstname' => $input['firstname'],
            'middlename' => $input['middlename'],
            'lastname' => $input['lastname'],
            'username' => $input['username'],
            'email' => $input['email'],
            'landlinetel' => $input['landlinetel'],
            'mobiletel' => $input['mobiletel'],
            'mandatory_fields_filled' => true,
        ])->save();

        Auth::logout();
        Session::flush();
        redirect(route('login'));

So after I provide the data I must log out (because there is a role change when I log in for the next time:

LoginResponse.php:

public function toResponse($request)
{
    $user = auth()->user();
    if ($user->mandatory_fields_filled && $user->hasRole('admin')) {
        $home = '/admin';
        return redirect()->intended($home);
    } elseif ($user->mandatory_fields_filled && $user->hasRole('user')) {
        $home = '/user';
        return redirect()->intended($home);
    } elseif ($user->hasRole('visitor')) {
        if ($user->mandatory_fields_filled) {
            $user->detachRole('visitor');
            $user->attachRole('user');
        }
        $home = '/dashboard';
        return redirect()->intended($home);
    } else {
        $home = '/';
        return redirect()->intended($home);
    }
}

To test, I manually edited the field in user_role to user role's id. After that I logged in so I was succesfully redirected to /user (which is 404 at the moment, it is under construction), but when I pressed back button, I was redirected to /dashboard as if I was logged in.

Is there any civilised method to fix my auth? Jetstream Teams is not an option unfortunately.

Thanks!

Armand

0 likes
0 replies

Please or to participate in this conversation.