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

ToxifiedM's avatar

Unable to attach role to a user, when creating an account using Jetstream

I am trying to attach a role and update the registered_at field in the invitations table through the Jetstream authentication controller itself, located at App\Actions\Fortify\CreateNewUser. I want to perform the said above function when the user creates an account.

The below code is performing the function as I wanted, It's successfully updating the registered_at field, and attaching the role as well, but is not successfully redirecting to the dashboard.

Error

Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in C:\xampp\htdocs20\vendor\laravel\fortify\src\Http\Controllers\RegisteredUserController.php on line 56

App\Actions\Fortify\CreateNewUser.php - Updated Code

public function create(array $input) {
    Validator::make($input, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => $this - > passwordRules(),
    ]) - > validate();

    $user = User::create([
        'name' => $input['name'],
        'email' => $input['email'],
        'password' => Hash::make($input['password']),
    ]);

    $user - > roles() - > sync('3');
    $invitation = Invitation::where('email', $user - > email) - > firstOrFail();
    $invitation - > registered_at = $user - > created_at;
    $invitation - > save();
}

App\Actions\Fortify\CreateNewUser.php - Original Code

public function create(array $input) {
    Validator::make($input, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => $this - > passwordRules(),
    ]) - > validate();

    return User::create([
        'name' => $input['name'],
        'email' => $input['email'],
        'password' => Hash::make($input['password']),
    ]);
}
0 likes
3 replies
rodrigo.pedra's avatar
Level 56

In the original code a user is returned,. In your version there is no return statement. JetStream/Fortify expects a user instance to be returned from that method.

Add this to the end of your create method:

return $user;

Hope this helps.

1 like

Please or to participate in this conversation.