【问题标题】:Laravel 4 - Auth::attempt() not workingLaravel 4 - Auth::attempt() 不工作
【发布时间】:2014-06-20 14:14:53
【问题描述】:

我一直在到处搜索,但无法弄清楚为什么我的 Auth::attempt() 一直失败。请帮帮我。

这是我的表格:

<h2>Admin Login</h2>
<form method="post" action="<?php echo url('login-process') ?>">
    Username: <input type="text" name="username" /> <br/>
    Password: <input type="password" name="password" />
    <button type="submit">Login</button>
</form>

这是我的登录功能:

$username = Input::get('username');
$password = Input::get('password');

if (Auth::attempt(array('username' => $username, 'password' => $password))) {

        echo "success!"; //just for testing purposes
} else {
        echo "fail!";

我的 User.php 是默认的。看起来像这样:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

}

这就是我保存新用户信息的方式(使用 Eloquent):

$user = new User();
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();

但是当用户使用正确的凭据登录时,我一直失败。现在已经为此工作了几天,不禁觉得我错过了一些微不足道的东西。请帮忙?谢谢!

【问题讨论】:

  • 您在数据库中的密码字段是否至少有 60 个字符长?
  • @AntonioCarlosRibeiro 是的,它有 64 个字符长

标签: php authentication laravel laravel-4


【解决方案1】:

如果您在这方面有更多错误会更容易,错误消息或其他内容。但以下是我登录用户的方式。自最新安装 Laravel 4.1.26 以来,新方法给了我错误。

它不在下面的部分,但我认为它的格式更清晰一些。它可能是新记住令牌的方法。请提供更多错误信息。你得到一个 Laravel 错误。您拥有的回声将不会显示,因为它不会保留该屏幕。也许使用return var_dump('Success') 这会破坏这个过程。

$credentials = [
   "username" => Input::get("username"),
   "password" => Input::get("password")
];
if(Auth::attempt($credentials)) {
   return true;
} else {
   return false;
}

在你的 User 模型中实现这些方法是一件好事,这些方法从 4.1.26 开始是必需的:

public function getRememberToken() {
   return $this->remember_token;
}


public function setRememberToken($value) {
   $this->remember_token = $value;
}


public function getRememberTokenName() {
   return 'remember_token';
}

【讨论】:

  • 我没有收到任何 laravel 错误。即使我输入了正确的凭据,我每次都会收到“失败”的回声。
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 2014-08-31
  • 2014-10-23
  • 2013-02-27
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
相关资源
最近更新 更多