
In this tutorial we will learn step by step how to make custom login and registration system in laravel 9. We use bootstrap for designing.
We will use laravel builded user Model and migration files for login and registration system.
Following steps are required to make custom login and registration system in laravel:
Laravel new project
Database Configure
Routes Creating
Controller & Methods
Blade Views creating
Firstly we need a fresh laravel project which we can create by using composer or by installing laravel globally:
composer create-project laravel/laravel CustomLogin
Open your project on your favourite code editor and open .env file and configure your database:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=customlogin
DB_USERNAME=root
DB_PASSWORD=
Here we are going to use laravel builded User migration:
php artisan migrate
Create custom login registration and dashboard; So visit laravel application directory and open routes/web.php file and add the following routes into it:
First you need to create a balde file where you can add buttons for login and registration then visit your project and open web.php which is available in routes/web.php and post the given code in it:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuth;
Route::get('/',function(){
return view('/home');//opening a page which show buttons for login and registration
});
Route::get('/dashboard', [CustomAuth::class,'dash']);//for dashboard
Route::get('/login', [CustomAuth::class,'login_form']);//for open login form
Route::get('/register', [CustomAuth::class,'register_form']);//for open registration form
Route::post('register_user', [CustomAuth::class, 'register_succes']);//for register a new user
Route::post('login_user', [CustomAuth::class, 'login_succes']);//for login a user
Route::get('/logout', [CustomAuth::class, 'logout']);//for logout user
For CustomAuth controller run the given command on your terminal:
php artisan make:controller CustomAuth
After creating customauth controller visit App/Http/controllers where you can find your CustomAuth controller open and paste the given code:
Note here we are using session,Auth and Hash libraries which provide there functionality.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class CustomAuth extends Controller
{
//for open login form
public function login_form(){
return view('login');
}
//for open registration form
public function register_form(){
return view('register');
}
//for open dashboard page
public function dash(){
if(Auth::check()){
return view('welcome');
}
return redirect("/login")->with('message','are not allowed to access');
}
//registration of new user
public function register_succes(Request $request){
$request->validate([
'name'=>'required',
'email'=>'required',
'password'=>'required'
]);
$data = $request->all();
$check = $this->create($data);
return redirect("/dashboard")->with('message','have signed-in');
}
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
//login a user
public function login_succes(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('/dashboard')
->withSuccess('Signed in');
}
return redirect("/login")->with('message','Login details are not valid');
}
//logout a user
public function logout(){
Session::flush();
Auth::logout();
return Redirect('/login');
}
}
First we need a file which show login and logout buttons:
home.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home - Login and Registration system</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
<div class="container m-5">
<div class="row justify-content-center mb-3 ">
<a class=" btn btn-sm btn-warning" href="{{ url('/login') }}">Login</a>
</div>
<div class="row justify-content-center ">
<a class=" btn btn-sm btn-info" href="{{ url('/register') }}">Register</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
</body>
</html>
Then create a file which name is
login.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Form </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row mb-3">
<form action="{{ url('login_user') }}" method="POST">
@csrf
<div class="form-group mb-3">
<label for="email">Enter Email</label>
<input type="email" name="email" id="email" class="form-control">
</div>
<div class="form-group mb-3">
<label for="password">Enter Password</label>
<input type="password" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" value="Sign In" class="btn btn-sm btn-info form-control">
</div>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
</body>
</html>
Then create a file which name is
register.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row mb-3">
<form action="{{ url('/register_user') }}" method="POST">
@csrf
<div class="form-group mb-3">
<label for="name">Enter Name</label>
<input type="text" name="name" id="name" class="form-control">
</div>
<div class="form-group mb-3">
<label for="email">Enter Email</label>
<input type="email" name="email" id="email" class="form-control">
</div>
<div class="form-group mb-3">
<label for="password">Enter Password</label>
<input type="password" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" value="Sign Up" class="btn btn-sm btn-info form-control">
</div>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
</body>
</html>
welcome.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>
<body>
@if (session('message'))
<h1>{{ session('message') }}</h1>
@endif
<div class="justify-content-center">
<a class=" btn btn-sm btn-warning" href="{{ url('/logout') }}">Logout</a>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
</body>
</html>
For running your project run the given command which start the development server:
php artisan serve
Now you run your project by clicking on given link:
Thank You for reading Tutorial!
No Comments Yet.