• Hello, Visitor
  • Monday, February 17, 2025
Search Result
Home » Laravel » Simple User Registration with Laravel Fortify

Simple User Registration with Laravel Fortify

Step 1: Install Laravel and Fortify

First, create a new Laravel project and install Fortify.

composer create-project laravel/laravel fortify-app
cd fortify-app
composer require laravel/fortify

Step 2: Configure Fortify

Publish Fortify’s configuration file and set up the service provider.

php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
php artisan fortify:install

Register Fortify in config/app.php:

'providers' => [
    // Other Service Providers
    App\Providers\FortifyServiceProvider::class,
],

Step 3: Define Registration Logic

In app/Actions/Fortify/CreateNewUser.php, customize the user registration logic to include fields like name, email, and password.

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

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

Step 4: Create the Registration Form

In resources/views, create a new Blade file named register.blade.php and include fields for name, email, and password.

<form method="POST" action="{{ route('register') }}">
    @csrf
    <div>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" required>
    </div>
    <div>
        <label for="email">Email</label>
        <input type="email" id="email" name="email" required>
    </div>
    <div>
        <label for="password">Password</label>
        <input type="password" id="password" name="password" required>
    </div>
    <button type="submit">Register</button>
</form>

Step 5: Test the Registration Flow

Run the Laravel server and visit the registration route to test the flow.

php artisan serve

Visit http://localhost:8000/register to try out user registration.

Conclusion

With Laravel Fortify, you can quickly set up user registration along with other authentication features. Explore Fortify’s configuration for more customization options like email verification and two-factor authentication.


Leave a Reply

You must Login to add a comment