我正在跟随Laracasts的“Build Larabook from Scratch”系列 我一遍又一遍地检查代码,但找不到我的错误。 我可以注册一个用户,因此Auth :: login($ user)可以工作,但我无法使Auth :: attempt()工作。
我尝试了以下(以及更多)。我无法理解。
如何调试此问题?!我想知道为什么Auth :: attempt()失败!!
请帮忙!
我的登录路线
Route::post('login', [
'as' => 'login_path',
'uses' => 'SessionsController@store'
]);
我的SessionsController构造函数()和store()方法[记录暂时尝试解决此问题]
public function __construct(SignInForm $signInForm) {
$this->signInForm = $signInForm;
$this->beforeFilter('guest', ['except' => 'destroy']);
}
public function store() {
$input = Input::only('email', 'password');
Log::info('User tried to login with email => ' . Input::get('email') . ' and password => ' . Input::get('password'));
$this->signInForm->validate($input);
$info = [
'email' => Input::get('email'),
'password' => Input::get('password')
];
if (Auth::attempt($info)) {
Log::info('AUTH ATTEMPT was successful');
// Add flash message "Welcome Back"
return Redirect::intended('/statuses');
} else {
Log::info('AUTH ATTEMPT failed');
return Redirect::to('login');
}
}
我的SignInForm.php
namespace Fujibook\Forms;
use Laracasts\Validation\FormValidator;
class SignInForm extends FormValidator {
/**
*Validation rules for the registration form
* @var type
*/
protected $rules = [
'email' => 'required',
'password' => 'required'
];
}
我的create.blade.php
<h1>Sign In</h1>
{{ Form::open(['route' => 'login_path']) }}
<div class="form-group">
{{ Form::label('email', 'Email:') }}
{{ Form::email('email', null, ['class' => 'form-control', 'required' => 'required']) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password:') }}
{{ Form::password('password', null, ['class' => 'form-control', 'required' => 'required']) }}
</div>
<div class="form-group">
{{ Form::submit('Sign In', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
最后,我的用户模型\ Fujibook \ Users
<?php
namespace Fujibook\Users;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Fujibook\Registration\Events\UserRegistered;
use Laracasts\Commander\Events\EventGenerator;
use Eloquent, Hash;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait,
RemindableTrait,
EventGenerator;
/**
* Which fileds that may be massassigned
*
* @var type
*/
protected $fillable = ['username', 'email', 'password'];
/**
* 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', 'remember_token');
/**
* Passwords must always be used;
* @param type $password
*/
public function setPasswordAttribute($password){
$this->attributes['password'] = Hash::make($password);
}
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
/**
* Register a new user
* @param type $username
* @param type $email
* @param type $password
*/
public static function register($username, $email, $password) {
$user = new static(
compact('username', 'email', 'password')
);
// raise an event
$user->raise(new UserRegistered($user));
return $user;
}
}
此外,我已在app / config / auth.php中的模型中设置模型
'model' => 'Fujibook\Users\User',
我可能还包括我的数据库迁移
public function up() {
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 70);
$table->timestamps();
$table->rememberToken();
});
}
答案 0 :(得分:0)
首先,您不需要再次使用Input::get()
来获取电子邮件和密码。数据库中的字段名称是什么,用户名或电子邮件?我想象一下电子邮件,如果是这样,请将登录代码更改为:
public function store() {
$input = Input::only('email', 'password');
Log::info('User tried to login with email => ' . Input::get('email') . ' and password => ' . Input::get('password'));
$this->signInForm->validate($input);
if (Auth::attempt($input)) {
Log::info('AUTH ATTEMPT was successful');
// Add flash message "Welcome Back"
return Redirect::intended('/statuses');
} else {
Log::info('AUTH ATTEMPT failed');
return Redirect::to('login');
}
}
答案 1 :(得分:0)
我看到的问题只是这个
$info = [
'username' => Input::get('email'), <--- username = email???
'password' => Input::get('password')
];
if (Auth::attempt($info)) {
不应该是这样的:
$info = [ 'email' => Input::get('email'), 'password' => Input::get('password') ];