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.
Contents
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
1 |
composer create-project --prefer-dist laravel/laravel ProjectName "10.*" |
Step 2: Creating the User Model and Migration
Use the below command
1 |
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.
1 2 3 4 5 6 7 |
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.
1 |
php artisan migrate |
Step 3: Creating Registration and Login Functionality
Registration:
Create a UserController using the following command:
1 |
php artisan make:controller UserController |
Open the UserController and add the following methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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
1 2 3 4 5 6 7 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 3 4 5 6 |
<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:
1 2 3 4 5 6 7 |
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!