【问题标题】:Where to find the Auth::attempt method in laravel在 laravel 中哪里可以找到 Auth::attempt 方法
【发布时间】:2019-02-03 10:02:31
【问题描述】:

我想在我的 Laravel 项目中除了用户的邮箱和密码外,还想在认证查询中添加额外的条件

我在官方 laravel 文档中阅读了此内容。 https://laravel.com/docs/5.6/authentication

指定附加条件如果您愿意,您还可以添加额外的 除了用户的身份验证查询的条件 电子邮件和密码。例如,我们可以验证用户是否被标记为 “活跃”:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // The user is active, not suspended, and exists.
}

我在哪里可以找到这个方法?

【问题讨论】:

  • “在哪里可以找到上述方法”是什么意思?它与向数组中添加附加元素的方法相同
  • 在您的 LoginController (app/Http/Controllers/Auth/LoginController.php) 中,并覆盖方法进行身份验证。如果还有,执行:php artisan make:auth
  • 对于那些在 Laravel 8 中遇到这个问题的人,我相信 Laravel 的基本身份验证脚手架中定义的实际 Auth::attempt 方法在 \Illuminate\Auth\Sessionguard.php 中。

标签: php laravel


【解决方案1】:

AuthController.php

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
/**
 * Handle an authentication attempt.
 *
 * @return Response
 */
public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}
}

这是您要找的那个吗?

【讨论】:

    【解决方案2】:

    如果你使用 Laravel 提供的 auth 脚手架,它将在 AuthenticatesUsers trait 中:

    /**
     * Attempt to log the user into the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function attemptLogin(Request $request)
    {
        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );
    }
    

    您可以在 LoginController 中覆盖此方法。

    【讨论】:

    • 谢谢,这正是我需要的,我找到了函数 credential(request $request),它接受用户名和密码,现在我想向它添加新参数,例如 active = 1, Devon
    【解决方案3】:

    您需要在 LoginController 中覆盖方法登录

    public function login(\Illuminate\Http\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 ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }
    
        // This section is the only change
        if ($this->guard()->validate($this->credentials($request))) {
            $user = $this->guard()->getLastAttempted();
    
            // Make sure the user is active
            if ($user->active && $this->attemptLogin($request)) {
                // Send the normal successful login response
                return $this->sendLoginResponse($request);
            } else {
                // Increment the failed login attempts and redirect back to the
                // login form with an error message.
                $this->incrementLoginAttempts($request);
                return redirect()
                    ->back()
                    ->withInput($request->only($this->username(), 'remember'))
                    ->withErrors(['active' => 'You must be active to login.']);
            }
        }
    
        // 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);
    }
    

    这对我有用

    【讨论】:

    • 我会检查的
    【解决方案4】:

    您呼叫的Auth::attempt 很可能是Illuminate\Auth\SessionGuard@attempt

    路径:

    Auth::attempt -&gt; Illuminate\Auth\AuthManager -&gt; (guard) Illuminate\Auth\SessionGuard

    Facade -&gt; Illuminate\Auth\AuthManager@call -&gt; @guard -&gt; Illuminate\Auth\SessionGuard@attempt

    Laravel 5.6 Docs - Facades - Class Reference

    为了能够调整为LoginController 传递的凭据,您只需覆盖1 个方法credentials,因为它是传递给attempt 的数组所涉及的唯一方法。

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

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 2017-02-14
      • 1970-01-01
      • 2017-03-19
      • 2011-06-16
      • 2015-04-28
      • 2023-03-05
      • 2017-05-13
      • 1970-01-01
      相关资源
      最近更新 更多