Skip to content

Latest commit

 

History

History
239 lines (239 loc) · 8.99 KB

File metadata and controls

239 lines (239 loc) · 8.99 KB

Step By Step Guid

1. Create a project

        composer create-project laravel/laravel laravel-multi-user-authentication
    

2. Open project in IDE and run the project

        php artisan serve
    
3. Connect database

        DB_CONNECTION=mysql
        DB_HOST=127.0.0.1
        DB_PORT=3306
        DB_DATABASE=authentication
        DB_USERNAME=root
        DB_PASSWORD=
    
4. Install Breeze

        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
                ]
            ]);
        }
    
10. Go to the factories folder and update the fake element in UserFactory.php

        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),
            ];
        }
    
11. Go to DatabaseSeeder.php and update this so that UserTableSeeder.php has been called from this file

        public function run(): void
        {
            $this->call(UserTableSeeder::class);
            \App\Models\User::factory(5)->create();
        }
    
12. Migrate database

        php artisan migrate:fresh --seed
    
13. Go to web.php and make route for admin

        Route::get('/admin/dashboard', [AdminController::class, 'AdminDashboard'])->name('admin.dashboard');
    
14. Make a controller named AdminController

        php artisan make:controller AdminController
    
15. Go to AdminController.php and add a function named AdminDashboard

        public function AdminDashboard(){   
        }
    
16. Make a folder in view path named admin and make a file named admin_dashboard.blade.php in admin folder
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');
        }
    
19. Make a controller named AgentController

        php artisan make:controller AgentController
    
20. Go to web.php and make route for agent

        Route::get('/agent/dashboard', [AgentController::class, 'AgentDashboard'])->name('agent.dashboard');
    
21. Go to AgentController.php and add a function named AgentDashboard

        public function AgentDashboard(){
        }
    
22. Make a folder in view path named agent and make a file named agent_dashboard.blade.php in agent folder
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');
        }
    
25. Go to Auth file and open AuthenticatedSessionController.php and update it so that after login, user has been redirecting to the appropriate page

        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);
        }
    
25. Make middleware named Role

        php artisan make:middleware Role
    
27. Register Role middleware into $middlewareAliases in the Kernel.php file

        'role' => \App\Http\Middleware\Role::class,
    
28. Go to Role middleware, add $role value in handle function and update that function

        public function handle(Request $request, Closure $next, $role): Response
        {
            if($request->user()-> role !== $role){
                return redirect('dashboard');
            }
            return $next($request);
        }
    
29. Go to web.php and protect admin route and agent route through middleware

        // 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');     
        });