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
Use the below command to set up Laravel 10 project
composer create-project --prefer-dist laravel/laravel ProjectName "10.*"
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
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.
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!
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…