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: 4 Average: 4.3]
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

Upgrade Your E-commerce Store with Magento 2 Hyvä Theme

Magento 2 Hyvä Theme is quickly becoming a popular choice among e-commerce businesses for its…

15 hours ago

A Complete Guide of Hyvä Themes

In the rapidly evolving world of e-commerce, the success of an online store greatly hinges…

15 hours ago

Magento 2: How to Add Custom Button to Download Custom Created PDF Programmatically in Admin Sales Order View

Hello Magento Friends, Ever wanted to provide admins with a quick way to download custom…

16 hours ago

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

6 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

1 week ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

2 weeks ago