I want to restrict login when the user is disabled by the Auth feature

Asked 2 years ago, Updated 2 years ago, 35 views

I'm using Ravel 6.

I would like to add a specific column (Invalid) to Users (table for login users) with the default Auth feature, and when this is true, I want the login screen to show that it behaves like a non-existent user or is invalid.Is there a good way?

laravel

2022-09-30 16:32

1 Answers

There are several possible ways.

If you want to meet your needs with simple behaviors such as "Display 403 when an invalid user accesses a page that requires authentication," it may be easier to use the middleware.

For example, if you create middleware such as the following and register it for each route, users with invalid=true will not be able to access those routes, and page 403 will appear instead.

namespace App\Http\Middleware;

use Close;

classIsUserValid
{
    public function handle($request,Closure$next,$role)
    {
        if($request->user()->invalid){
            abort(403, 'error message');
        }

        return$next($request);
    }
}

If you need to show that the user is invalid as a validation message on the login page, I think Create a custom validation rule would be good.

Alternatively, I haven't tried it, but I think I can do the same thing using the existing exist rule because I can customize the eloquent query when I check it.

By the way, even if we implement validation when logging in, it would be better to register middleware.If the user is already logged in and changes to user.invalid=false, it will still be accessible without middleware.


2022-09-30 16:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.