0

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\htdocs\2020\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']),
    ]);
}
ToxifiedHashkey
  • 267
  • 4
  • 19

2 Answers2

1

You need to return the $user from the method

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

    return $user;
}
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
0

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

Just had to add this, in the end.

return $user;
ToxifiedHashkey
  • 267
  • 4
  • 19