From 0c225ea18a40ecc3b92de66d57cab156fdecfa84 Mon Sep 17 00:00:00 2001 From: Slymee Date: Tue, 9 Jan 2024 14:19:36 +0545 Subject: [PATCH] Admin side multiguard authentication: complete --- app/Http/Controllers/AdminController.php | 38 ++++++++ .../AdminDataDetailsController.php | 92 ------------------- app/Http/Middleware/Authenticate.php | 2 +- .../Middleware/RedirectIfAuthenticated.php | 5 + app/Models/Admin.php | 48 ++++++++++ app/Models/User.php | 3 + config/auth.php | 17 ++++ .../2014_10_12_000000_create_users_table.php | 1 + ...2024_01_08_234553_create_admins_table.php} | 12 ++- .../{AdminDataDetails.php => UserSeeder.php} | 14 ++- resources/css/admin-login.css | 2 +- .../views/commonComponents/side-nav.blade.php | 2 +- resources/views/userend/index.blade.php | 0 .../layouts/login-form-template.blade.php | 23 +++++ resources/views/userend/login.blade.php | 38 ++++++++ resources/views/userend/register.blade.php | 47 ++++++++++ routes/web.php | 28 +++--- 17 files changed, 258 insertions(+), 114 deletions(-) create mode 100644 app/Http/Controllers/AdminController.php delete mode 100644 app/Http/Controllers/AdminDataDetailsController.php create mode 100644 app/Models/Admin.php rename database/migrations/{2023_12_11_045738_create_admin_data_details_table.php => 2024_01_08_234553_create_admins_table.php} (53%) rename database/seeders/{AdminDataDetails.php => UserSeeder.php} (62%) create mode 100644 resources/views/userend/index.blade.php create mode 100644 resources/views/userend/layouts/login-form-template.blade.php create mode 100644 resources/views/userend/login.blade.php create mode 100644 resources/views/userend/register.blade.php diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php new file mode 100644 index 0000000..dbc4e4c --- /dev/null +++ b/app/Http/Controllers/AdminController.php @@ -0,0 +1,38 @@ +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'); + } +} diff --git a/app/Http/Controllers/AdminDataDetailsController.php b/app/Http/Controllers/AdminDataDetailsController.php deleted file mode 100644 index cb088ac..0000000 --- a/app/Http/Controllers/AdminDataDetailsController.php +++ /dev/null @@ -1,92 +0,0 @@ -only('username', 'password'); - - $credentials = $request->validated(); - - try{ - if(Auth::attempt($credentials)): - return redirect()->intended('/dashboard')->with('username', $credentials['username']); - endif; - }catch(\Exception $e){ - return redirect()->back()->with('error', $e->getMessage()); - } - - return redirect()->back()->with('message', 'Credentials do not match!'); - } - - - //Logout Functionality - public function logout(Request $request){ - - try{ - return redirect('admin-login')->with(Auth::logout()); - }catch(\Exception $e){ - - } - } - - /** - * Display the specified resource. - */ - public function show(AdminDataDetails $adminDataDetails) - { - // - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(AdminDataDetails $adminDataDetails) - { - // - } - - /** - * Update the specified resource in storage. - */ - public function update(Request $request, AdminDataDetails $adminDataDetails) - { - // - } - - /** - * Remove the specified resource from storage. - */ - public function destroy(AdminDataDetails $adminDataDetails) - { - // - } -} diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index d4ef644..9e66051 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -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'); } } diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index afc78c4..ff1194a 100644 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -27,4 +27,9 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp return $next($request); } + + // app/Http/Middleware/RedirectIfAuthenticated.php + + + } diff --git a/app/Models/Admin.php b/app/Models/Admin.php new file mode 100644 index 0000000..d2b95eb --- /dev/null +++ b/app/Models/Admin.php @@ -0,0 +1,48 @@ + + */ + protected $table = 'admins'; + protected $fillable = [ + 'name', + 'username', + 'email', + 'password', + ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var array + */ + protected $hidden = [ + 'password', + 'remember_token', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + ]; + +} diff --git a/app/Models/User.php b/app/Models/User.php index 4d7f70f..ce356a8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -17,8 +17,10 @@ class User extends Authenticatable * * @var array */ + protected $table = 'users'; protected $fillable = [ 'name', + 'username', 'email', 'password', ]; @@ -42,4 +44,5 @@ class User extends Authenticatable 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; + } diff --git a/config/auth.php b/config/auth.php index 9548c15..b9873a0 100644 --- a/config/auth.php +++ b/config/auth.php @@ -40,6 +40,11 @@ 'driver' => 'session', 'provider' => 'users', ], + + 'admin' => [ + 'driver' => 'session', + 'provider' => 'admins', + ], ], /* @@ -69,6 +74,11 @@ // 'driver' => 'database', // 'table' => 'users', // ], + + 'admins' => [ + 'driver' => 'eloquent', + 'model' => App\Models\Admin::class, + ], ], /* @@ -97,6 +107,13 @@ 'expire' => 60, 'throttle' => 60, ], + + 'admins' => [ + 'provider' => 'admins', + 'table' => 'password_reset_tokens', + 'expire' => 60, + 'throttle' => 60, + ], ], /* diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 519d3ee..02b919f 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -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(); diff --git a/database/migrations/2023_12_11_045738_create_admin_data_details_table.php b/database/migrations/2024_01_08_234553_create_admins_table.php similarity index 53% rename from database/migrations/2023_12_11_045738_create_admin_data_details_table.php rename to database/migrations/2024_01_08_234553_create_admins_table.php index 7e1e554..0164dfa 100644 --- a/database/migrations/2023_12_11_045738_create_admin_data_details_table.php +++ b/database/migrations/2024_01_08_234553_create_admins_table.php @@ -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(); }); } @@ -24,6 +28,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('admin_data_details'); + Schema::dropIfExists('admins'); } }; diff --git a/database/seeders/AdminDataDetails.php b/database/seeders/UserSeeder.php similarity index 62% rename from database/seeders/AdminDataDetails.php rename to database/seeders/UserSeeder.php index f9d047d..b7bfaea 100644 --- a/database/seeders/AdminDataDetails.php +++ b/database/seeders/UserSeeder.php @@ -9,7 +9,7 @@ use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; -class AdminDataDetails extends Seeder +class UserSeeder extends Seeder { /** @@ -17,13 +17,23 @@ class AdminDataDetails extends Seeder */ public function run(): void { - DB::table('users')->insert([ + DB::table('admins')->insert([ + 'name' => 'Slymee', 'username' => 'slymee', 'email' => 'slimismurf@gmail.com', 'password' => Hash::make('slymee'), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), + ]); + DB::table('users')->insert([ + 'name' => 'MeeMee', + 'username' => 'meemee', + 'email' => 'meemee@gmail.com', + 'password' => Hash::make('meemee'), + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]); } } diff --git a/resources/css/admin-login.css b/resources/css/admin-login.css index 5d11146..8feeb3a 100644 --- a/resources/css/admin-login.css +++ b/resources/css/admin-login.css @@ -20,7 +20,7 @@ margin-right: auto; margin-left: auto; width: 60%; - height: 80%; + height: 90%; background: #f0f2ff; display: flex; justify-content: space-between; diff --git a/resources/views/commonComponents/side-nav.blade.php b/resources/views/commonComponents/side-nav.blade.php index 56133de..e09eb3c 100644 --- a/resources/views/commonComponents/side-nav.blade.php +++ b/resources/views/commonComponents/side-nav.blade.php @@ -18,7 +18,7 @@
  • Logout
  • - diff --git a/resources/views/userend/index.blade.php b/resources/views/userend/index.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/userend/layouts/login-form-template.blade.php b/resources/views/userend/layouts/login-form-template.blade.php new file mode 100644 index 0000000..a7b946f --- /dev/null +++ b/resources/views/userend/layouts/login-form-template.blade.php @@ -0,0 +1,23 @@ + + + + + + + + Dashboard-Login + + @vite(['resources/css/admin-login.css']) + + + +
    +
    +
    + Authenticate +
    + @yield('content') +
    +
    + + \ No newline at end of file diff --git a/resources/views/userend/login.blade.php b/resources/views/userend/login.blade.php new file mode 100644 index 0000000..11a1baf --- /dev/null +++ b/resources/views/userend/login.blade.php @@ -0,0 +1,38 @@ +@extends('userend.layouts.login-form-template') + +@section('content') +
    + +
    + @csrf +
    + +
    +
    +
    + +
    + + Forgot Password? + + + + + + @if(session('message')) + {{ session('message') }} + @endif + + @if($errors->any()) + @foreach ($errors->all() as $error) + {{ $error}}
    + @endforeach + @endif +
    + +
    +@endsection + + diff --git a/resources/views/userend/register.blade.php b/resources/views/userend/register.blade.php new file mode 100644 index 0000000..d431c06 --- /dev/null +++ b/resources/views/userend/register.blade.php @@ -0,0 +1,47 @@ +@extends('userend.layouts.login-form-template') + +@section('content') +
    + +
    + @csrf +
    + +
    +
    + +
    +
    + +
    +
    +
    +
    +
    + + +
    + + Forgot Password? + + + + + + @if(session('message')) + {{ session('message') }} + @endif + + @if($errors->any()) + @foreach ($errors->all() as $error) + {{ $error}}
    + @endforeach + @endif +
    + +
    +@endsection + + diff --git a/routes/web.php b/routes/web.php index 795d493..97171b1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ name('login'); -Route::post('/admin-validate', [AdminDataDetailsController::class, 'login']); -Route::post('/admin-logout', [AdminDataDetailsController::class, 'logout'])->name('logout'); +Route::get('/admin-login', [AdminController::class, 'index'])->name('admin.login'); +Route::post('/admin-validate', [AdminController::class, 'login'])->name('admin.validate'); +Route::post('/admin-logout', [AdminController::class, 'logout'])->name('admin.logout'); //Admin Dsahboard routes -Route::middleware(['auth'])->group(function () { - Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard'); - // Route::get('/admin-category', [CategoryController::class, 'index'])->name('category.and.subcategory'); - // Route::get('/admin-category-add', [CategoryController::class, 'create'])->name('add.category.form'); - // Route::post('/admin-category-add/insert', [CategoryController::class, 'store'])->name('admin.insert.category'); - // Route::get('/admin-category-edit/{category_id}', [CategoryController::class, 'edit'])->name('admin.edit.category.form'); - // Route::post('/admin-category-edit/update/{category_id}', [CategoryController::class, 'update'])->name('admin.edit.category'); - // Route::get('/admin-delete-category/{category_id}', [CategoryController::class, 'destroy'])->name('admin.delete.category'); +Route::middleware(['auth:admin'])->group(function () { + Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard'); Route::resource('admin-category', CategoryController::class, ['except' => ['destroy']]); Route::get('admin-category/{id}/destroy', [CategoryController::class, 'destroy'])->name('admin-category.destroy'); }); + //User Routes +Route::get('user/login', [UserController::class, 'userLoginForm'])->name('user.login'); +Route::post('user/user-validate', [UserController::class, 'userLogin'])->name('user.validate'); +Route::get('user/register', [UserController::class, 'userRegisterForm'])->name('user.register'); + + +