【发布时间】:2015-02-23 23:55:58
【问题描述】:
if(Auth::attempt(['username' => Input::get('username'), 'password' => Input::get('password')])){
return Auth::user();
}
return 'Failed!';
这是有问题的代码,我确信问题出在Auth::attempt 行。我找到了关于这个的其他主题,但他们都没有解决方案。即:https://laracasts.com/forum/?p=1314-laravel-from-scratch-authentication-lesson-help/0
我的User 模型实现了UserInterface,并且我还将use Illuminate\Support\Facades\Auth as Auth; 放入控制器中,以防在Auth::attempt 部分中无法识别它。然而,无论有没有use ...,它都不起作用。
我也试过了
Auth::attempt(Input::only('username', 'password'))
而且我总是会到达Failed! 部分...
密码作为哈希字符串存储在数据库中,当我使用以下内容进行调试时:
echo Input::get('username') . "____" . Input::get('password');
die();
我得到了正确的结果。所以,除了::attempt函数中的问题,我想不出别的了。
有什么建议吗?
**更新:**User 模型
使用 Illuminate\Auth\UserTrait; 使用 Illuminate\Auth\UserInterface; 使用 Illuminate\Auth\Reminders\RemindableTrait; 使用 Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $fillable = [
'username',
'password',
'email'
];
public static $rules = [
'username' => 'required',
'password' => 'required',
'email' => 'required'
];
public static $error_messages;
/**
* 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 = ['password', 'remember_token'];
public static function isValid($input)
{
$validation = Validator::make($input, static::$rules);
if ($validation->passes()) return true;
static::$error_messages = $validation->messages();
return false;
}
【问题讨论】:
-
你能告诉我们你的
User模特吗? -
@lukasgeiter - 已更新。 :)
-
@lukasgeiter - 我发现了问题并发布了答案。不过,谢谢你的尝试。
-
太棒了!我要花很长时间才能猜到那个。我会将它添加到我脑海中的列表中,以解决 SO 上的下一个“auth is not working”问题;)
标签: authentication laravel laravel-4