Instal Laravel
Run following Commands :
composer create-project --prefer-dist laravel/laravel laravel_custom_auth
cd laravel_custom_auth
php artisan migrate
Add required address,phone_no column in users table in database
By default a users table will be generated in the database by migration
App/Models/User.php
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = ['name','email','password','address', 'phone_no','created_at','updated_at'];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\DashboardController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
/*-----User Routes----*/
Route::group(["middleware" => ['guest']],function () {
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/auth_login', [LoginController::class, 'auth_login'])->name('auth_login');
Route::get('/registrations', [LoginController::class, 'registrations'])->name('registrations');
Route::post('/registrations_save', [LoginController::class, 'registrations_save'])->name('registrations_save');
});
Route::group(["middleware" => ['auth']],function () {
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
Route::any('/logout', [DashboardController::class, 'logout'])->name('logout');
});
Create LoginController
php artisan make:controller LoginController
app/Http/Controllers/LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class LoginController extends Controller
{
public function index(Request $request)
{
return view('login');
}
public function auth_login(Request $request)
{
$request->validate([
'email'=>'required|email',
'password'=>'required'
]);
$email = $request->input('email');
$password = $request->input('password');
if(Auth::attempt(['email' => $email, 'password' => $password])){
return redirect()->route('dashboard');
}else{
return redirect()->route('login')->with('error', 'Invalid Email Or Password..')->withInput();;
}
}
public function registrations(Request $request)
{
return view('registrations');
}
public function registrations_save(Request $request)
{
$request->validate([
'name'=> 'required',
'email'=>'required|email',
'password'=>'required',
'address' => 'required',
'phone_no' => 'required'
]);
$email = $request->input('email');
$password = $request->input('password');
$user = User::where('email', $email)->first();
if($user)
{
session()->flash('error', 'This Email is already Registered');
return redirect()->route('registrations')->withInput();
}else{
$user_data = array(
'name' => $request->input('name'),
'email' => $email,
'password' => Hash::make($request->input('password')),
'phone_no' => $request->input('phone_no'),
'address' => $request->input('address'),
'created_at' => date("Y-m-d H:i:s"),
);
$user = User::create($user_data);
if($user->id){
if(Auth::attempt(['email' => $email, 'password' => $password])){
return redirect()->route('dashboard');
}else{
return redirect()->route('registrations')->with('error', 'Something went wrong.Please Login again..')->withInput();;
}
}else{
return redirect()->route('registrations')->with('error', 'Something went wrong.Please Try again..')->withInput();;
}
}
}
}
Create Dashboard Controller
php artisan make:controller DashboardController
app/Http/Controllers/DashboardController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DashboardController extends Controller
{
public function index(Request $request)
{
return view('dashboard');
}
public function logout(Request $request)
{
Auth::logout();
return redirect()->route('login');
}
}
Create login view file
app/resources/views/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 Page</title>
<!-- 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">
</head>
<body>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-2"></div>
<div class="col-lg-6 col-md-8 login-box">
<div class="col-lg-12 login-key">
<i class="fa fa-key" aria-hidden="true"></i>
</div>
<div class="col-lg-12 login-title">
USER LOGIN
</div>
<br>
<div class="col-lg-12 login-form">
<div class="col-lg-12 login-form">
<form action="{{ route('auth_login') }}" method="post" id="login-portal-form" class="frm_submit" name="login-portal-form">
@csrf
@if(Session::has('error'))
<h2 style="color:red;">{{ Session::get('error'); }}</h2>
@endif
<div class="form-group">
<label class="form-control-label">USERNAME</label>
<input class="form-control" type="text" name="email" id="email" placeholder="Email" value="{{ old('email') }}">
@error('email')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<div class="form-group">
<label class="form-control-label">PASSWORD</label>
<input class="form-control" type="password" name="password" id="password" placeholder="Password">
@error('password')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<br>
<div class="col-lg-12 loginbttm">
<div class="col-lg-12 login-btm login-button text-center">
<input type="submit" name="submit" class="btn btn-outline-primary" id="submit" value="submit">
</div>
</div>
<br>
<div class="col-lg-12 text-center">
<a href="{{ route('registrations') }}">Sign Up</a>
</div>
</form>
</div>
</div>
<div class="col-lg-3 col-md-2"></div>
</div>
</div>
</div>
</body>
</html>
Create Registrations view file
app/resources/views/registrations.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Registration Page</title>
<!-- 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">
</head>
<body>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-2"></div>
<div class="col-lg-6 col-md-8 login-box">
<div class="col-lg-12 login-key">
<i class="fa fa-key" aria-hidden="true"></i>
</div>
<div class="col-lg-12 login-title">
USER REGISTRATION
</div>
<br>
<div class="col-lg-12 login-form">
<div class="col-lg-12 login-form">
<form action="{{ route('registrations_save') }}" method="post" id="login-portal-form" class="frm_submit" name="login-portal-form">
@csrf
@if(Session::has('error'))
<h2 style="color:red;">{{ Session::get('error'); }}</h2>
@endif
<div class="form-group">
<label class="form-control-label">User Name</label>
<input class="form-control" type="text" name="name" id="name" placeholder="Name" value="{{ old('name') }}">
@error('name')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<div class="form-group">
<label class="form-control-label">Email</label>
<input class="form-control" type="text" name="email" id="email" placeholder="Email" value="{{ old('email') }}">
@error('email')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<div class="form-group">
<label class="form-control-label">Password</label>
<input class="form-control" type="password" name="password" id="password" placeholder="Password">
<!-- <input type="password" class="form-control" i> -->
@error('password')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<div class="form-group">
<label class="form-control-label">Address</label>
<input class="form-control" type="text" name="address" id="address" placeholder="Address" value="{{ old('address') }}">
@error('address')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<div class="form-group">
<label class="form-control-label">Phone No.</label>
<input class="form-control" type="text" name="phone_no" id="phone_no" placeholder="Phone No" value="{{ old('phone_no') }}">
@error('phone_no')
<label class="error" style="color:red">{{ $message }}</label>
@enderror
</div>
<br>
<div class="col-lg-12 loginbttm">
<div class="col-lg-12 login-btm login-button text-center">
<input type="submit" name="submit" class="btn btn-outline-primary" id="submit" value="submit">
</div>
</div>
<br>
<div class="col-lg-12 text-center">
<a href="{{ route('login') }}">Sign In</a>
</div>
</form>
</div>
</div>
<div class="col-lg-3 col-md-2"></div>
</div>
</div>
</div>
</body>
</html>
Create dashboard view file
app/resources/views/dashboard.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Yinka Enoch Adedokun">
<title>Dashboard Page</title>
<!-- 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">
</head>
<body>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-2"></div>
<div class="col-lg-6 col-md-8 login-box">
<div class="col-lg-12 login-key">
<i class="fa fa-key" aria-hidden="true"></i>
</div>
<div class="row">
<div class="col-lg-6">
DASHBOARD
</div>
<div class="col-lg-6 ">
<a href="{{ route('logout') }}">logout</a>
</div>
</div>
<br>
<div class="col-lg-12 login-form">
<div class="col-lg-12 login-form">
{{ Auth::user()->name;}}
<br>
{{ Auth::user()->email}}
</div>
</div>
<div class="col-lg-3 col-md-2"></div>
</div>
</div>
</div>
</body>
</html>