使用Laravel默认身份验证验证登录中的其他用户字段



我有此迁移:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->boolean('enable')->default(0);
            $table->string('name', 120)->nullable();
            $table->string('surname', 120)->nullable();
            $table->string('email', 120)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->bigInteger('counter')->default(0);
            $table->string('url_address', 160);
            $table->string('ip', 25)->nullable();
            $table->boolean('isCompany')->default(0);
            $table->boolean('isMailing')->default(0);
            $table->text('content')->nullable();
            $table->string('nip1', 12)->nullable();
            $table->string('business1', 120)->nullable();
            $table->string('phone1', 60)->nullable();
            $table->string('street1', 150)->nullable();
            $table->string('number1', 8)->nullable();
            $table->string('postal_code1', 12)->nullable();
            $table->string('city1', 100)->nullable();
            $table->bigInteger('country_id1')->default(0);
            $table->bigInteger('provincial_id1')->default(0);
            $table->string('nip2', 12)->nullable();
            $table->string('business2', 120)->nullable();
            $table->string('phone2', 60)->nullable();
            $table->string('street2', 150)->nullable();
            $table->string('number2', 8)->nullable();
            $table->string('postal_code2', 12)->nullable();
            $table->string('city2', 100)->nullable();
            $table->bigInteger('country_id2')->default(0);
            $table->bigInteger('provincial_id2')->default(0);
            $table->string('nip3', 12)->nullable();
            $table->string('business3', 120)->nullable();
            $table->string('phone3', 60)->nullable();
            $table->string('street3', 150)->nullable();
            $table->string('number3', 8)->nullable();
            $table->string('postal_code3', 12)->nullable();
            $table->string('city3', 100)->nullable();
            $table->bigInteger('country_id3')->default(0);
            $table->bigInteger('provincial_id3')->default(0);
            $table->decimal('cash', 9, 2)->default(0);
            $table->decimal('lng', 10, 8)->default(0);
            $table->decimal('lat', 10, 8)->default(0);
            $table->boolean('enable_map')->default(0);
            $table->rememberToken();
            $table->timestamps();
            $table->engine = "InnoDB";
        });

我有2个有关登录到Laravel的问题:

  1. 我只能在用户启用= 1

  2. 时才能登录
  3. 参数启用为默认值为0。在单击邮件中的激活链接后,我想更改enable = 0 enable = 1

我该怎么做?

调用attempt()方法时,您可以通过可使用的凭据数组。

您可以如前所述进行并创建自己的控制器,但是如果您使用Laravel随附的auth脚手架(这包括auth:makeapp/Http/Controllers/Auth中的类(,则可以简单地编辑文件:

app/Http/Controllers/Auth/LoginController.php

在这里您要通过添加以下内容来覆盖凭据:

protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'), ['active' => 1]);
}

这意味着当其余的代码自动启动时,它将使信条数组看起来像:

array(
    'username' => 'theusername',
    'password' => 'thepassword',
    'active'   => 1
)

先前提到的是,您应该将属性投放给布尔值,但事实并非如此。Laravel迁移创建了一个微小的Int列而不是布尔值,而铸件仅在处理模型时才能起作用。由于此数组用于生成DB查询条件的位置,因此铸件无法正常工作,因为DB中的值将是1而不是true

为此,您必须制作自定义登录控制器并处理这种情况。我要在下面详细提及我的步骤。

  1. 更新您的路由/web.php

    Route::get('/', function () {
       return redirect(route('login'));
    });
    Route::get('/home', 'HomeController@index')->name('home');
    Route::post('/authenticate', 'LoginController@authenticate')->name('authenticate');
    Route::get('/logout', 'AppHttpControllersAuthLoginController@logout');
    
    1. 创建应用程序/http/controllers/logincontroller.php

并将此方法添加到此控制器

 /**
     * Handle an authentication attempt.
     *
     * @param  IlluminateHttpRequest $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        //ADD VALIDATION CODE HERE
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
  1. 所以我在上面的方法中提到"//此处添加验证代码"在那个地方,您必须验证请求并进行查询以检查该用户是否已启用。

我希望这对您有用。

您必须覆盖默认控制器和路由。

当您自己创建它们时,首先删除验证路线。

然后定义您的控制器。

对于登录部分,您可以创建自己的登录控制器,然后在那里进行自己的登录尝试,这是Laravel使用的。您可以在这里添加所需的属性验证。

public function login(Request $request)
{
    //Validate your form data here
    $request->validate([
        'email' => ['required', 'string'],
        'password' => ['required', 'string'],
    ]);
    //Create your own login attempt here
    $login_atempt = Auth::attempt([
        'email' => $request->email,
        'password' => $request->password,
        'enabled' => true //or 1 , I recommend you to cast your attribute to boolean in your model
    ], $request->filled('remember'));
    if ($login_atempt) {
        $request->session()->regenerate();
        return redirect()->route('home'); //Your home screen route after successful login
    }
    //using custom validation message as Laravel does
    throw ValidationException::withMessages([
        'email' => [trans('auth.failed')],
    ]);
}

还添加了一种调用登录表单的简单方法。

public function showLoginForm()
{
    return view('auth.login');
}

然后您的路线(我将我的控制器命名为UserLoginController(

Route::group(['middleware' => 'guest'], function () {
    Route::get('/login', 'UserLoginController@showLoginForm')->name('login.index');
    Route::post('/login', 'UserLoginController@login')->name('login');
});

对于第二个问题Laravel文档状态

验证了电子邮件地址后,用户将自动重定向到/home。您可以通过定义verification controller上的redirectto方法或属性来自定义验证重定向位置:

protected $redirectTo = '/dashboard';

因此,您可以制作自己的控制器来处理启用属性更改和重定向的控制器。

要完成,请确保您手动添加所需的验证路线。

最新更新