laravelfortifylaravel-livewirecustom-authentication

How to send data colected with Laravel Livewire to Fortify?


I'm not familiar with Vue.js at all, so found a good replacement using Livewire.

The challenge that I've got to solve is to have a user friendly registration on my website using Fortify + Livewire. The registration process is a multistep one and depends on the choices that the user makes it will load the relative fields.

So far I set up the Fortify views by adding in the FortifyServiceProvider.php file the following code:

Fortify::loginView(function () {
    return view('auth.login');
});

Fortify::registerView(function () {
    return view('auth.register');
});

The auth/login.blade.php view loading the livewire component which is basically a form:

<form action="{{ route('register') }}" method="POST" wire:submit.prevent="submit">
    /**
    * Here would go the inputs that must be shown depends on what users choice 
    * (is it an ordinar user or a company)
    */
    <button type="submit">Save<button/>
</form>

The multiform challenge would be resolved by adding $step property into the Register.php class:

class RegisterForm extends Component
{
    public $step;

    public function mount()
    {
        $this->step = 0;
    }

    public function submit()
    {
        if ($this->step < 3) {
            $this->step++;           
        } else {
            // pass all the data to the fortify register method
            // <-- Here is my trouble!
        }
    }
}

which will be incremented by passing each of the registration steps ($this->step++).

The most important thing that is quite complicated for me is how to prevent form submission to have the validation + form changes and by the end all the set of the data to pass trough Fortify registration process?


Solution

  • Look at the fortify Controller for register

    public function store(Request $request, CreatesNewUsers $creator): RegisterResponse
        {
            event(new Registered($user = $creator->create($request->all())));
            $this->guard->login($user);
            return app(RegisterResponse::class);
        }
    

    T

    use Illuminate\Auth\Events\Registered;
    use Illuminate\Support\Facades\Auth;
    use Laravel\Fortify\Contracts\CreatesNewUsers;
    use Livewire\Component;
    
    class Register extends Component
    {
        public $name;
        public $email;
        public $password;
        public $password_confirmation;
    
        public function submit(CreatesNewUsers $creator)
        {
            event(new Registered($user = $creator->create([
                'name' => $this->name,
                'email' => $this->email,
                'password' => $this->password,
                'password_confirmation' => $this->password_confirmation,
            ])));
    
            Auth::guard()->login($user);
    
            $this->redirect('home');
        }
    
        public function render()
        {
            return view('livewire.register');
        }
    }
    

    Something like this will work for your use case.

    You are still using the fortify Action and Still Firing the Event