Skip to content

Commit 8cbce32

Browse files
feat: implement Admin model, authentication guard, and migration for admin users
1 parent fe05f7c commit 8cbce32

5 files changed

Lines changed: 181 additions & 7 deletions

File tree

app/Models/Admin.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Filament\Auth\MultiFactor\App\Concerns\InteractsWithAppAuthentication;
6+
use Filament\Auth\MultiFactor\App\Contracts\HasAppAuthentication;
7+
use Filament\Models\Contracts\FilamentUser;
8+
use Illuminate\Foundation\Auth\User as Authenticatable;
9+
use Filament\Panel;
10+
use Illuminate\Console\Attributes\Hidden;
11+
use Illuminate\Database\Eloquent\Attributes\Fillable;
12+
13+
#[Fillable([
14+
'name',
15+
'email',
16+
'password',
17+
])]
18+
#[Hidden([
19+
'password',
20+
])]
21+
class Admin extends Authenticatable implements FilamentUser, HasAppAuthentication
22+
{
23+
use InteractsWithAppAuthentication;
24+
25+
public function canAccessPanel(Panel $panel): bool
26+
{
27+
return true;
28+
}
29+
}

app/Providers/Filament/AdminPanelProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function panel(Panel $panel): Panel
5252
])
5353
->authMiddleware([
5454
Authenticate::class,
55-
]);
55+
])
56+
->authGuard('admin');
5657
}
5758
}

config/auth.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Authentication Defaults
8+
|--------------------------------------------------------------------------
9+
|
10+
| This option defines the default authentication "guard" and password
11+
| reset "broker" for your application. You may change these values
12+
| as required, but they're a perfect start for most applications.
13+
|
14+
*/
15+
16+
'defaults' => [
17+
'guard' => env('AUTH_GUARD', 'web'),
18+
'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
19+
],
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Authentication Guards
24+
|--------------------------------------------------------------------------
25+
|
26+
| Next, you may define every authentication guard for your application.
27+
| Of course, a great default configuration has been defined for you
28+
| which utilizes session storage plus the Eloquent user provider.
29+
|
30+
| All authentication guards have a user provider, which defines how the
31+
| users are actually retrieved out of your database or other storage
32+
| system used by the application. Typically, Eloquent is utilized.
33+
|
34+
| Supported: "session"
35+
|
36+
*/
37+
38+
'guards' => [
39+
'web' => [
40+
'driver' => 'session',
41+
'provider' => 'users',
42+
],
43+
44+
'admin' => [
45+
'driver' => 'session',
46+
'provider' => 'admins',
47+
],
48+
],
49+
50+
/*
51+
|--------------------------------------------------------------------------
52+
| User Providers
53+
|--------------------------------------------------------------------------
54+
|
55+
| All authentication guards have a user provider, which defines how the
56+
| users are actually retrieved out of your database or other storage
57+
| system used by the application. Typically, Eloquent is utilized.
58+
|
59+
| If you have multiple user tables or models you may configure multiple
60+
| providers to represent the model / table. These providers may then
61+
| be assigned to any extra authentication guards you have defined.
62+
|
63+
| Supported: "database", "eloquent"
64+
|
65+
*/
66+
67+
'providers' => [
68+
'users' => [
69+
'driver' => 'eloquent',
70+
'model' => env('AUTH_MODEL', App\Models\User::class),
71+
],
72+
73+
'admins' => [
74+
'driver' => 'eloquent',
75+
'model' => env('AUTH_MODEL', App\Models\Admin::class),
76+
],
77+
],
78+
79+
/*
80+
|--------------------------------------------------------------------------
81+
| Resetting Passwords
82+
|--------------------------------------------------------------------------
83+
|
84+
| These configuration options specify the behavior of Laravel's password
85+
| reset functionality, including the table utilized for token storage
86+
| and the user provider that is invoked to actually retrieve users.
87+
|
88+
| The expiry time is the number of minutes that each reset token will be
89+
| considered valid. This security feature keeps tokens short-lived so
90+
| they have less time to be guessed. You may change this as needed.
91+
|
92+
| The throttle setting is the number of seconds a user must wait before
93+
| generating more password reset tokens. This prevents the user from
94+
| quickly generating a very large amount of password reset tokens.
95+
|
96+
*/
97+
98+
'passwords' => [
99+
'users' => [
100+
'provider' => 'users',
101+
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
102+
'expire' => 60,
103+
'throttle' => 60,
104+
],
105+
],
106+
107+
/*
108+
|--------------------------------------------------------------------------
109+
| Password Confirmation Timeout
110+
|--------------------------------------------------------------------------
111+
|
112+
| Here you may define the number of seconds before a password confirmation
113+
| window expires and users are asked to re-enter their password via the
114+
| confirmation screen. By default, the timeout lasts for three hours.
115+
|
116+
*/
117+
118+
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
119+
120+
];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('admins', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
$table->string('email')->unique();
18+
$table->string('password');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*/
26+
public function down(): void
27+
{
28+
Schema::dropIfExists('admins');
29+
}
30+
};

database/seeders/DatabaseSeeder.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Database\Seeders;
44

5-
use App\Models\User;
65
use Illuminate\Database\Seeder;
76
use Illuminate\Support\Facades\Artisan;
87

@@ -13,11 +12,6 @@ class DatabaseSeeder extends Seeder
1312
*/
1413
public function run(): void
1514
{
16-
User::factory()->create([
17-
'name' => 'Test User',
18-
'email' => 'test@example.com',
19-
]);
20-
2115
Artisan::call('make:filament-user', [
2216
'--name' => 'Admin',
2317
'--email' => 'admin@admin.com',

0 commit comments

Comments
 (0)