[Login Admin]

Done
This commit is contained in:
Arief Dwi Wicaksono 2025-09-01 09:45:58 +07:00
parent 6588ee8c46
commit f38f8a286f
12 changed files with 203 additions and 269 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminAuthController extends Controller
{
public function showLogin()
{
return view('admin.auth.login');
}
public function login(Request $request)
{
$credentials = $request->validate([
'email' => ['required','email'],
'password' => ['required'],
]);
$remember = $request->boolean('remember');
if (Auth::guard('admin')->attempt($credentials, $remember)) {
$request->session()->regenerate();
return redirect()->intended(route('admin.dashboard'));
}
return back()->withErrors([
'email' => 'Email atau password salah.',
])->onlyInput('email');
}
public function logout(Request $request)
{
Auth::guard('admin')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('admin.login');
}
}

View File

@ -1,36 +0,0 @@
<?php
namespace App\Http\Controllers\Adm;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AdminAuthController extends Controller
{
// Form Login
public function showLogin()
{
return view('admin.login');
}
// Proses Login
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::guard('admin')->attempt($credentials)) {
return redirect()->route('admin.dashboard')->with('success', 'Login berhasil!');
}
return back()->with('error', 'Email atau password salah.');
}
// Logout
public function logout(Request $request)
{
Auth::guard('admin')->logout();
return redirect()->route('admin.login');
}
}

View File

@ -9,13 +9,7 @@ class Admin extends Authenticatable
{ {
use Notifiable; use Notifiable;
protected $table = 'admins'; protected $fillable = ['name','email','password'];
protected $fillable = [ protected $hidden = ['password','remember_token'];
'username', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
} }

View File

@ -2,35 +2,11 @@
return [ return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [ 'defaults' => [
'guard' => 'web', 'guard' => 'web',
'passwords' => 'users', 'passwords' => 'users',
], ],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| A great default configuration has been defined for you here which
| uses session storage and the Eloquent user provider.
|
| Supported drivers: "session", "token"
|
*/
'guards' => [ 'guards' => [
// Guard untuk user biasa // Guard untuk user biasa
'web' => [ 'web' => [
@ -38,39 +14,13 @@ return [
'provider' => 'users', 'provider' => 'users',
], ],
// Guard untuk admin (login via web session) // Guard untuk admin
'admin' => [ 'admin' => [
'driver' => 'session', 'driver' => 'session',
'provider' => 'admins', 'provider' => 'admins',
], ],
// Guard API untuk user (misalnya dengan sanctum / token)
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
], ],
// Guard API untuk admin
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are retrieved from your database or other storage systems.
|
| Supported drivers: "database", "eloquent"
|
*/
'providers' => [ 'providers' => [
// Provider untuk user biasa // Provider untuk user biasa
'users' => [ 'users' => [
@ -83,25 +33,8 @@ return [
'driver' => 'eloquent', 'driver' => 'eloquent',
'model' => App\Models\Admin::class, 'model' => App\Models\Admin::class,
], ],
// Kalau mau pakai database langsung (tidak lewat model)
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
], ],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| Anda dapat menentukan beberapa konfigurasi reset password jika ada
| lebih dari satu tabel atau model user dan ingin pengaturan reset
| yang berbeda berdasarkan tipe user tertentu.
|
*/
'passwords' => [ 'passwords' => [
'users' => [ 'users' => [
'provider' => 'users', 'provider' => 'users',
@ -109,8 +42,6 @@ return [
'expire' => 60, 'expire' => 60,
'throttle' => 60, 'throttle' => 60,
], ],
// Kalau admin juga butuh reset password, bisa tambahkan ini
'admins' => [ 'admins' => [
'provider' => 'admins', 'provider' => 'admins',
'table' => 'password_reset_tokens', 'table' => 'password_reset_tokens',
@ -119,16 +50,6 @@ return [
], ],
], ],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Jumlah detik sebelum konfirmasi password kadaluarsa.
| Default: 3 jam (10800 detik).
|
*/
'password_timeout' => 10800, 'password_timeout' => 10800,
]; ];

View File

@ -5,10 +5,11 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
return new class extends Migration { return new class extends Migration {
public function up(): void { public function up(): void
{
Schema::create('admins', function (Blueprint $table) { Schema::create('admins', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('username')->unique(); $table->string('name');
$table->string('email')->unique(); $table->string('email')->unique();
$table->string('password'); $table->string('password');
$table->rememberToken(); $table->rememberToken();
@ -16,7 +17,8 @@ return new class extends Migration {
}); });
} }
public function down(): void { public function down(): void
{
Schema::dropIfExists('admins'); Schema::dropIfExists('admins');
} }
}; };

View File

@ -2,16 +2,20 @@
namespace Database\Seeders; namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents; use App\Models\Admin;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class AdminSeeder extends Seeder class AdminSeeder extends Seeder
{ {
/**
* Run the database seeds.
*/
public function run(): void public function run(): void
{ {
// Admin::updateOrCreate(
['email' => 'admin@example.com'],
[
'name' => 'Super Admin',
'password' => Hash::make('password123'), // ganti setelah login
]
);
} }
} }

View File

@ -7,16 +7,10 @@ use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
/**
* Seed the application's database.
*/
public function run(): void public function run(): void
{ {
// \App\Models\User::factory(10)->create(); $this->call([
AdminSeeder::class,
// \App\Models\User::factory()->create([ ]);
// 'name' => 'Test User', }
// 'email' => 'test@example.com',
// ]);
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,109 @@
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background: #ffffff;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.login-card {
background: #EFEFEF;
border-radius: 15px;
box-shadow: 0 0 25px rgb(0, 123, 255);
width: 100%;
max-width: 400px; /* batas lebar maksimum */
padding: 30px;
}
.login-logo {
display: block;
margin: 0 auto 15px;
max-width: 130px;
}
.btn-login {
background-color: #3bb9ff;
border: none;
border-radius: 8px;
}
.btn-login:hover {
background-color: #1a8edb;
}
.form-control {
border-radius: 10px;
}
/* Responsive padding */
@media (max-width: 576px) {
.login-card {
padding: 20px;
margin: 10px;
}
.login-logo {
max-width: 100px;
}
h4 {
font-size: 1.2rem;
}
p {
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<div class="login-card">
<div class="text-center mb-3">
<!-- Logo -->
<img src="{{ asset('images/logo.png') }}" alt="Logo" class="login-logo">
</div>
<h4 class="text-center mb-2 fw-bold">SELAMAT DATANG</h4>
<p class="text-center text-muted mb-4">Selamat datang! Silakan masukkan detail Anda.</p>
@if ($errors->any())
<div class="alert alert-danger">
{{ $errors->first() }}
</div>
@endif
<form action="{{ route('admin.login.post') }}" method="POST">
@csrf
<div class="mb-3">
<label class="form-label">Nama</label>
<input type="email" name="email" value="{{ old('email') }}" class="form-control"
placeholder="Masukkan nama Anda" required autofocus>
</div>
<div class="mb-3">
<label class="form-label">Kata Sandi</label>
<input type="password" name="password" class="form-control"
placeholder="Masukkan kata sandi Anda" required>
</div>
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember">
<label class="form-check-label" for="remember">Ingat saya</label>
</div>
<a href="#" class="text-decoration-none mt-2 mt-sm-0">Lupa kata sandi</a>
</div>
<button type="submit" class="btn btn-login text-white w-100">Login</button>
</form>
</div>
</body>
</html>

View File

@ -1,14 +1,21 @@
<!DOCTYPE html> <!doctype html>
<html> <html lang="id">
<head> <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Dashboard</title> <title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head> </head>
<body> <body class="p-4">
<h1>Halo, {{ Auth::guard('admin')->user()->username }}</h1> <div class="container">
<div class="d-flex justify-content-between align-items-center mb-4">
<form method="POST" action="{{ route('admin.logout') }}"> <h3>Admin Dashboard</h3>
<form action="{{ route('admin.logout') }}" method="POST">
@csrf @csrf
<button type="submit">Logout</button> <button class="btn btn-outline-danger">Logout</button>
</form> </form>
</div>
<div class="alert alert-success">Berhasil login sebagai <strong>{{ auth('admin')->user()->name }}</strong></div>
</div>
</body> </body>
</html> </html>

View File

@ -1,114 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome (kalau tidak dipakai bisa dihapus) -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
body {
background-color: #ffffff;
height: 100vh;
}
.login-card {
width: 380px;
border-radius: 15px;
padding: 30px;
background: #ffffff;
box-shadow: 0 0 25px rgb(0, 123, 255);
}
.login-card img {
width: 150px;
display: block;
margin: 0 auto 20px;
}
.login-card h4 {
font-weight: 700;
text-align: center;
margin-bottom: 10px;
}
.login-card p {
text-align: center;
color: #6c757d;
margin-bottom: 20px;
}
.form-control {
border-radius: 10px;
padding: 10px 15px;
}
.btn-login {
background: #2ea8ff;
border: none;
border-radius: 10px;
padding: 10px;
font-size: 16px;
font-weight: 600;
color: #fff;
transition: 0.3s;
}
.btn-login:hover {
background: #0d6efd;
}
.remember-forgot {
font-size: 14px;
}
.remember-forgot a {
color: #0d6efd;
text-decoration: none;
}
.remember-forgot a:hover {
text-decoration: underline;
}
/* Samakan panjang label */
.login-card .form-label {
display: block;
width: 100%;
font-weight: 500;
}
</style>
</head>
<body class="d-flex justify-content-center align-items-center">
<div class="login-card">
<!-- Logo -->
<img src="{{ asset('images/abbauf.png') }}" alt="Logo">
<h4>WELCOME BACK</h4>
<p>Welcome back! Please enter your details.</p>
{{-- Notifikasi --}}
@if(session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif
@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
<form method="POST" action="{{ route('admin.login.submit') }}">
@csrf
<div class="mb-3">
<label class="form-label w-100">Username</label>
<input type="text" name="email" class="form-control" placeholder="Enter your name" required>
</div>
<div class="mb-3">
<label class="form-label w-100">Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="********" required>
</div>
<div class="d-flex justify-content-between align-items-center mb-3 remember-forgot">
<div>
<input type="checkbox" id="remember">
<label for="remember">Remember me</label>
</div>
<a href="#">Forgot password</a>
</div>
<button type="submit" class="btn btn-login w-100">Login</button>
</form>
</div>
</body>
</html>

View File

@ -1,7 +1,19 @@
<?php <?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminAuthController; use App\Http\Controllers\AdminAuthController;
Route::get('admin/login', [AdminAuthController::class, 'showLogin'])->name('admin.login'); Route::prefix('admin')->name('admin.')->group(function () {
Route::post('admin/login', [AdminAuthController::class, 'login'])->name('admin.login.post'); Route::middleware('guest:admin')->group(function () {
Route::post('admin/logout', [AdminAuthController::class, 'logout'])->name('admin.logout'); Route::get('/login', [AdminAuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AdminAuthController::class, 'login'])->name('login.post');
});
Route::middleware('auth:admin')->group(function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
})->name('dashboard');
Route::post('/logout', [AdminAuthController::class, 'logout'])->name('logout');
});
});