本文介绍了仅当状态为活动时,如何在Laravel 8 Jetstream中对用户进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Laravel 8应用程序,并已成功实现身份验证.现在,我想在登录之前检查用户的状态是否为活动状态.我在users表中添加了一个字段

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)
...

我正在使用 JetStream Fortify

谢谢

推荐答案

您可以在 boot 方法上,通过 app \ Providers \ JetStreamServiceProvider.php 自定义用户身份验证:/p>

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 && Hash::check($request->password, $user->password)) {
            if ($user->status == 1) {  // it will return if status == 1
                 return $user;
            }
       }

    });
}

请参阅 Jetstream 文档.rel ="nofollow noreferrer">自定义用户身份验证

See the official Jetstream documentation of Customizing User Authentication

这篇关于仅当状态为活动时,如何在Laravel 8 Jetstream中对用户进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 05:00