Laravel 10: Custom User Registration & Login Tutorial

Laravel 10 Custom User Registration & Login Tutorial

Hello Laravel Friends,

Custom user registration and login is very powerful feature in Laravel 10. That’s what we will explain you  in this blog, along with how to code.

In Laravel, by default, there is a full basic set of application authentication that contains such options as users’ registration, authentication and password restoration. However, there might be occasions in which the simplified authentication schemes described are not suitable for a given project. This tutorial will teach you more about what is required to implement a User Registration and login system in Laravel v10.

Steps to Create Custom User Registration & Login in Laravel 10:

Step 1: Set Up a Laravel 10 Project

Use the below command to set up Laravel 10 project

composer create-project --prefer-dist laravel/laravel ProjectName "10.*"

Step 2: Creating the User Model and Migration

Use the below command

php artisan make:model User -m

This command will generate a User model and its corresponding migration file.

Open the migration file located at the database/migrations and define the schema for your users table as shown below.

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->timestamps();
});

After defining the schema, run the migration to create the users table in the database.

php artisan migrate

Step 3: Creating Registration and Login Functionality

Registration:

Create a UserController using the following command:

php artisan make:controller UserController

Open the UserController and add the following methods:

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);

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

        return redirect('/login')->with('success', 'Registration successful! Please log in.');
    }
}

Next, create a blade file for the registration form at the following path

resources/views/auth/register.blade.php

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

Login:

Add the following methods to your UserController

use Illuminate\Support\Facades\Auth;

public function showLoginForm()
{
    return view('auth.login');
}

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        return redirect()->intended('/');
    }

    return redirect('/login')->with('error', 'Invalid credentials. Please try again.');
}

Create a blade file for the login form at the below path

resources/views/auth/login.blade.php

<form method="POST" action="{{ route('login') }}">
    @csrf
    <input type="email" name="email" placeholder="Email">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Login</button>
</form>

Updating Routes:

Update your routes in routes/web.php to include routes for registration and login:

use App\Http\Controllers\UserController;

Route::get('/register', [UserController::class, 'showRegistrationForm']);
Route::post('/register', [UserController::class, 'register'])->name('register');

Route::get('/login', [UserController::class, 'showLoginForm']);
Route::post('/login', [UserController::class, 'login'])->name('login');

That’s it! You’ve successfully implemented custom user registration and login functionality in Laravel 10. You can now create, register, and authenticate users in your application.

Conclusion:

In this tutorial, we’ve seen how to create a custom user registration and login system in Laravel 10. This setup provides more flexibility and customization compared to the default Laravel authentication system. If you have any doubt, share it with me through the comment section. Let your friends learn to create custom user registration and login in Laravel 10 by sharing the tutorial with them.

Also learn – How to Create User Register & Login GraphQL API in Laravel?

Happy Coding!

Previous Article

Magento 2 Extensions Digest February 2024 (New Release & Updates)

Next Article

7 Awesome B2B Website Design Examples for Inspiration

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨