Skip to content

Commit

Permalink
Admin side multiguard authentication: complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee authored and aashish-t-magar committed Jan 11, 2024
1 parent a350f24 commit 0c225ea
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 114 deletions.
38 changes: 38 additions & 0 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\LoginFormValidator;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
//display login form
public function index(){
return view('backend.admin-login');
}

//login module
public function login(LoginFormValidator $request){
try{
if(auth()->guard('admin')->attempt($request->only(['username', 'password']))):
return redirect()->intended('/admin/dashboard');
else:
return redirect()->back()->with('message', 'Invalid Credentials');
endif;
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}


//logout module
public function logout(Request $request){
Auth::guard('admin')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/admin-login');
}
}
92 changes: 0 additions & 92 deletions app/Http/Controllers/AdminDataDetailsController.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Authenticate extends Middleware
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('login');
return $request->expectsJson() ? null : route('admin.login');
}
}
5 changes: 5 additions & 0 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp

return $next($request);
}

// app/Http/Middleware/RedirectIfAuthenticated.php



}
48 changes: 48 additions & 0 deletions app/Models/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 Admin extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $table = 'admins';
protected $fillable = [
'name',
'username',
'email',
'password',
];

/**
* 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',
'password' => 'hashed',
];

}
3 changes: 3 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class User extends Authenticatable
*
* @var array<int, string>
*/
protected $table = 'users';
protected $fillable = [
'name',
'username',
'email',
'password',
];
Expand All @@ -42,4 +44,5 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

}
17 changes: 17 additions & 0 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
'driver' => 'session',
'provider' => 'users',
],

'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],

/*
Expand Down Expand Up @@ -69,6 +74,11 @@
// 'driver' => 'database',
// 'table' => 'users',
// ],

'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],

/*
Expand Down Expand Up @@ -97,6 +107,13 @@
'expire' => 60,
'throttle' => 60,
],

'admins' => [
'provider' => 'admins',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/
public function up(): void
{
Schema::create('admin_data_details', function (Blueprint $table) {
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('admin_username')->unique();
$table->string('admin_password');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Expand All @@ -24,6 +28,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('admin_data_details');
Schema::dropIfExists('admins');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class AdminDataDetails extends Seeder
class UserSeeder extends Seeder

{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('users')->insert([
DB::table('admins')->insert([
'name' => 'Slymee',
'username' => 'slymee',
'email' => '[email protected]',
'password' => Hash::make('slymee'),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);

DB::table('users')->insert([
'name' => 'MeeMee',
'username' => 'meemee',
'email' => '[email protected]',
'password' => Hash::make('meemee'),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),

]);
}
}
2 changes: 1 addition & 1 deletion resources/css/admin-login.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
margin-right: auto;
margin-left: auto;
width: 60%;
height: 80%;
height: 90%;
background: #f0f2ff;
display: flex;
justify-content: space-between;
Expand Down
2 changes: 1 addition & 1 deletion resources/views/commonComponents/side-nav.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<a href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"><li><i class="fa-solid fa-layer-group"></i>
Logout</li></a>

<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
<form id="logout-form" action="{{ route('admin.logout') }}" method="POST" style="display: none;">
@csrf
</form>
</ul>
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions resources/views/userend/layouts/login-form-template.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href='https://fonts.googleapis.com/css?family=Poppins' rel='stylesheet'>
<title>Dashboard-Login</title>

@vite(['resources/css/admin-login.css'])

</head>
<body>
<section class="main-section">
<div class="form-banner">
<div class="img-place">
<img class="image" src="{{ URL('images/authenticate.jpg') }}" alt="Authenticate" srcset="">
</div>
@yield('content')
</div>
</section>
</body>
</html>
Loading

0 comments on commit 0c225ea

Please sign in to comment.