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

时间:2019-05-15 18:24:06

标签: php laravel laravel-5

我有这次迁移:

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";
        });

关于登录Laravel,我有两个问题:

  1. 仅当用户启用enable = 1时,我才能登录

  2. 参数enable为默认值0。单击邮件中的激活链接后,我想在enable = 1上更改enable = 0

我该怎么做?

3 个答案:

答案 0 :(得分:1)

调用attempt()方法时,您可以传递一组可以使用的凭据。

您可以按照前面所述进行操作并创建自己的控制器,但是如果您使用的是laravel随附的身份验证支架(其中包括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]);
}

这意味着当其余代码自动插入时,它将使credentails数组看起来像:

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

之前提到过,您应该将属性强制转换为布尔值,但这不是事实。 Laravel迁移会创建一个很小的int列而不是布尔值,并且强制转换仅在处理模型时有效。由于此数组用于生成数据库查询的条件,因此强制转换将不起作用,因为数据库中的值为1而不是true

答案 1 :(得分:-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。您可以通过在VerificationController上定义redirectTo方法或属性来自定义验证后重定向位置:

protected $redirectTo = '/dashboard';

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

要完成此操作,请确保手动添加所需的身份验证路由。

答案 2 :(得分:-1)

为此,您必须制作自定义登录控制器并处理这种情况。 我要详细提及这一点,请按照以下步骤检查我。

  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', '\App\Http\Controllers\Auth\LoginController@logout');
    
    1. 创建app / Http / Controllers / LoginController.php

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

 /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $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. 所以我在上面的方法中提到了“ // ADD VALIDATION CODE HERE ”,那么您必须验证请求并进行查询,以检查该用户是否已启用。

我希望这对您有用。

相关问题