问题
I'm trying to do a basic authentication and when I try to authenticate the user,but Auth :: attempt
method returns false
Blade
{{ Form::open(array('url' => 'usuario')) }}
<input type="text" class="text" id="email" name="email" placeholder="Correo" {{(Input::old('email')) ? 'value ="'. Input::old('email').'"' : '' }}>
<p>{{ $errors->first('email') }}</p>
<input type="text" class="text" id="password" name="password" placeholder="Contraseña" >
<p>{{ $errors->first('password') }}</p>
{{ Form::submit('Ingresar', array('class' => 'bg1')) }}
{{ Form::close() }}
Controller
class UsuarioController extends BaseController{
public function doLogin(){
$rules = array(
'email' => 'required|email',
'password' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::to('usuario')->withErrors($validator)->withInput(Input::except('password'));
}else{
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if(Auth::attempt($userdata)){
return View::make('hello');
}else{
return Redirect::to('usuario');
}
}
}
}
Auth
'driver' => 'database',
'model' => 'Usuario',
'table' => 'Usuario',
Model
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Usuario extends Eloquent implements UserInterface, RemindableInterface{
protected $table = 'Usuario';
protected $primaryKey = 'idUsuario';
protected $hidden = array('Contrasena');
protected $fillable = array(
'Nombre',
'Apellido',
'Tipo',
'password',
'email',
'Fono',
'Foto'
);
}
The code always returns false and redirects me to login, use incorrect data when the database
回答1:
There are many reasons that can make it return false every time:
1) Your password is not correctly stored/hashed, you must hash it with:
Hash::make($password);
This is a way to create users and store passwords correctly hashed by Laravel:
$user = new User;
$user->email = 'example@domain.com';
$user->password = Hash::make('thePasswordToBeHashed');
$user->save();
2) Your password column size has not enough room to store a hashed password. It must be at least 60 bytes long, but migrating it with:
$table->string('password');
Is a good practice, because if this size somehow changes in the future you won't have problem.
3) You don't have a user with the e-mail (Correo) you are trying to login. Double check it, because this is more frequent than we think. And you can also try to do it manually to see if it really works:
$userdata = array(
'Correo' => 'youremail@domain.com',
'Password' => 'password'
);
dd( Auth::attempt($userdata) );
4) Data is not being correctly passed by your form, check if your form really works, by dying it with:
dd(Input::all());
And check if you get the correct data.
5) You have a validation, so the problem could be on it. Add another die message to be sure:
if($validator->fails())
{
dd('Validation is failing!');
...
}
6) From the comment:
$userData = array('email' => Input::get('email'), 'password' => Input::get('password'));
return (string) Auth::attempt($userData));
7) Try to create a different user manually:
$user = new User;
$user->email = 'example@domain.com';
$user->password = Hash::make('passwordExample');
$user->save();
And login with it:
$userData = array('email' => 'example@domain.com', 'password' => 'passwordExample');
return (string) Auth::attempt($userData));
来源:https://stackoverflow.com/questions/23368438/why-the-authattempt-method-always-returns-false