【问题标题】:Laravel: Using multiple columns for authenticationLaravel:使用多列进行身份验证
【发布时间】:2020-11-14 13:29:36
【问题描述】:

我有一个 Laravel 6 应用程序。

在典型的应用程序中,用户只需指定他们的email,其中电子邮件是唯一的。

但是,在我的应用程序中,我在 User 模型中有 2 列用于对用户进行身份验证。

  • app_id
  • email
  • unique(app_id, email)

所以为了登录,我们需要同时传递app_idemail,以及password。相同的email 可以用于不同的app_ids。

我将如何实现这一目标?

【问题讨论】:

  • 您是否检查过身份验证文档以手动验证用户身份?结合查看 LoginController 应该是您所需要的全部
  • 问题是我只能找到自定义一个列。不跨列查询。
  • laravel.com/docs/6.x/authentication#authenticating-users 它告诉您如何执行此操作.. 一旦您知道attempt 的工作原理,您可以调整 LoginController 的特定方法以根据需要调整凭据和验证...如果您没有使用 LoginController 那么你大部分都需要

标签: laravel laravel-6 laravel-authentication laravel-authorization


【解决方案1】:

Auth::routes() 提供的默认登录操作如下:

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');

这是默认的login 函数,是LoginController 使用的AuthenticatesUsers trait 的一部分:

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

有几种方法可以解决这个问题。

选项 1:覆盖 LoginController 中的 login 函数

# app/Http/Controllers/Auth/LoginController.php

    public function login(Request $request)
    {
        // add more stuff like validation, return the view you want, etc.
        // This is barebones
        auth()->attempt($request->only(['app_id', 'login', 'password']);
    }

选项 2:覆盖 LoginController 中的 validateLogincredentials 函数

# app/Http/Controllers/Auth/LoginController.php

    protected function validateLogin(Request $request)
    {
        $request->validate([
            'app_id' => 'required|string',
            'email' => 'required|string',
            'password' => 'required|string',
        ]);
    }

    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only('app_id', 'email', 'password');
    }

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2021-07-20
    • 2020-12-30
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2018-09-21
    相关资源
    最近更新 更多