【问题标题】:Laravel 5.1 Auth::attempt() doesn't work after Composer updateLaravel 5.1 Auth::attempt() 在 Composer 更新后不起作用
【发布时间】:2016-07-19 17:05:13
【问题描述】:

我在 Laravel 5.1 中的项目,在 Composer 更新后我无法登录

在我运行之前一切正常

composer update

我正在使用Laravel's register and login process 内置的标准 AuthController.php 中的函数 Auth::attempt() 总是返回 false

我已经在 Laravel 5.1 的一个新项目上对其进行了测试,并且出现了同样的问题。 我重设了密码,创建了新用户,但没有任何效果……

我得到这个错误:

这些凭据与我们的记录不符

这些是更新的包:

Updating dependencies (including require-dev)
- Removing giggsey/libphonenumber-for-php (7.2.6)
- Installing giggsey/libphonenumber-for-php (7.2.8)
  Downloading: 100%

- Removing symfony/var-dumper (v2.7.10)
- Installing symfony/var-dumper (v2.7.11)
  Downloading: 100%

- Removing symfony/translation (v2.7.10)
- Installing symfony/translation (v2.7.11)
  Downloading: 100%

- Removing symfony/routing (v2.7.10)
- Installing symfony/routing (v2.7.11)
  Downloading: 100%

- Removing symfony/process (v2.7.10)
- Installing symfony/process (v2.7.11)
  Downloading: 100%

- Installing symfony/polyfill-mbstring (v1.1.1)
  Downloading: 100%

- Removing symfony/http-foundation (v2.7.10)
- Installing symfony/http-foundation (v2.7.11)
  Downloading: 100%

- Removing symfony/event-dispatcher (v2.8.3)
- Installing symfony/event-dispatcher (v2.8.4)
  Downloading: 100%

- Removing symfony/debug (v2.7.10)
- Installing symfony/debug (v2.7.11)
  Downloading: 100%

- Removing symfony/http-kernel (v2.7.10)
- Installing symfony/http-kernel (v2.7.11)
  Downloading: 100%

- Removing symfony/finder (v2.7.10)
- Installing symfony/finder (v2.7.11)
  Downloading: 100%

- Removing symfony/dom-crawler (v2.7.10)
- Installing symfony/dom-crawler (v2.7.11)
  Downloading: 100%

- Removing symfony/css-selector (v2.7.10)
- Installing symfony/css-selector (v2.7.11)
  Downloading: 100%

- Removing symfony/console (v2.7.10)
- Installing symfony/console (v2.7.11)
  Downloading: 100%

- Removing psy/psysh (v0.7.1)
- Installing psy/psysh (v0.7.2)
  Downloading: 100%

- Removing paragonie/random_compat (v1.2.1)
- Installing paragonie/random_compat (v1.4.1)
  Downloading: 100%

- Removing monolog/monolog (1.18.0)
- Installing monolog/monolog (1.18.1)
  Downloading: 100%

- Removing league/flysystem (1.0.18)
- Installing league/flysystem (1.0.20)
  Downloading: 100%

- Removing symfony/polyfill-util (v1.1.0)
- Installing symfony/polyfill-util (v1.1.1)
  Downloading: 100%

- Removing symfony/polyfill-php56 (v1.1.0)
- Installing symfony/polyfill-php56 (v1.1.1)
  Downloading: 100%

- Removing propaganistas/laravel-phone (2.6.1)
- Installing propaganistas/laravel-phone (2.7.0)
  Downloading: 100%

- Removing symfony/yaml (v3.0.3)
- Installing symfony/yaml (v3.0.4)
  Downloading: 100%

- Removing phpunit/phpunit (4.8.23)
- Installing phpunit/phpunit (4.8.24)
  Downloading: 100%

- Removing phpspec/phpspec (2.4.1)
- Installing phpspec/phpspec (2.5.0)
  Downloading: 100%

知道是哪个软件包导致了问题吗? 任何解决方法或想法如何解决这个问题?

postLogin 函数(它是标准的,我没有做任何更改):

public function postLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);

    // 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.
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->getCredentials($request);

    if (Auth::attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    // 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.
    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }

    return redirect($this->loginPath())
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors([
            $this->loginUsername() => $this->getFailedLoginMessage(),
        ]);
}

postRegister 函数:

public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::login($this->create($request->all()));

    return redirect($this->redirectPath());
}

【问题讨论】:

  • Auth::attempt() 方法与composer install 和/或composer update 无关。问题出在您的控制器代码上。您确定您的身份验证代码正确吗?提供控制器方法代码...
  • 不工作是什么意思?你有没有收到任何错误信息,如果有分享错误信息。
  • @user3514160 在更新之前它正在工作。我收到错误:这些凭据与我们的记录不匹配
  • 你能给我看看代码吗?另请注意,Auth::attempt() 方法使用password_verify() 方法检查密码是否经过哈希处理。您是否以哈希格式存储密码???
  • @user3514160 不,我不能给你看代码,但正如我所说,这是标准 laravel 的登录过程,没什么特别的......你可以试试,克隆一个 laravel 5.1 的新项目并做一个作曲家更新laravel.com/docs/5.2/authentication#authentication-quickstart

标签: php laravel authentication composer-php laravel-5.1


【解决方案1】:

我找到了问题!

我正在使用这个包Roles And Permissions For Laravel 5,但忘记在创建时为用户设置一个具有登录权限的角色……

显然,我正在使用在播种过程中创建的用户进行测试(它附加了一个角色),但是当我尝试注册新用户时,问题就开始了……

$user->attachRole($regularUserRole);

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    $team = new Team(["name" => ""]);
    $regularUserRole = Role::where('slug','user')->first();
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
    if ($team->save() && $regularUserRole) {
        $user->team()->associate($team);
        $user->attachRole($regularUserRole);
        if ($user->save()) {
            $team->owner()->associate($user);
            $team->save();
            return $user;
        }
    }
    return false;
}

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2018-06-24
    • 1970-01-01
    • 2016-05-17
    • 2017-01-06
    • 2018-02-27
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多