为什么Auth :: attempt方法总是返回false? [英] Why the Auth::attempt method always returns false?

查看:564
本文介绍了为什么Auth :: attempt方法总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行基本身份验证,并且尝试对用户进行身份验证,但是Auth :: attempt方法返回false

I'm trying to do a basic authentication and when I try to authenticate the user,but Auth :: attempt method returns false

刀片

   {{ 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() }}

控制器

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');
    }

}
}
}

身份验证

'driver' => 'database',
'model' => 'Usuario',
'table' => 'Usuario',

模型

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'
                    );
}

代码总是返回false并重定向我登录,在数据库时使用不正确的数据

The code always returns false and redirects me to login, use incorrect data when the database

推荐答案

有很多原因可以使它每次都返回false:

There are many reasons that can make it return false every time:

1)您的密码未正确存储/散列,您必须使用以下密码对其进行哈希处理:

1) Your password is not correctly stored/hashed, you must hash it with:

Hash::make($password);

这是一种创建用户并存储由Laravel正确散列的密码的方法:

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)您的密码列大小没有足够的空间来存储哈希密码.它必须至少为60个字节长,但可以使用以下方式进行迁移:

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)您没有要尝试登录的电子邮件(Correo)用户.请仔细检查,因为这比我们想象的要频繁.您也可以尝试手动执行此操作,以查看其是否确实有效:

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)您的表单没有正确传递数据,请通过以下方法检查表单是否正常工作:

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)您有一个验证,所以问题可能出在此上.添加另一个死消息,以确保:

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)从评论中:

$userData = array('email' => Input::get('email'), 'password' => Input::get('password'));            

return (string) Auth::attempt($userData));

7)尝试手动创建其他用户:

7) Try to create a different user manually:

$user = new User;

$user->email = 'example@domain.com';

$user->password = Hash::make('passwordExample');

$user->save();

并使用它登录:

$userData = array('email' => 'example@domain.com', 'password' => 'passwordExample');            

return (string) Auth::attempt($userData));

这篇关于为什么Auth :: attempt方法总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆