skip to Main Content

I want to check an extra field that is stored in the users table (for example approval field) in the login process. In what way I can handle this, actually out of the vendor’s scope?

2

Answers


  1. There are many ways that you can solve this problem. Here I will talk about the simplest one:

    Step 1: Create a Middleware

    php artisan make:middleware "MiddlewareName"
    

    Step 2: Check your specific column

    public function handle(Request $request, Closure $next)
    {
        if (! auth()->user()->approval) {
            return redirect()->route('route-name');
        }
        return $next($request);
    }
    

    Step 3: Add your middleware to route (after login)

    Route::prefix('dashboard')->middleware(['auth', 'auth_user_verified'])->group(function () {
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard.index');
    });
    
    Login or Signup to reply.
  2. you can do that only by creating your custom LoginController and set backpack config to use your controller instead of the vendor’s one.

    Custom Login Controller

    <?php
    use AppHttpControllersAuthLoginController;
    use IlluminateHttpRequest;
    
    class CustomLoginController extends LoginController
    {
        public function login(Request $request)
        {
            ...
    
            $credentials = $this->credentials($request);
            $credentials['approved'] = true;
    
            // Attempt to log the user in
            if ($this->guard()->attempt($credentials)) {
                ...
            }
    
            // Redirect if login fails
            ...
        }
    }
    

    now go to config/backpack/base.php and declare your custom login controller:

    ...
    'setup_auth_routes' => false,
    

    web.php

    ...
    Route::group(['middleware' => 'web', 'prefix' => config('backpack.base.route_prefix')], function () {
        Route::auth();
        Route::get('login', 'AuthCustomLoginController@login');
    });
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search