How do I authenticate a user in Laravel 8 Jetstream only if his status is active?

巧了我就是萌 提交于 2021-02-05 06:12:05

问题


I am building a Laravel 8 application and have successfully implemented authentication. Now I want to check if a user's status is active before logging him in. I have added a field in the users table

username varchar
password varchar
....
status tinyint(1)
...

I am using JetStream and Fortify

Thank You


回答1:


You can customize user authentication from app\Providers\JetStreamServiceProvider.php, on boot method :

use App\Models\User;
use Illuminate\Http\Request;
use Laravel\Fortify\Fortify;

public function boot()
{
    $this->configurePermissions();

    Jetstream::createTeamsUsing(CreateTeam::class);
    Jetstream::updateTeamNamesUsing(UpdateTeamName::class);
    Jetstream::addTeamMembersUsing(AddTeamMember::class);
    Jetstream::deleteTeamsUsing(DeleteTeam::class);
    Jetstream::deleteUsersUsing(DeleteUser::class);
    // Below code is for your customization
    Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)->first();

        if ($user->status == 1) {  // it will return if status == 1
            return $user;
        }
    });
}

See the official Jetstream documentation of Customizing User Authentication



来源:https://stackoverflow.com/questions/64835186/how-do-i-authenticate-a-user-in-laravel-8-jetstream-only-if-his-status-is-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!