Laravel

Laravel 10: Custom User Registration & Login Tutorial

Hello Laravel Friends,

In this blog, I will show how to create custom user registration and login in Laravel 10.

By default, Laravel offers a complete authentication setup that includes features such as user registration, login, and password reset. However, in some cases, you might need a more customized authentication process to fit your project’s requirements. In this tutorial, we’ll explore how to create a custom user registration and login system in Laravel 10.

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!

Click to rate this post!
[Total: 18 Average: 4.2]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago