composer create-project laravel/laravel laravel-multi-user-authentication
2. Open project in IDE and run the project
php artisan serve
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=authentication
DB_USERNAME=root
DB_PASSWORD=
run composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run dev
5. Go to the migrations folder and add some items according to your needs in create_user_table.php
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->enum('role', ['admin', 'agent', 'user'])->default('user');
$table->rememberToken();
$table->timestamps();
});
}
6. Go to the Model folder and update User.php
protected $guarded = [];
7. Migrate database
php artisan migrate
8. Create a new seeder named UserTableSeeder
php artisan make:seeder UserTableSeeder
9. Go to UserTableSeeder.php and add some value
public function run(): void
{
DB::table('users')->insert([
// Admin
[
'name' => 'Admin',
'username' => 'admin',
'email' => 'admin@gmail.com',
'password' => Hash::make('111'),
'role' => 'admin',
],
// Agent
[
// Agent Information
],
// User
[
// User Information
]
]);
}
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'role' => fake()->randomElement(['admin', 'agent', 'user']),
'remember_token' => Str::random(10),
];
}
public function run(): void
{
$this->call(UserTableSeeder::class);
\App\Models\User::factory(5)->create();
}
php artisan migrate:fresh --seed
Route::get('/admin/dashboard', [AdminController::class, 'AdminDashboard'])->name('admin.dashboard');
php artisan make:controller AdminController
public function AdminDashboard(){
}
17. Add some HTML code in admin_dashboard.blade.php
18. Go to AdminController.php and redirect AdminDashboard into admin.admin_dashboard file
public function AdminDashboard(){
return view('admin.admin_dashboard');
}
php artisan make:controller AgentController
Route::get('/agent/dashboard', [AgentController::class, 'AgentDashboard'])->name('agent.dashboard');
public function AgentDashboard(){
}
23. Add some HTML code in agent_dashboard.blade.php
24. Go to AgentController.php and redirect AgentDashboard into agent.agent_dashboard file
public function AdminDashboard(){
return view('admin.admin_dashboard');
}
public function store(LoginRequest $request): RedirectResponse
{
$request->authenticate();
$request->session()->regenerate();
$url = '';
if($request->user() -> role === 'admin'){
$url = 'admin/dashboard';
} elseif($request -> user() -> role === 'agent'){
$url = 'agent/dashboard';
} elseif($request -> user() -> role === 'user'){
$url = '/dashboard';
}
return redirect()->intended($url);
}
php artisan make:middleware Role
'role' => \App\Http\Middleware\Role::class,
public function handle(Request $request, Closure $next, $role): Response
{
if($request->user()-> role !== $role){
return redirect('dashboard');
}
return $next($request);
}
// Admin Route
Route::middleware(['auth', 'role:admin'])->group(function(){
Route::get('/admin/dashboard', [AdminController::class, 'AdminDashboard'])->name('admin.dashboard');
});
// Agent Route
Route::middleware(['auth', 'role:agent'])->group(function(){
Route::get('/agent/dashboard', [AgentController::class, 'AgentDashboard'])->name('agent.dashboard');
});