Laravel 9 Registration and Login using MySQL
Registration and login were separated, the user firstly needs to register and set the password accordingly. Through such a method it can gain the user’s password information while making login through multiple platforms which shall bring a better user experience. In this tutorial, we will explain how to create a user Registration and Login Module in Laravel 9 using MySQL. This is a very simple example, you can just copy-paste and change it according to your requirement.
Before started to implement the Registration and Login Module in Laravel 9 using MySQL, look files structure:
- laravel-registration-login
- app
- Console
- Exceptions
- Http
- Controllers
- Auth
- AuthController.php
- Auth
- Controllers
- Models
- User.php
- Providers
- resources
- config
- database
- public
- assets
- css
- style.css
- images
- js
- app.js
- css
- .htaccess
- index.php
- assets
- resources
- views
- dashboard.blade.php
- layout.blade.php
- auth
- login.blade.php
- registration.blade.php
- views
- routes
- api.php
- channels.php
- console.php
- web.php
- storage
- tests
- vendor
- .env
- server.php
- app
Follow the following steps and build Login and Registration process in Laravel:
- Step 1 – Firstly, Install Laravel Application
- Step 2 – Configure Database
- Step 3 – Create Controller and Methods.
- Step 4 – Create Routes
- Step 5 – Create Blade Views
- Step 6 – Finally, Run Laravel Application
Step 1 – Install Laravel 9 Application
To handle the actual install you would use the following command in your terminal the Laravel application.
1 |
composer create-project --prefer-dist laravel/laravel laravel-registration-login |
Step 2: Setup and Configure Database access
Go to app directory and open .env file. And add/update database credentials:
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=demo_DB DB_USERNAME=root DB_PASSWORD= |
Next, migrate the table into the database using the below command :
1 |
php artisan migrate |
Step 3: Create controllers
Create a controllers file named AuthController.php inside “app\Http\Controllers\Auth” folder.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
<?php /** * Description of Users Controller * * @author Team TechArise * * @email info@techarise.com */ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Session; use App\Models\User; use Hash; class AuthController extends Controller { // code login method public function index() { return view('auth.login'); } // code registration method public function registration() { return view('auth.registration'); } // code action login method public function actionLogin(Request $request) { $request->validate([ 'email' => 'required', 'password' => 'required', ]); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended('dashboard') ->withSuccess('You have Successfully loggedin'); } return redirect("login")->withSuccess('Please enter valid credentials'); } // code action registration method public function actionRegistration(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required|email|unique:users', 'password' => 'required|min:6', ]); $data = $request->all(); $check = $this->create($data); return redirect("dashboard")->withSuccess('You have Successfully logged-in'); } // code dashboard method public function dashboard() { if (Auth::check()) { return view('dashboard'); } return redirect("login")->withSuccess('You do not have access'); } // code create method public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']) ]); } // code logout method public function logout() { Session::flush(); Auth::logout(); return Redirect('login'); } } ?> |
Step 4: Create Routes
Add/Update code the file named web.php inside “routes/” folder
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 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\Auth\AuthController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ // custom create routes Route::get('/', [AuthController::class, 'index'])->name('login'); Route::get('login', [AuthController::class, 'index'])->name('login'); Route::post('action-login', [AuthController::class, 'actionLogin'])->name('login.action'); Route::get('registration', [AuthController::class, 'registration'])->name('register'); Route::post('action-registration', [AuthController::class, 'actionRegistration'])->name('register.action'); Route::get('dashboard', [AuthController::class, 'dashboard']); Route::get('logout', [AuthController::class, 'logout'])->name('logout'); ?> |
Step 6: Create Blade Views
This file contains the header and footer sections of the webpage. The Bootstrap library is used to provide a better UI.
i – Create(layout) a views file named layout.blade.php inside “resources\views\” folder.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<!doctype html> <html lang="en"> <head> <link rel="canonical" href="https://techarise.com/" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="icon" type="image/ico" href="https://techarise.com/wp-content/themes/v1/favicon.ico"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <!--- Custom css ---> <link rel="stylesheet" type="text/css" href="{{ url('/assets/css/sticky-footer-navbar.css') }}" /> <link rel="stylesheet" type="text/css" href="{{ url('/assets/css/style.css') }}" /> <title>Laravel 9 Login and Registration | Tech Arise</title> </head> <body> <header> <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <div class="container-fluid"> <a class="navbar-brand" href="https://techarise.com">TechArise</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarCollapse"> <ul class="navbar-nav me-auto mb-2 mb-md-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="https://techarise.com">Home</a> </li> @guest <li class="nav-item"> <a class="nav-link" href="{{ route('login') }}">Login</a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('register') }}">Register</a> </li> @else <li class="nav-item"> <a class="nav-link" href="{{ route('logout') }}">Logout</a> </li> @endguest </ul> </div> </div> </nav> </header> @yield('content') <footer class="footer"> <div class="container"> </div> </footer> <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> </body> </html> |
ii – Create(login) a views file named login.blade.php inside “resources\views\auth\” folder.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
@extends('layout') @section('content') <main class="flex-shrink-0"> <div class="container mt-5"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="mt-5 text-center">Laravel 9 Login and Registration</h2> <div class="card"> <div class="card-header">Login</div> <div class="card-body"> <form action="{{ route('login.action') }}" method="POST"> @csrf <div class="form-group row"> <label for="email_address" class="col-md-4 col-form-label text-md-right">E-Mail Address</label> <div class="col-md-6"> <input type="text" id="email_address" class="form-control" name="email" required autofocus> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">Password</label> <div class="col-md-6"> <input type="password" id="password" class="form-control" name="password" required> @if ($errors->has('password')) <span class="text-danger">{{ $errors->first('password') }}</span> @endif </div> </div> <div class="form-group row"> <div class="col-md-6 offset-md-4"> <div class="checkbox"> <label> <input type="checkbox" name="remember"> Remember Me </label> </div> </div> </div> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> Login </button> <a href="{{ route('register') }}" class="text-primary" style="float: right;">Register</a> </div> </form> </div> </div> </div> </div> </div> </main> @endsection |
iii – Create(registration) a views file named registration.blade.php inside “resources\views\auth\” folder.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
@extends('layout') @section('content') <main class="flex-shrink-0"> <div class="container mt-5"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="mt-5 text-center">Laravel 9 Login and Registration</h2> <div class="card"> <div class="card-header">Register</div> <div class="card-body"> <form action="{{ route('register.action') }}" method="POST"> @csrf <div class="form-group row"> <label for="name" class="col-md-4 col-form-label text-md-right">Name</label> <div class="col-md-6"> <input type="text" id="name" class="form-control" name="name" required autofocus> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> </div> <div class="form-group row"> <label for="email_address" class="col-md-4 col-form-label text-md-right">E-Mail Address</label> <div class="col-md-6"> <input type="text" id="email_address" class="form-control" name="email" required autofocus> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">Password</label> <div class="col-md-6"> <input type="password" id="password" class="form-control" name="password" required> @if ($errors->has('password')) <span class="text-danger">{{ $errors->first('password') }}</span> @endif </div> </div> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> Register </button> <a href="{{ route('login') }}" class="text-primary" style="float: right;">Login</a> </div> </form> </div> </div> </div> </div> </div> </main> @endsection |
iv – Create(dashboard) a views file named dashboard.blade.php inside “resources\views\” folder.
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 |
@extends('layout') @section('content') <main class="flex-shrink-0"> <div class="container mt-5"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="mt-5 text-center">Laravel 9 Login and Registration</h2> <div class="card"> <div class="card-header">{{ __('Dashboard') }}</div> <div class="card-body"> @if (session('success')) <div class="alert alert-success" role="alert"> {{ session('success') }} </div> @endif You are Logged In </div> </div> </div> </div> </div> </main> @endsection |
Step 6: Run Laravel Application:
Finally, start the server. So, execute the PHP artisan serve command on the terminal:
1 2 3 |
php artisan serve If want to run the project different port to use the below command php artisan serve --port=9090 |
Now, Go to your web browser, type the given URL:
1 |
http://localhost:8000/login |